Home

How to Use CSS3 Transition Animation

|
Updated:  
2016-03-26 13:16:11
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

It's already possible to change CSS properties on the fly through pseudo-classes (like hover) or with JavaScript code. Prior to CSS3, all CSS state changes happened instantly. With the new transition attribute, you can cause transitions to happen over time.

image0.jpg

Look at a simple h1 heading:

<h1>Transition Demo</h1>

The CSS code is mainly quite straightforward:

 h1 { color: black font-size: 300%; transition:color 1s ease-in; }
 h1:hover { color: red; }

Begin by ignoring the transition attribute. If you look at the rest of the code, it's easy to see what it does. In the normal state, the heading is black. In the hover state, the color is red. Typically, the heading turns red as soon as the mouse hovers over it, and will instantly turn black when the mouse leaves.

However, when the transition attribute is added, the color change is not immediate, but takes a second. The color gradually changes from black to red and back.

Transitions are even more interesting when you pair them with transformations. Imagine a very simple div:

<div id = "box">Box 1</div>

Apply a little CSS3 magic and when the user hovers over the div, it rotates smoothly until it is upside-down. When the user leaves the div, it smoothly rotates back to its original position:

 #box { transition: all 1s ease-in; height: 100px; width: 100px; border: 1px solid black; }
 #box:hover { transform: rotate(180deg); }

The transform is defined in the: hover pseudo-class. The only new element is the transition specified in the class’ standard style.

The transition attribute takes several parameters:

  • animation property: The type of animation defined by this tag. The default value is all, but other types are expected to work, including color, length, width, pecentage, opacity, and number. If in doubt, use the standard all.

  • duration: The length of the animation in seconds. One second is 1s.

  • timing function: If you want the animation to occur at a constant speed, use . If you want a more natural motion that gradually speeds up and slows down at the ends of the animation, use one of the following: ease, ease-in, ease-out, ease-in-out.

  • delay: If you include a second time value, this will be considered a delay. The animation will not begin until after the delay.

If you prefer, you can use individual properties for the various parts of the animation, but most developer prefer the one-line shortcut (like the one used for borders).

Not all CSS attributes can be animated, but many can be. It may require some experimentation to determine which CSS attributes can be animated with the transition attribute.

Unfortunately, the stock transition attribute is not supported by any major browsers, but there are vendor-specific versions for Mozilla (-moz-), webKit (-webkit), and Opera (-o-). Your best bet until support is widespread is to include all vendor-specific versions in addition to the standard version.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].