How to use CSS3 transitions

Everyone loves motion. Adding the fourth dimension (time) to a site design is the main way screen-based design stands out from print design. CSS Transitions are a simple method for animating properties of an element enabling you to enrich certain events in your web design, without the need of Flash or JavaScript.

The W3C aptly describes transitions on their website as “CSS Transitions allows property changes in CSS values to occur smoothly over a specified duration”. In other words, CSS transitions allow us to modify a property incrementally, creating a sensation of motion and imbuing designs with subtlety and emotion that isn’t possible with a quick change.

 

Browser support

All modern browsers (yes even IE!) now support CSS transitions. However, importantly, if transitions aren’t supported in the browser being used, the transition is ignored and the property changes will be applied instantly. This graceful degradation is a cornerstone of best practice.

In order to extend the range of browser support we can use vendor prefixes, this extends the feature to include Firefox 4–15, Opera 10.5–12 and most versions of Chrome and Safari. The code, including the vendor prefix alternatives looks like this:

div {
  -o-transition: all 1s ease;
  -moz-transition: all 1s ease;
  -webkit-transition: all 1s ease;
  transition: all 1s ease;
}

The non-prefixed property is added at the bottom of the stack to ensure final implemantation will trump all the others, as the property moves from draft to finished status.

 

Transition parameters

There are four parameters for CSS transitions:

  • transition-property: the property to be tweened, or the keyword ‘all’ to apply to all properties;
  • transition-duration: the duration of the transition;
  • transition-timing-function: the easing to be applied, this creates more natural looking movement
  • transtion-delay: specifies a delay to the start of the transition.

In addition, you can also use the shorthand property (as in the example above) where the parameters are specified as property, duration, timing function, delay.

 

Properties that can be transitioned

You can only transition properties that can be translated into a mathematical value. So for example, you can transition font-size; you cannot transition font-face.

The full list of properties that can currently be transitioned is as follows: 

background-position, border-bottom-color, border-bottom-width, border-left-color, border-left-width, border-right-color, border-right-width, border-spacing, border-top-color, border-top-width, bottom, clip, color, font-size, font-weight, height, left, letter-spacing, margin-bottom, margin-left, margin-right, margin-top, max-height, max-width, min-height, min-width, opacity, outline-color, outline-width, padding-bottom, padding-left, padding-right, padding-top, right, text-indent, text-shadow, top, vertical-align, width, word-spacing, z-index.

 

Using transitions

Transition declarations are attached to the normal state of the element. Therefore only declared once for several states such as :focus, :active and pseudo classes.

Using the following code we can set up a basic menu and a paragraph of text. It is styled so that the menu items change their color and background color when rolled over, and so that the inline link changes its underline color from white to green when hovered over:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8" />
 <title>CSS transition demo</title>
 
 <style type='text/css'>
 
 /*
 
 STATIC PROPERTIES
 
 */
 body {background:#fff; }
 ul { padding:0; margin:20px; }
 li { display:inline; padding:5px 0; }
 .nav a { padding:5px; }
 p { padding:0; margin:21px; }
 a { text-decoration:none; }
 
 
 /*
 
 PROPERTIES TO TWEEN
 
 */
 
 .nav a
 {
 color:#bee0bf;
 background:#1a9e5c;
 }
 
 .nav a:hover
 {
 color:#fff;
 background:#38b476;
 }
 
 p a
 {
 color:#3fa32d;
 border-bottom:1px solid #fff;
 }
 
 p a:hover
 {
 color:#bee0bf;
 border-bottom:1px solid #3fa32d;
 }
 
 
 </style>
</head>
<body>

<ul class='nav'>
 <li><a href='#'>Business</a></li>
 <li><a href='#'>Code</a></li>
 <li><a href='#'>Design</a></li>
 <li><a href='#'>Inspiration</a></li>
 <li><a href='#'>Miscellaneous</a></li>
 <li><a href='#'>Mobile</a></li>
 <li><a href='#'>News</a></li>
 <li><a href='#'>Resources</a></li>
 <li><a href='#'>Usability</a></li>
</ul>

<p>Integer posuere erat a ante <a href='#'>inline link</a> venenatis dapibus posuere velit aliquet. Nullam id dolor id nibh ultricies vehicula ut id elit. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Nullam quis risus eget urna mollis ornare vel eu leo.</p>

</body>
</html>

Here’s how it looks:

We want to tween those properties, to make them run smoothly. We want to tween both the color, and the background-color of the nav elements, but for the inline text, we only want to tween the underline. We do this by adding the following to our style definition:

/*
 
 CSS TRANSITIONS 
 */
 
 .nav a
 {
 -o-transition: all 500ms;
 -moz-transition: all 500ms;
 -webkit-transition: all 500ms;
 transition: all 500ms;
 }
 
 p a
 {
 -o-transition: border-bottom 250ms;
 -moz-transition: border-bottom 250ms;
 -webkit-transition: border-bottom 250ms;
 transition: border-bottom 250ms;
 }

And here’s the result:

Do you use CSS3 Transitions? What effects have you been able to create? Let us know in the comments.

Featured image/thumbnail, motion image via Shutterstock.

  • http://www.websitetemplates.org/ Stacy Summers

    strong post! thanks

  • Ryan Pergola

    I’ve used transitions in site design for various project, always with the caveat to clients that they won’t always work in older browsers. With that said, has anyone been able to transition three button states? So far, my attempts at a down state have failed.

    • http://www.creativesomething.net/ tannerc

      :active selector works beautifully, even with animations. http://www.w3schools.com/cssref/sel_active.asp

    • http://www.facebook.com/emosewamai Andrew Hersh

      Has :active not worked for you? I haven’t personally found the need for it, but I don’t see why it wouldn’t work for links.

  • http://www.facebook.com/emosewamai Andrew Hersh

    I’ve been having problems implementing :hover on touch devices. I’ve got some ideas to get it working correctly using entirely CSS but it’s a mind-bender and I really don’t have the time.

  • Jonath Lee

    Nice post, normally I would create a class name > .animate-slow-ease and add this class to anywhere I want it to animate. Pretty flexible.

  • bu

    webkit special: fading background-images (http://css3.bradshawenterprises.com/cfimg/#cfimg6)

    • http://www.blackbookoperations.com/ Black Book Operations

      Great link, thanks!

  • mystik

    Nice article! But how did you
    implement the UI shown on the image above? and thanks for the information on CSS3 transition it is really helpful for me as i am working on a project.http://www.mystikcreation.com/

  • http://twitter.com/UxPiper Piper Lawson

    Exactly which IEs are compatible with this?

  • Shawn Lowe

    Works great with Sprites too!

    • http://www.blackbookoperations.com/ Black Book Operations

      depending on what you want to do with those sprites of course. (if it are standard gifs or jpgs, we wont be able to change bg colours, transparant gifs n png’s aside of course) but yes, many transitions work on all kinds of elements, great to see all that in action. (without any javascript or animated gifs/flash/…)

  • http://www.facebook.com/pavel.antolik Pavel TwoNine Antolík

    Short but great article :)

  • http://www.jtechcommunications.com/ JTech

    Thanks for the post! I have been interested in expanding the functionally of my transitions and this is a great jumping off point.

  • http://twitter.com/RaptoraUK Raptora

    Great article :) We have been using some great CSS3 transitions on the re-designing of our website, would love to get feedback on some of the nifty trasitions we have used: http://www.raptora.co.uk

  • http://jimihendrixwallpaper.com/ Mike

    Nice article. A nice subtle effect. If you want to see a more fun approach look here http://designertuts.com/css3-animations.php

  • http://epinterestfollowers.com/ Tahmid Ahmed

    Any body please explain me: I tried only writing {transition: all 500ms;}. And it work. So why should I write-

    -o-transition: all 500ms;
    -moz-transition: all 500ms;
    -webkit-transition: all 500ms;

    Thanks.

    • Chris

      You need to add the prefixes to enable cross-browser support. -o-transition is for Opera 10.50-12.00, -webkit-transition for Chrome 1-25 and Safari 3.2+, -moz-transition for Firefox 4-15. Note that if you are using lots of CSS3 features you might consider using Prefix free (http://leaverou.github.io/prefixfree/)

      • http://epinterestfollowers.com/ Tahmid Ahmed

        Thanks.

  • Chris

    Nice article, if you would like further information I created a tutorial on CSS3 tutorials on my blog at http://www.lingulo.com/tutorials/css/css3-transitions

  • Mario rocchi

    These kind of post are the one I love!

  • http://www.webiberis.com/ Ashok Prajapati