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!Technorati!StumbleUpon!Newsvine!Furl!Ma.gnolia!
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!
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
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

Get updates from this website along with 5407 others.