In CSS, you have two techniques for visualizing change that are competing for your attention: Animations & Transitions. In this article, let’s examine the similarities and differences between them so that you can know when to use which.
To make the most of this article, it would be helpful for you to be familiar with using both animations and transitions. If you haven’t had a chance to get your hands dirty with them, the Creating a Simple CSS Animation and Looking at CSS3 Transitions tutorials will help you get started.
From a distance, both animations and transitions are very 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.
Beyond this point, though, you will see that animations and transitions diverge a bit and let their uniqueness shine through. Let’s look at those unique qualities in greater detail.
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:
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 theanimation-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:
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:
Upon hover, I specify a different value for both background-color as well aswidth. 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 widthproperties, I will need to explicitly add another transition entry for width:
What About transition: all?
You do not have to specify every property that you care about when using a transition. You could simplify your life by using the all value instead:transition: all .5s ease-in. I do not recommend this because you will take a performance hit. Your browser is now listening for a whole lotta properties as opposed to just a few that you know you will be modifiying. Unless you have a strong need to do this, I would recommend specifying each CSS property you wish to transition individually.
With animations, you have the ability to specify properties at a whim in each keyframe without having to do anything that resembles declaring them:
In this example, the height and background-color of whatever element I am animating will smoothly transition when the appropriate keyframe is reached – even if the property was never listed before!
In many cases, a transition or animation you declare in CSS will be good enough. You specify in CSS your starting value, the ending value, and any intermediate values that you want your properties to take. Your animation or transition will read these values and take care of business from there. This scenario works best when what you are wanting to do is predefined. There will be times when you want to alter the value of a property that you are animating based on some external input – a mouse click, the result of some calculation, etc.
Contrasting the predefined path of an animation is the transition. Transitions are not as well defined as they may seem. Your transition will kick in when a property it is listening for changes. This property change can be set entirely via only CSS:
To help visualize what I just wrote, let’s look at a simple example:
Click anywhere in the gray box to have the circle move to the point of the click. Keep clicking around to see the circle keep moving.