There are a number of ways of achieving animations with CSS3:
If you’re not looking too closely both transitions and animations look similar. They both allow you to:
- Specify which CSS properties to listen for changes on
- Set timing (easing) functions to alter the rate of going from a one property value to another
- Specify a duration to control how long the animation or transition will take
- Programmatically listen to animation and transition-specific events that you can then do with as you wish
- Visualize CSS property changes.
Once you understand the differences it becomes easier to decide which technique to use as each excels at different things.
One of the major differences between animations and transitions can be seen in how you trigger them to start playing.
A transition only plays as a reaction to a CSS property that has changed. A common scenario is one where you use the :hover pseudo class to change the value of a CSS property:
example of hovering to make something larger
Animations, on the other hand, don’t require any explicit triggering. Once you define the animation, it will start playing automatically. Yep, that’s how animations roll!
This is pretty simple. Animations can be easily made to loop by setting the animation-iteration-count property. You can specify a fixed number of times you want your animation to repeat:
If you just want your animation to loop forever, you can do that as well:
Transitions, on the other hand, don’t have a property that specifies how many times they can run. When triggered, a transition runs only once. You can make a transition loop by fiddling with the transitionEnd event, but that isn’t particularly straightforward – especially when compared with animations.
Defining Intermediate Points / Keyframes
With an animation, you have the ability to define keyframes which give you more control over your CSS property values beyond just the start and the end:
You can set as many keyframes as you want, and when your animation plays, each keyframe will be hit with the specified property changes reflected. This allows you to create the kinds of involved animations that help HTML5 compete with more established animation technologies like Flash more effectively.
With a transition, you don’t have much control over anything beyond the end result:
transitions are linear
A transition simply goes from an initial state to the final state. You cannot specify any points in-between like you can with an animation, so a transition might not be a good choice if you are trying to create the next Teen Girl Squad sensation or a complex animation.
Specifying Properties Up-Front
The next thing I will describe is how formal animations and transitions are when it comes to defining a transition between CSS property values.
On the formal side, you have transitions. Every CSS property you want recognized by your transition must be explicitly represented.
For example, let’s say you have something like the following:
transition:background-color .5s ease-in;
Upon hover, I specify a different value for both background-color as well as width. My transition specifies only background-color though. This means your browser is only listening for changes on the background-color property.
If I wanted my transition to affect both the background-color and width properties, I will need to explicitly add another transition entry for width:
transition:background-color .5s ease-in, width .5s ease-in
Here’s some examples we went over: