Rotated Images with CSS3
by c.bavota | Posted in Tutorials | 13 comments
I have been playing around with a lot of CSS3 lately due to PressWork, and I’ve come up with some cool tricks. One that I really like uses the transform style to create a rotated image. If you surround it with a div container and mask the outer edges, you get a pretty awesome effect.
Let’s take a look at the HTML required:
<div class="image-box"> <img src="http://your-site/images/your-image.jpg" alt="" width="300" height="200" /> </div>
That is pretty straightforward enough and it gives us something like this:
We need some CSS to get it looking how we want:
.image-box img {
transform: rotate(15deg);
-moz-transform: rotate(15deg);
-webkit-transform: rotate(15deg);
}
That gives us:
Not exactly what we want, but we’re getting there.
Let’s crop the div container to mask out the image’s edges:
.image-box {
width: 200px;
height: 150px;
overflow: hidden;
}
Our image now looks like this:
We need to center it within the div container and that requires a few modifications to the CSS:
.image-box {
width: 200px;
height: 150px;
overflow: hidden;
position: relative;
}
.image-box img {
transform: rotate(15deg);
-moz-transform: rotate(15deg);
-webkit-transform: rotate(15deg);
position: absolute;
left: 50%;
top: 50%;
margin: -100px 0 0 -150px;
}
That’s exactly what we want. It might look a little plain but now that you have the basic idea, you can add some more CSS to make it look even better:
.image-box {
width: 200px;
height: 150px;
overflow: hidden;
position: relative;
border: 5px solid #eee;
box-shadow: 0 0 1px rgba(0,0,0,0.3);
-moz-box-shadow: 0 0 1px rgba(0,0,0,0.3);
-webkit-box-shadow: 0 0 1px rgba(0,0,0,0.3);
}
.image-box img {
transform: rotate(15deg);
-moz-transform: rotate(15deg);
-webkit-transform: rotate(15deg);
position: absolute;
left: 50%;
top: 50%;
margin: -100px 0 0 -150px;
}
With CSS3 you no longer need to rely on something like Photoshop to get certain effects on your images. All it takes is a few lines of CSS3.



Great to know Chris! What makes this effect even cooler is mixing it with a CSS3 drop shadow (-moz-box-shadow: 3px 3px 3px #666;) to add a nice defined shadow to the rotated image.
True dat.
I really do like the effect of having a drop shadow on an image. I tend to think it makes it jump out of the page a little more, and makes everything a little less flat.
I am really enjoying CSS3. We have been trying to work it in to projects as much as possible and it saves a lot of time. I always hated cutting images for shadows and rounded corners and rotating images was even worse because you had to make the image so much larger than it needed to be.
Awesome, I am still a beginner when it comes to CSS and didn’t know it was possible to do this.
Thanks for sharing. Thanks also to Christopher Ross above for the shadow tip. BTW Christopher, your avatar is strange, does it represent something?
This is a great article and it’s a very pretty feature that I think people should start capitalizing on!
You can create really unique and cool features with rotate that would otherwise require a 100% image, that can now be done with simple css.
Wow, it´s fantastic what CSS can do. Until now I did this all with Photoshop but now I will give it a try with true CSS. Thanks for sharing!
I never knew about CSS3 before reading. It is nice to know there is a simpler photo editing process out there besides photoshop! Thanks for sharing this as well!
Wow, what a nice and simple tutorial.
Thanks.
Liked your blog, bookmarking it.
Thanks for the detailed tutorial. Now I know how to apply the deformation of images directly in the engine
Thanks for the tutorial, this will help me to make my blog more stylish
.