An Introduction To CSS Keyframes Animation
By now you’ve probably heard at least something about animation in CSS3 using keyframe-based syntax. The CSS3 animations module in the specification has been around for a couple of years now, and it has the potential to become a big part of Web design.
Using CSS keyframe animations, developers can create smooth, maintainable animations that perform relatively well and that don’t require reams of scripting. It’s just another way that CSS3 is helping to solve a real-world problem in an elegant manner. If you haven’t yet started learning the syntax for CSS animations, here’s your chance to prepare for when this part of the CSS3 spec moves past the working draft stage.
In this article, we’ll cover all the important parts of the syntax, and we’ll fill you in on browser support so that you’ll know when to start using it.
A Simple Animated Landscape Scene
For the purpose of this article, I’ve created a simple animated landscape scene to introduce the various aspects of the syntax. You can view the demo page to get an idea of what I’ll be describing. The page includes a sidebar that displays the CSS code used for the various elements (sun, moon, sky, ground and cloud). Have a quick look, and then follow along as I describe the different parts of the CSS3 animations module.
(NOTE: Versions of Safari prior to 5.1 have a bug that prevents the animation from finishing correctly. See more under the heading “The Animation’s Fill Mode”.)
I’ll describe the CSS related to only one of the elements: the animated sun. That should suffice to give you a good understanding of keyframe-based animations. For the other elements in the demo, you can examine the code on the demo page using the tabs.
The @keyframes At-Rule
The first unusual thing you’ll notice about any CSS3 animation code is the keyframes
@ rule. According to the spec, this specialized CSS @ rule is followed by an identifier (chosen by the developer) that is referred to in another part of the CSS.
The @ rule and its identifier are then followed by a number of rule sets (i.e. style rules with declaration blocks, as in normal CSS code). This chunk of rule sets is delimited by curly braces, which nest the rule sets inside the @ rule, much as you would find with other @ rules.
Here’s the @ rule we’ll be using:
@keyframes sunrise {
/* rule sets go here … */
}
The word sunrise
is an identifier of our choosing that we’ll use to refer to this animation.
Notice that I’m using not using any vendor prefixes for all of the code examples here and in the demo. I’ll discuss browser support at the end of this article, but for now just realize that currently no browser supports this standard syntax, so to get the code working, you have to include all the vendor prefixes.
The Keyframe Selectors
Let’s add some rule sets inside the @ rule:
@keyframes sunrise {
0% {
bottom: 0;
left: 340px;
background: #f00;
}
33% {
bottom: 340px;
left: 340px;
background: #ffd630;
}
66% {
bottom: 340px;
left: 40px;
background: #ffd630;
}
100% {
bottom: 0;
left: 40px;
background: #f00;
}
}
With the addition of those new rule sets, we’ve introduced the keyframe selector. In the code example above, the keyframe selectors are 0%
, 33%
, 66%
, and 100%
. The 0%
and 100%
selectors could be replaced by the keywords “from” and “to,” respectively, and you would get the same results.
Each of the four rule sets in this example represents a different snapshot of the animated element, with the styles defining the element’s appearance at that point in the animation. The points that are not defined (for example, from 34% to 65%) comprise the transitional period between the defined styles.
Although the spec is still in development, some rules have been defined that user agents should follow. For example, the order of the keyframes doesn’t really matter. The keyframes will play in the order specified by the percentage values, and not necessarily the order in which they appear. Thus, if you place the “to” keyframe before the “from” keyframe, the animation would still play the same way. Also, if a “to” or “from” (or its percentage-based equivalent) is not declared, the browser will automatically construct it. So, the rule sets inside the @ rule are not governed by the cascade that you find in customary CSS rule sets.
The Keyframes That Animate the Sun
For the purpose of animating the sun in this demo, I’ve set four keyframes. As mentioned, the code above includes comments that describe the changes.
In the first keyframe, the sun is red (as if it were just rising or setting), and it is positioned below ground (i.e. not visible). Naturally, the element itself must be positioned relatively or absolutely in order for the left
and bottom
values to have any effect. I’ve also used z-index to stack the elements (to make sure, for example, that the ground is above the sun). Take note that the only styles shown in the keyframes are the styles that are animated. The other styles (such as z-index
and position
, which aren’t animated) are declared elsewhere in the style sheet and thus aren’t shown here.
0% {
bottom: 0; /* sun at bottom */
left: 340px; /* sun at right */
background: #f00; /* sun is red */
}
About one third of the way into the animation (33%), the sun is on the same horizontal plane but has risen and changed to a yellow-orange (to represent full daylight):
33% {
bottom: 340px; /* sun rises */
left: 340px;
background: #ffd630; /* changes color */
}
Then, at about two thirds into the animation (66%), the sun has moved to the left about 300 pixels but stays on the same vertical plane. Notice something else in the 66% keyframe: I’ve repeated the same color from the 33% keyframe, to keep the sun from changing back to red too early.
66% {
bottom: 340px;
left: 40px; /* sun moves left across sky */
background: #ffd630; /* maintains its color */
}
Finally, the sun gradually animates to its final state (the full red) as it disappears below the ground.
100% {
bottom: 0; /* sun sets */
left: 40px;
background: #f00; /* back to red */
}
Associating The Animation Name With An Element
Here’s the next chunk of code we’ll add in our example. It associates the animation name (in this case, the word sunrise
) with a specific element in our HTML:
#sun.animate {
animation-name: sunrise;
}
Here we’re introducing the animation-name
property. The value of this property must match an identifier in an existing @keyframes
rule, otherwise no animation will occur. In some circumstances, you can use JavaScript to set its value to none
(the only keyword that has been reserved for this property) to prevent an animation from occurring.
The object we’ve targeted is an element with an id of sun
and a class of animate
. The reason I’ve doubled up the id and class like this is so that I can use scripting to add the class name animate
. In the demo, I’ve started the page statically; then, with the click of a button, all of the elements with a particular class name will have another class appended called animate
. This will trigger all of the animations at the same time and will allow the animation to be controlled by the user.
Of course, that’s just one way to do it. As is the case with anything in CSS or JavaScript, there are other ways to accomplish the same thing.
The Animation’s Duration And Timing Function
Let’s add two more lines to our CSS:
#sun.animate {
animation-name: sunrise;
animation-duration: 10s;
animation-timing-function: ease;
}
You can specify the duration of the animation using the animation-duration
property. The duration represents the time taken to complete a single iteration of the animation. You can express this value in seconds (for example, 4s
), milliseconds (2000ms
), and seconds in decimal notation (3.3s
).
The specification doesn’t seem to specify all of the available units of time measurement. However, it seems unlikely that anyone would need anything longer than seconds; and even then, you could express duration in minutes, hours or days simply by calculating those units into seconds or milliseconds.
The animation-timing-function
property, when declared for the entire animation, allows you to define how an animation progresses over a single iteration of the animation. The values for animation-timing-function
are ease
, linear
, ease-out
, step-start
and many more, as outlined in the spec.
For our example, I’ve chosen ease
, which is the default. So in this case, we can leave out the property and the animation will look the same.
Additionally, you can apply a specific timing function to each keyframe, like this:
@keyframes sunrise {
0% {
background: #f00;
left: 340px;
bottom: 0;
animation-timing-function: ease;
}
33% {
bottom: 340px;
left: 340px;
background: #ffd630;
animation-timing-function: linear;
}
66% {
left: 40px;
bottom: 340px;
background: #ffd630;
animation-timing-function: steps(4);
}
100% {
bottom: 0;
left: 40px;
background: #f00;
animation-timing-function: linear;
}
}
A separate timing function defines each of the keyframes above. One of them is the steps
value, which jerks the animation forward a predetermined number of steps. The final keyframe (100%
or to
) also has its own timing function, but because it is for the final state of a forward-playing animation, the timing function applies only if the animation is played in reverse.
In our example, we won’t define a specific timing function for each keyframe, but this should suffice to show that it is possible.
The Animation’s Iteration Count And Direction
Let’s now add two more lines to our CSS:
#sun.animate {
animation-name: sunrise;
animation-duration: 10s;
animation-timing-function: ease;
animation-iteration-count: 1;
animation-direction: normal;
}
This introduces two more properties: one that tells the animation how many times to play, and one that tells the browser whether or not to alternate the sequence of the frames on multiple iterations.
The animation-iteration-count
property is set to 1
, meaning that the animation will play only once. This property accepts an integer value or infinite
.
In addition, the animation-direction
property is set to normal
(the default), which means that the animation will play in the same direction (from start to finish) each time it runs. In our example, the animation is set to run only once, so the property is unnecessary. The other value we could specify here is alternate
, which makes the animation play in reverse on every other iteration. Naturally, for the alternate
value to take effect, the iteration count needs to have a value of 2
or higher.
The Animation’s Delay And Play State
Let’s add another two lines of code:
#sun.animate {
animation-name: sunrise;
animation-duration: 10s;
animation-timing-function: ease;
animation-iteration-count: 1;
animation-direction: normal;
animation-delay: 5s;
animation-play-state: running;
}
First, we introduce the animation-delay
property, which does exactly what you would think: it allows you to delay the animation by a set amount of time. Interestingly, this property can have a negative value, which moves the starting point partway through the animation according to negative value.
The animation-play-state
property, which might be removed from the spec, accepts one of two possible values: running
and paused
. This value has limited practical use. The default is running
, and the value paused
simply makes the animation start off in a paused state, until it is manually played. You can’t specify a paused
state in the CSS for an individual keyframe; the real benefit of this property becomes apparent when you use JavaScript to change it in response to user input or something else.
The Animation’s Fill Mode
We’ll add one more line to our code, the property to define the “fill mode”:
#sun.animate {
animation-name: sunrise;
animation-duration: 10s;
animation-timing-function: ease;
animation-iteration-count: 1;
animation-direction: normal;
animation-delay: 5s;
animation-play-state: running;
animation-fill-mode: forwards;
}
The animation-fill-mode
property allows you to define the styles of the animated element before and/or after the animation executes. A value of backwards
causes the styles in the first keyframe to be applied before the animation runs. A value of forwards
causes the styles in the last keyframe to be applied after the animation runs. A value of both
does both.
UPDATE: The animation-fill-mode
property is not in the latest draft of the spec, but it is found in the editors draft. Also, certain versions of Safari (5.0 and older) will only apply a value of “forwards” if there are exactly two keyframes defined. These browsers always seems to use the 2nd keyframe as the “forwards” state, which is not how other browsers do it; the correct behaviour uses the final keyframe. This is fixed in Safari 5.1.
Shorthand
Finally, the specification describes shorthand notation for animations, which combines six of the properties described above. This includes everything except animation-play-state
and animation-fill-mode
.
Some Notes On The Demo Page And Browser Support
As mentioned, the code in this article is for animating only a single element in the demo: the sun. To see the full code, visit the demo page. You can view all of the source together or use the tabs in the sidebar to view the code for individual elements in the animation.
The demo does not use any images, and the animation does not rely on JavaScript. The sun, moon and cloud are all created using CSS3’s border-radius
, and the only scripting on the page is for the tabs on the right and for the button that lets users start and reset the animation.
Here are the browsers that support CSS3 keyframe animations:
- Chrome 2+,
- Safari 4+,
- Firefox 5+,
- IE10 PP3,
- iOS Safari 3.2+,
- Android 2.1+.
Although no official announcement has been made, support in Opera is expected.
If you code your animations using a single vendor-based syntax, you can use a tool like Prefixr or Animation Fill Code to automatically fill in the extra code for you.
Further Reading On CSS Keyframes
- “CSS Animations Module Level 3,” W3C
- “CSS3 Animations,” Robert Nyman
- “CSS3 Animator,” Anthony Calzadilla A website “dedicated solely to CSS3/JavaScript animation and how to realistically use it in your projects today.”
- “Adding Delight to Your Design with CSS3 and Webkit Keyframe Animation,” Jason Garber
- “Using CSS3 Transitions, Transforms and Animation” (with examples), Rich Bradshaw
- “Creating Awesome CSS3 Animations,” by Thomas Fuchs
Further Reading
- The State Of Animation 2014
- The Guide To CSS Animation: Principles and Examples
- Let’s Play With Hardware-Accelerated CSS
- Upgrading CSS Animation With Motion Curves