Placed in: Home arrow Programming arrow CSS arrow 3d animation using pure CSS3
3d animation using pure CSS3

A couple of days ago, somebody tweeted a great looking CSS3 example. I was absolutely stunned by the example And all that Malarkey created and was wondering how he was able to produce that kind of effect. I knew about the CSS3 transition property, but how to create that 3d effect?

At that point, I started digging through the Safari Reference Library, where I eventually found the Safari CSS Visual Effects Guide: Transforms page. This page describes a CSS property I've never seen before: -webkit-perspective.

The perspective property is what we need to create the 3d effect. Using transform and transition, we can create 3d animation using pure CSS3.

3d animation using pure CSS3

Simply open up the demo page and hover the movie poster elements and be amazed. As you could have expect, this technique will only work on -webkit based browsers (Latest version of Safari and probably Chrome too). Even Safari on the iPhone displays the 3d effect, but not the animations.

Demo 3d animation using pure CSS3   Download 3d animation using pure CSS3

For those who don't have a webkit browser, I've added a small video to this post to see the demo in action. This really opens up the possibilities and shows the strength of CSS3!

Video

Here's a small video example, where the page is running on the latest version of Safari.

Looks pretty nifty, doesn't it? Now let's see how we can create something like this ourself!

HTML

As usual, we're using HTML as the backbone for this effect. It will not contain any strange stuff, since all the "magic" will happen in CSS.

 
<ul id="movieposters">
   <li>
      <img src="images/01_iron_man_2.jpg" alt="Iron Man 2" />
      <div class="movieinfo">
         <h3>Iron Man 2</h3>
         <p>With the world now aware of his dual life as the armored superhero Iron Man, billionaire inventor Tony...</p>
         <a href="#" title="Iron Man 2">More info</a>
      </div>
   </li>
   <!-- More movie posters here -->
</ul>

That's all we need to create the nifty effect! Feel free to add more movie posters (and info) to the list, and you're ready to go.

CSS

Here's the CSS I came up with. Make sure you read the comments that I added, so you'll understand what the code does.

 
/* Basic list things. */
#movieposters
   { list-style:none; margin:100px 0; height:550px; }
 
/* List items. Take note of the -perspective property. We assign the transform-style to
* make the info element tranform correctly with the parent. transition is used for animation
*/
#movieposters li
   { display:inline; float:left;
   -webkit-perspective: 500; -webkit-transform-style: preserve-3d;
   -webkit-transition-property: perspective; -webkit-transition-duration: 0.5s; }
   
/* When hovering, we change the perspective. Since this property is defined
* in the transition, it'll animate occordingly.
*/
#movieposters li:hover
   { -webkit-perspective: 5000; }
 
/* We use the tranform property to enhance the 3d effect, making it rotated.
* We also add some shadows here, just to add even more depth.
* Also, for the image, we capture the rotateY event to be animated (transition)
* /
#movieposters li img
   { border:10px solid #fcfafa; -webkit-transform: rotateY(30deg);
   -moz-box-shadow:0 3px 10px #888; -webkit-box-shadow:0 3px 10px #888;
   -webkit-transition-property: transform; -webkit-transition-duration: 0.5s; }
 
/* Rotate back when hovering the parent element */
#movieposters li:hover img
   { -webkit-transform: rotateY(0deg); }
 
/* For the movie info box, we do almost the same as for the image. Still, we use
* some extra css (like the position, margin etc.) to place it on the correct place.
* Take note of the translateZ and rotateY properties to enhance the 3d effect.
*/
.movieinfo { border:10px solid #fcfafa; padding:20px; width:200px; height:180px; background-color:#deddcd; margin:-195px 0 0 55px; position:absolute;
   -moz-box-shadow:0 20px 40px #888; -webkit-box-shadow:0 20px 40px #888;
   -webkit-transform: translateZ(30px) rotateY(30deg);
   -webkit-transition-property: transform, box-shadow, margin; -webkit-transition-duration: 0.5s; }
 
/* Animate everything to their 2d state when hovering the parent. */
#movieposters li:hover .movieinfo
   { -webkit-transform: rotateY(0deg); -webkit-box-shadow:0 5px 10px #888; margin:-175px 0 0 30px; }
 
/* Some basic CSS for the movie info */
.movieinfo h3 { color:#7a3f3a; font-variant: small-caps; font-family:Georgia,serif,Times; text-align:center; padding-bottom:15px; }
.movieinfo p { padding-bottom:15px; }
.movieinfo a { background-color:#7a3f3a; padding:5px 10px; color:#eee; text-decoration:none; display:block; width:80px; text-align:center; margin:0 auto;
   -moz-border-radius:5px; -webkit-border-radius:5px; }
.movieinfo a:hover, .movieinfo a:focus { background-color:#6a191f; color:#fff; }

That's all the code we need to create this beautiful effect!

Oddities

Although this effect looks pretty great already, I found some minor bugs/oddities:

  • Sometimes, when you hover the poster, it doesn't trigger the :hover event, preventing the animation to occur.
  • For some reason, when animating the last element (the Tron Legacy Poster), the info box is quickly flickering just before the animation ends.

If you know why these oddities occur (is it my CSS or a real bug in -webkit), feel free to share your knowledge.

Conclusion and Download

Other than those rather small oddities, I'm pretty happy with the final result. Although the one Malarkey created might be a little bit smoother, mine looks like it could be used in your next web project too (if you're have clients that work on CSS3 ready browser).

Demo 3d animation using pure CSS3   Download 3d animation using pure CSS3

What do you think? Where could you see this kind of CSS in action? Do you want to see more of these demos? Feel free to share!


Tags:  3d css3 animation transition perspective

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!StumbleUpon!
Comments
Add NewSearchRSS
Ben - Excellent   2010-04-26 17:50:05
Gravatar image Brilliant mate! Amazing what can be done with coding!
Anonymous   2010-04-26 19:06:43
Gravatar image doesn't work in Firefox
Marco - I know   2010-04-26 19:35:43
Gravatar image As stated in the beginning of the article:
Quote:

As you could have expect, this technique will only work on -webkit based browsers (Latest version of Safari and probably Chrome too).
Michael Hobson - Chrome   2010-04-26 20:26:22
Gravatar image Doesn't work in Chrome v-4.1.249
The images just look squished, not 3D like in the YouTube video. Still looks great in the video though, and can't wait until things like this are fully supported!
Marco - Yup!   2010-04-26 20:37:26
Gravatar image I thought it was Chrome on the Mac that just caused it to not work - I'm having the same results. The animation does work, but the "3d" effect doesn't display.

Chrome and Safari are both -webkit based browsers and therefor I thought it would work there too (maybe on Windows).

But still strange, apparently Chrome and Safari don't use the same -webkit engine to parse the CSS. Thanks for noticing!
Nate - Works for me :)   2012-02-02 23:08:18
Gravatar image On PC, Chrome 16.0.912.77 m it works niiiiice, however...

The info box hides behind the image once the rotation is complete (full frontal).

Also, the flicker bug you mentioned happens in my Safari 5.0 on the first AND the last pictures.
Deoxys - Chrome / Safari   2010-04-26 20:59:33
Gravatar image Doesn't work on Chrome (5.0.342 Beta / Mac) or Safari (4.0.5 / Mac).... pretty poor support then...
Marco - Strange!   2010-04-26 21:45:19
Gravatar image Whoah, that's pretty strange. It's not working properly on Chrome here too (see previous comment), but Safari 4.0.5 on a Mac seems to be working fine over here.

I wonder what the differences could be...?
Hidayat Sagita - Webkit   2010-04-27 05:06:37
Gravatar image Hi Marco,
Great tuts as always :)
You can use Webkit Nightly Build (http://nightly.webkit.org/), it already support CSS3 3d Transform.
colin - no go   2010-04-26 22:08:35
Gravatar image it's not working in chrome or safari on my win 7 setup.
colin - Squished not 3d   2010-04-26 22:18:13
Gravatar image Doesn't work for me on the latest version of Safari 4.0.5 nor on Chrome (Windows XP). Just appears squished.
Jireck - Chrome & FF   2010-04-27 00:19:13
Gravatar image Hi Marco,

First : Thanks for this tutos
Second : On Chrome (old version) and Firefox (last), the result is not like video... (css3 and compatibility)

But, yes but :

the design is almost correct...

Great marco ... Great !! I'm Fan
Anonymous   2010-04-27 03:46:06
Gravatar image Not so fresh on iPad as hover is difficult sans mouse. :s
Anonymous - re:   2010-04-27 05:05:22
Gravatar image
Anonymous wrote:
Not so fresh on iPad as hover is difficult sans mouse. :s


Thats a great point!! wonder if you could build in the motion sensor of the ipad to somehow work with this effect???
Marco - Works on iPhone   2010-04-28 08:48:47
Gravatar image When I view the demo page with my iPhone and "press" on one of the posters, the effect is still visible (although not so smooth as on Safari).
Ted Thompson - Great Article   2010-04-27 13:34:10
Gravatar image Great article, thanks for posting. Ted
Chris Pierre - Front End Ninja   2010-04-29 17:39:12
Gravatar image Really Great CSS Effect, I just can't wait this will be applicable in all browser :)
Smashing Share - Cool effect!   2010-05-03 08:56:49
Gravatar image Really cool effect. Like it!
PsdDude   2010-05-05 11:58:38
Gravatar image Really interesting ! I must read this carefully :0 good article
palm tree plant - cool article   2010-05-07 07:42:56
Gravatar image That is one of the most amaz­ing things I have seen in a while.
Linda - Nice   2010-05-07 19:10:35
Gravatar image Nice work. I enjoyed the article. Thanks for video.
Adam   2010-05-14 17:49:41
Gravatar image The reason for the flicker on the TRON poster is that you don't have a tanslateZ on the images. You can add translateZ(0px) to the images -webkit-transform property and that's taken care of. As for the triggering events, I think it's because when you hover over the posters the element actually changes shape rather than just changing perspective, causing the corner you passed when moving to a hover on to quickly move out of the hover, confusing the rendering engine about whether or not you're actually hovering or not. If you're seeing a shudder, that's the reason. As for a fix, I'm not sure there really is one for strictly CSS.
9swords   2010-05-16 20:20:43
Gravatar image Super awesome! :idea:
Domo   2010-05-19 07:02:54
Gravatar image Hi Marco, is it Mac-only? Cos it doesn't work neither in Safari nor in Chrome on Windows.
Steve - Very Nice   2010-05-23 10:33:09
Gravatar image Whoah, that's pretty strange. It's not working properly on Chrome here too (see previous comment), but Safari 4.0.5 on a Mac seems to be working fine over here.
Sivaranjan - This is incredibly useful , but a little tweaking   2010-05-26 07:02:43
Gravatar image This is incredibly useful , but a little tweaking here and there could give a much nicer effect.
lily - nice   2010-05-27 22:19:42
Gravatar image looks great in safari :D
Geek Rider   2010-06-17 17:50:20
Gravatar image awesome tutorial!

first I tried it both on Safari (4.0) and Google Chrome(5.0) on Windows Vista but couldn't see that "3D" effect until today when I upgraded the Safari to 5.0 and it worked like a charm!

thanx for such a great tutorial.
Dave   2010-06-18 11:58:37
Gravatar image Hi Marco,

Nice work - if you set the perspective on the ul rather than the li then you'll get a result that's more like Andy's example
xyz   2010-11-02 15:30:19
Gravatar image Works great in Safari. Even with the latest Chrome, still looks wired (animation without the 3D effect). Looking forward to more browser support.
ONEBOYS - Amazing effect   2010-11-07 09:23:23
Gravatar image I think we can forge this '3d effec' widthout "-transform-style: preserve-3d"
Brett Widmann - Very Helpful   2010-11-08 00:57:24
Gravatar image Thanks for posting this. It has been very helpful. 3D can add some pretty sweet effects to design and now I know how to do it with CSS.
Grafics - Wonderful   2010-11-09 21:40:32
Gravatar image It's very beautiful.
It's a pity that Firefox and IE does not accept... :evil:
Derek - OMG   2010-12-02 03:09:23
Gravatar image OMG, it's so cool!
And luckly, Google Chrome supports it!!! :lol:
Marcel   2011-01-22 11:17:30
Gravatar image I'm on Windows XP SP 3. It works great on Safari 5.0.3 but the 3D does not work on Chrome 8.0.552.237
James - Copycat   2011-01-26 15:09:32
Gravatar image Not sure why you just copied Andy Clarke's example to pass off as you own.
You just changed the images.
I think you should have thought up an alternative use of this technique instead of just repeating something that is already out there.
J. Hendrix - Future   2011-03-23 08:38:51
Gravatar image Wow, great to see what the future brings with these new CSS3 techniques. However, it's far to early to imlement if you don't have a decent fallback.
e11world - Web Design & Development   2011-04-13 17:53:15
Gravatar image Just thought I'd let you know that it is not working on Firefox 3.6.1
mohammed   2011-07-11 18:57:15
Gravatar image hot and dangerous...
hirmani80 - WebHostingPad Reviews   2011-08-02 07:12:48
Gravatar image Thanks a lot for providing the great info in this blog and using the nice info in this blog. This info is very much interesting by providing in this blog. I am very happy for using the nice info in this blog.
hirmani80 - Web Hosting Pad   2011-08-02 07:14:42
Gravatar image Wonderful info by providing the nice info in this blog and the great technology is visible in this blog. I am really very happy for using the great technology is visible in this blog. Thanks a lot for visiting the nice info.
PhotoshopWarrior - Great   2011-10-21 10:57:52
Gravatar image Thanks for the CSS tips
FreeFull   2012-02-07 21:05:51
Gravatar image Firefox now supports 3D transforms, just replace -webkit- with -moz-
ljswan   2012-04-11 15:57:27
Gravatar image Everything works as expected in FF when replacing -webkit- with -moz-. The only issue I have found is the perspective property in FF. You must define a unit of measure for -moz-perspective which is not included in the demo. You will need to add 'px' to the perspective values. example...

-webkit-perspective: 5ooo;
-moz-perspective: 5000px;
Thomas - Not working in Chrome 17   2012-03-28 14:49:52
Gravatar image Text is hidden behind image after animation. Chrome 17
Azrael - Bug Fix   2012-04-04 22:23:22
Gravatar image to avoid the image to get hide behind after animation change the order in the html put the movieinfo object before the image and then at the css .movieinfo margin:290px 0 0 55px; and #movieposters li:hover .movieinfo margin:320px 0 0 30px; but this only fix for Chrome 17 if you use this in 18 you will have the same issues info will hide behind of image. :idea:
Ani   2012-04-14 17:27:49
Gravatar image nice use of css3
Twipas   2012-04-20 09:28:19
Gravatar image Thanks for this great information that you are sharing with us!!!
Anonymous   2012-08-18 11:17:54
Gravatar image its awesome!!!thought there was a bit of jquery until I downloaded the source code...amazing stuff with CSS...but I wonder if opera renders -o-perspective..thank you
Jason - Bring To Front   2012-08-24 22:50:52
Gravatar image I love this effect, and I want to use, but I have a question for you:

How do I cause the element that is being hovered over to come to the front? I've tried setting the z-index, but it doesn't seem to do anything.

In other words, if you hover over the center element, I'd really like for it to grow (which I already have it doing), but also to be "in front of" the other elements, or partially cover them (think of pulling it toward you, in the sense of perspective (real life perspective, not the CSS property)).

Any chance you could tell me how to do this?
bitraptor   2013-01-03 19:34:39
Gravatar image It works on the blogger blog I'm bulding up from scratch, but when I try to change the background color in the css file the 3d effect goes away. It doesn't make any sense. Is there any workaround for that? By the way I'm using Chrome and it works. it works partially on the lastest version of Firefox.
Arsal   2013-04-17 12:34:31
Gravatar image I Have Changed The Bg, By The Way On Chrome It Is Good.... FF Is Partially Working AsYou Mention
gero pellicer - It is a pity   2013-02-21 19:00:17
Gravatar image It is a pity that it doesnt work in IE 9... I will try it on 10...
Great use of CSS! congratulations, the effect is really wonderful
wahyu kodar - WOW   2013-02-25 23:12:14
Gravatar image Nice coding and sharing, i will use that in my web :cheer:
wahyu kodar - WOW   2013-02-25 23:13:07
Gravatar image Nice coding and sharing, i will use that in my web :cheer:
Arsal - Hunh??   2013-04-17 12:32:25
Gravatar image It Is Working Fine On Chrome Updated, But Not Working On Morzilla Firefox??? Any Fix For It Is Present???
Ajay   2013-05-15 13:02:36
Gravatar image Just replace -webkit with -moz in the css document and enjoy the 3D effect.
Ajay   2013-05-15 13:01:39
Gravatar image You can replace webkit with -moz in the css file and can enjoy the same 3D effect in firefox also. :cheer:
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Security Image
Please input the anti-spam code that you can read in the image.
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio