CSS Fundamentals: CSS 3 Transitions

CSS Fundamentals: CSS 3 Transitions

This entry is part 10 of 16 in the CSS3 Mastery Session
« PreviousNext »

As CSS3 rolls out around the web, it is bringing some interesting new presentational techniques along with it. Today, we’ll review the basics of using CSS3 transitions and animations to add an extra layer of polish.


Step 1 – Link Transitions

To begin, we’ll work with some basic techniques – firstly a simple change of text color when a user hovers over a specified element.

 
a { color:#000; } 
a:hover { color:#f00; }

Here, we change the text color to red on hover. Now, with a little CSS3, we can create a much smoother look by gradually fading the color.

a{ 
   color:#000; 
   -webkit-transition:color 1s ease-in;
} 
a:hover{color:#f00;}

We’ve added the css property, -webkit-transition. Note that the -webkit prefix specifies that this will only work in Webkit engines, or Safari and Chrome. Luckily, other modern browsers have their own implementations as well. A full statement covering all browsers might look similar to the following:

a { 
   color:#000; 
   -webkit-transition:color 1s ease-in;
   -moz-transition:color 1s ease-in;
   -o-transition:color 1s ease-in;
   transition:color 1s ease-in;
} 

Please note that, for this tutorial, we’ll be focusing exclusively on the webkit implementation. After the declaration of the property, we have the values “color 1s ease-in.” This is the shorthand declaration; the three values represent:

  1. the property to be transitioned
  2. the duration of the transition
  3. the type of transition

In this case, we transition using ease-in, which will begin the transition slowly, and speed up into the transition.


Step 2 – Background Transitions

Another basic use of changing states is to change the background of an input box on focus.

input.ourInputBox:focus{
 -webkit-transition:background-color 0.5s linear; 
 background:#CCC; 
}

This time, we put the transition declaration into the hover state, so that we aren’t adding additional unnecessary classes to the CSS. We apply the transition once the input box acquires focus.


Step 3 – Transitioning Multiple Properties

CSS transitions are actually relatively straight forward to add to existing hover functionality, and give your
site that extra polish for browsers that support CSS3.

To take things a step further, we can also transition multiple CSS properties using the longhand versions of the CSS3 transition.

a.thebg {
 color:#000; 
 background:#f00;  
 padding:5px; 
 -webkit-border-radius: 5px; 

 -webkit-transition-property:color, background; 
 -webkit-transition-duration: 1s, 1s; 
 -webkit-transition-timing-function: linear, ease-in;
} 

a.thebg:hover {
 color:#f00;
 background:#000;
}

This time, the background and text color changes on hover, so we can target both of these properties with our transition.
We simply split the transition: first we have -webkit-transition-property and separate the different values with a comma. So we target the color and background properties, respectively.

-webkit-transition-property:color, background;

Then we set the duration of the transition using:

-webkit-transition-duration:1s, 1s; 

These are referenced in the same order as the first statement; this time, however, both values are set to 1s.

Lastly, we set the timing function, and set two different values: the first, linear, relates to our first declared variable – color; and the second relates to the variable background.

So, we’ve set the color to a linear change over one second, and the background to ease-in over that same time period.


Step 4 – Putting the Pieces Together

CSS3 transitions start to come into their own when they’re combined with other new CSS properties.

You can review examples of using overlapping elements and transitions on Andy Clarke’s For a Beautiful Web.

Let’s create a simple example of animating a pop out sign.

We first create the bounding box for the signpost, and give it a relative positioning context to ensure that we can
position items absolutely within it.

#signpost{
 width:60px;
 height:196px;
 position:relative;
}

Now we place the box on the page and put the pieces of our sign within it.

<div id="signpost">
	<img id="post" src="post.png">
	<img id="sign" src="sign.png">
</div>

Next, the post is positioned with a z-index of two, to place it on top of the sign.

#post{
 width:79px;
 height:196px;
 z-index:2;
 position:relative;
}

Now, we add in the sign, positioned underneath the post, and rotate it with the CSS3 transform property.

#sign{
 height:46px;
 width:80px;
 position:absolute;
 top:10;
 left:60;
 -webkit-transform-origin:0 0;
 -webkit-transform: rotate(86deg);
 z-index:1;
 -webkit-transition:-webkit-transform 0.5s ease-in-out;
}

The sign is rotated using -webkit-transform: rotate(86deg), and is positioned under the post. To ensure that the sign rotates around the proper point, we must change the transform origin to the top left corner: 0, 0.

We set the transition to change the -webkit-transform property over 0.5s with an ease-in-out profile, and on hover, we rotate the sign back to its original orientation.

#signpost:hover #sign{
	-webkit-transform:rotate(0deg);
}

We do this on the containing #signpost:hover rather than on the hover of the #sign itself.


Step 5 – Introducing Animations

We can now tie all of this together, using webkit animations, which give us the power to carry out things such as continuous rotation.

We begin by creating two circle images, and positioning them inside a containing div – as we did with the signpost above.

<div id="circles">
	<img src="outer.png" width="180" height="180" class="outercircle"/>
	<img src="middle.png" width="90" height="90" class="middlecircle"/>
</div>
#circles{
	width:180px; 
	height:180px;
	position:relative;
}
.outercircle{
	width:180px; 
	height:180px;
	position:absolute;
	z-index:1;
	top:0: 
	left:0;
}
.middlecircle{
	width:90px; 
	height:90px;
	position:absolute;
	z-index:3;
	top:45px; 
	left:45px;
}

Now we need to define the animations; we’ll spin the circles in opposite directions, so we need to set up two animations.

@-webkit-keyframes spin {
from {
	-webkit-transform: rotate(0deg);
}
to {
	-webkit-transform: rotate(360deg);
	}
}

@-webkit-keyframes spinrev {
from {
	-webkit-transform: rotate(0deg);
	}
to {
	-webkit-transform: rotate(-360deg);
	}
}

The animation is given a name for reference, in this case “spin” and “spinrev.” Then, we assign them a to and from value; so we rotate the image from
0 deg to 360 deg using webkit transform, and to -360 for the reverse.

Now we assign this animation to the hover states. Note that, if we assigned it to the normal state, the animation would run immediately when the page is loaded.

#circles:hover .outercircle {
	-webkit-animation-name: spin;
	-webkit-animation-iteration-count: infinite;
	-webkit-animation-timing-function: linear;
	-webkit-animation-duration: 10s;
}	

#circles:hover .middlecircle{
	-webkit-animation-name: spinrev;
	-webkit-animation-iteration-count: infinite;
	-webkit-animation-timing-function: linear;
	-webkit-animation-duration: 5s;
}

We reference the animation name we created earlier (-webkit-animation-name: spin;). Then, we declare the number of times we want the animation to run (-webkit-animation-iteration-count: infinite;).
In this case, infinite will keep it going round and round whilst the #circles div is hovered upon.

We next set the timing function (-webkit-animation-timing-function: linear;), and, finally, we set a duration for each iteration – in this case, it will be ten seconds (-webkit-animation-duration: 10s;), and five for a complete revolution.


Step 6 – Graceful Degredation with Modenizr

Once we have everything working, we should consider our users who are browsing without CSS3 capable web browsers. This is easily accomplished using a JavaScript library such as Modernizr, which adds classes to the HTML element relating to the browser capabilities.

Using the sign post example above, browsers that don’t support CSS transforms will not place the sign under the post correctly; so we can target these browsers and simply hide the sign until it is hovered over.

.no-csstransforms #signpost #sign{
	display:none;
}
.no-csstransforms #signpost:hover #sign{
	display:block;
}

It’s as simple as linking to the Modernizr script, finding the appropriate class name, and then creating a separate CSS statement for that instance.


Conclusion

That’s all for now. I hope you enjoyed it! Let me know if you have any questions/comments below!

Tags: CSScss3
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Dave Henderson

    This is terrible. You’re encouraging bad web practices by only using the -webkit- prefix. I’d request that you change the article accordingly.

    • http://cinitriqs.deviantart.com CiNiTriQs

      Well… It was a tutorial written in 2010, even though that doesn’t seem such a long time ago, css3 was barely implemented by any browser. Still, only using webkit is indeed not a progressively enhanced practice. But, therefor we can stat in a comment (like u can find below) the different other prefixes that should be used future wise. So to update the best practices for these particular features I’ll include the other prefixes here: (I’ll take “transition” as the basis for all)

      example:
      -webkit-transition:background-color 0.5s linear;

      should become:
      -webkit-transition:background-color 0.5s linear; /* Safari and Chrome */
      -moz-transition:background-color 0.5s linear; /* Firefox 4 */
      -o-transition:background-color 0.5s linear; /* Opera */
      transition:background-color 0.5s linear; /* Hopefully all future browsers */

      and Iexplorer? same old story: always late ;) for that we will have to fall back to good old/new jQuery or any other scripting language that can handle the dom properly.

    • The Dude

      This is a great article for 2010, for people that don’t know coding you can just take any line of code

      Use http://prefixr.com/

      ex:
      INPUT

      -webkit-animation-name: spin;
      -webkit-animation-iteration-count: infinite;
      -webkit-animation-timing-function: linear;
      -webkit-animation-duration: 10s;

      OUTPUT

      -webkit-animation-name: spin;
      -moz-animation-name: spin;
      -ms-animation-name: spin;
      -o-animation-name: spin;
      animation-name: spin;

      -webkit-animation-iteration-count: infinite;
      -moz-animation-iteration-count: infinite;
      -ms-animation-iteration-count: infinite;
      -o-animation-iteration-count: infinite;
      animation-iteration-count: infinite;

      -webkit-animation-timing-function: linear;
      -moz-animation-timing-function: linear;
      -ms-animation-timing-function: linear;
      -o-animation-timing-function: linear;
      animation-timing-function: linear;

      -webkit-animation-duration: 10s;

      Enjoy :D

  • http://www.uuiuii.com gfhfh

    @-moz-keyframes roll {
    from { -moz-transform: rotate(0deg) }
    to { -moz-transform: rotate(360deg) }
    }
    body {
    -moz-animation-name: roll;
    -moz-animation-duration: 9s;
    -moz-animation-iteration-count: 10;
    }

    • Makc

      =D

  • http://colorcowstudios.tk Karthik

    awesome tutorial :)
    Thanks a lot.

  • http://Abidkhanweb.wordpress.com Abid Khan

    Nice tutorials..

  • rakesh kumar

    Demo page does not working as per the tutorial. So that i am not able to guess what will be the actual effect of all these css techniques

    • Manoj G K

      Did u check in chrome or safari . Cos this example will work only on webkit browsers