Css Animations
Css Animations
AND TRANSITIONS
for the Modern Web
STEVEN BRADLEY
PEACHPIT PRESS
x GETTIN G STARTED
CHAPTER 4
ANIMATIONS
CSS transitions offer you a way to create simple animations that always start
as the result of triggering a CSS property change. Transitions can animate only
between a start and end state, and each state is controlled by existing CSS prop-
erty values. For example, a transition that runs on hover transitions between
values on the element and values on the hover state of the element. Overall,
transitions are a simple way to animate but offer little control over the animation.
CSS animations provide a bit more control. They allow for the creation of mul-
tiple keyframes (Figure 4.1) over which the animation occurs. While they
can start in reaction to a change in CSS property value, they can also run on
their own. An animation executes as soon as the animation property is
applied.
94 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
Figure 4.1
Animation keyframes
Transitions don’t change property values; they define how the change occurs.
Animations can change property values inside each keyframe.
Transitions change implicitly. You define things at the start and end states, and
you leave it to the browser to determine all the intermediate states. Animations
change explicitly. The animation can define start and end states as well as some
intermediate states. The browser still determines the intermediate states between
keyframes, but the animation gets to define as many keyframes as it wants.
All the things you could change when working with transitions, you can still
change when working with animations. You determine how long the anima-
tion lasts and what timing-function to use between keyframes. You also
get to delay the animation if you like.
In addition, you can decide how many times the animation should run and
in which direction it should run. You can set the animation to be running or
paused. You can even determine which CSS property values apply outside the
time frame in which the animation runs.
Animations have other benefits over transitions as you’ll see in this chapter.
In general, these benefits are about giving you more control. Transitions have
advantages over CSS animations, too. In general, they’re about the simplicity
of transitions.
CHAPTER 4 ANIMATIONS 95
Browser Support
Browser support for CSS animations is good. It’s similar to what you saw ear-
lier for transforms and transitions. CSS animations work in all modern brows-
ers. In IE10 and newer, Firefox, and IE Mobile, no vendor prefixes are needed.
Safari, Chrome, Opera, iOS Safari, Android Browser, and Blackberry Browser
all use the -webkit vendor prefix, so you have only the one prefix to deal with.
The animation-fill-mode property isn’t supported in Android below version
2.3. In iOS 6.1 and earlier, animations aren’t supported on pseudo-elements.
As you probably expect by this point, the holdouts are Opera Mini and IE9
and earlier. Unfortunately, there’s no polyfill like there was for transforms
and transitions. The fallback is to create the animation using JavaScript: You
first check to detect CSS animation support and then use one of the available
JavaScript libraries for working with animation.
JavaScript animation is beyond the scope of this book, but the following sec-
tion gives you to a few places where you can find more information.
You could create animations for every browser using JavaScript and ignore
CSS animations completely. If you’re using JavaScript to create the animation
for some browsers, why not use JavaScript for all browsers and not worry so
much about CSS animation support? CSS animations are usually, though not
always, more performant than the same animation in JavaScript.
Another option, and the one I recommend, is to treat CSS animations as part
of the noncritical experience. Use animations to enhance the design and the
design’s aesthetic, but make sure nothing breaks in browsers that don’t support
CSS animations. Your site should still work in any browser that doesn’t support
animations, but it can provide a more enjoyable experience for those that can.
Note that while CSS animations work in modern browsers, you don’t neces-
sarily see the same smoothness. A smooth-running animation in one browser
might look a bit jerky in another, and it’s not always the same browsers looking
smooth or not. It depends on the browser and the specifics of the animation.
CSS Animations
As we’ve been doing throughout this book, let’s start with an example.
CSS Positioning
You’ll make a box slide across the screen from left to right in two ways. The
first way will be to use CSS positioning (Example 4.1).
<div class="box"></div>
2. Give the .box div dimensions and a background color so you can see it
on the page. Set its position to absolute. Top and left values will be 0
by default, which is fine for this example.
.box {
width: 200px;
height: 200px;
background-color: #393;
position: absolute;
}
CHAPTER 4 ANIMATIONS 97
You need two components to create the animation. The first one declares
the animation on .box. Part of the benefit of the animation property is
the name of a keyframe where you’ll change properties, so you also need
to create this keyframe, which is the second component.
.box {
-webkit-animation: slide 5s linear 0s 3;
The first value in the list is slide, which is the name of your keyframe.
@-webkit-keyframes slide {
from {
left:0
}
to {
left: 600px
}
@keyframes slide {
from {
left: 0;
}
to {
left: 600px;
}
}
98 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
Figure 4.2
Slide animation using the
left property
from { to {
left: 0; left: 600px;
} }
The animation itself probably wasn’t very smooth, but you’ll get to that in a
moment. Let’s talk about what the code is doing, starting with the keyframe.
The keyframe has the name slide. It includes two declarations for the left
property, once in a from state and once in a to state. In the from state, the
left value is 0, and in the to state, the value is 600px. The states from and
to represent the start and end states, so initially the .box is positioned 0 pix-
els from the left edge, and at the end of the animation cycle, it is 600 pixels
from the left edge.
To start the animation, you set the animation shorthand property on the .box div.
The animation is calling the keyframe named slide, and it runs for a dura-
tion of 5 seconds. The timing-function is linear. There’s no delay, and the
animation is set to run three times.
1. Replace the keyframe in step 4 of Example 4.1 with the following keyframe:
@-webkit-keyframes slide {
to {
@keyframes slide {
to {
In this code, the translate function moves the .box div 600 pixels to
the right, the same as the left values did in the previous @keyframes
rule. Notice that only the to state is included this time. You don’t need to
include a from state. You really didn’t need it the first time either. The ini-
tial state of the .box div as set on the .box class is exactly what you want
for the from state, so there isn’t a need to explicitly set it in the keyframe.
The same thing happens as before: A green .box moves 600 pixels to the
right three times (Figure 4.3). However, this time the animation runs
smoother. We’ll get to why at the end of the chapter. For now just know
there are multiple ways to create an animation (or a transition), but the
performance of each way can vary.
100 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
Figure 4.3
Slide animation using
translate function
from { to {
} -webkit-transform: translate(600px, 0px);
-ms-transform: translate(600px, 0px);
transform: translate(600px, 0px);
}
As you can see in the example, animations can reset CSS property values inside
their keyframes. Transitions can’t do this. Although CSS animations affect
property values while running, they don’t by default control values before the
animation starts or after it ends. By default, the intrinsic styles (styles added
directly to the element and not inside keyframes) of the element control the
values outside the time the animation is running. The styles set in the keyframe
are in control while the animation is running, but not necessarily before or
after. You do have a measure of control to change the default.
It’s possible to have multiple animations running at the same time and for each
animation to set different values on the same property. When this happens,
the animation defined last in the list of keyframe names overrides the other
animations, and the value it sets is used.
Animations can start in one of two ways:
◆ On page load
◆ In reaction to a CSS property change
The start time of an animation is the latter of the time when the style speci-
fying the animation changes (changing the element on hover for example) or
the time the document’s load event is fired—in other words, automatically
after the page has loaded.
CHAPTER 4 ANIMATIONS 101
he @Keyframes Rule
Keyframes are the different states of the element being animated. They’re used
to specify different values for the properties being animated at various points
during the animation. A series of keyframes defines the behavior for one cycle
through the animation. Remember animations can repeat multiple times.
You define keyframes inside the @keyframes rule.
@keyframes identifier {
List of properties and values
Inside each @keyframes rule is a list of percent values or the keywords to and
from. The keyword from is equivalent to 0%, and the keyword to is equivalent
to 100%. When using a percent, the % sign needs to be included. 0 and 100
are invalid values; 0% and 100% are the correct values.
@Keyframes slide {
0% {
left: 0;
20% {
left: 100px;
40% {
left: 200px;
}
102 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
60% {
left: 300px;
80% {
left: 400px;
100% {
left: 500px;
@Keyframes slide {
from {
left: 0;
20% {
left: 100px;
40% {
left: 200px;
60% {
left: 300px;
}
CHAPTER 4 ANIMATIONS 103
80% {
left: 400px;
to {
left: 500px;
Each keyframe selector specifies the percentage of the animation’s duration Note
that the specific keyframe represents. The keyframe state is specified by the I’m using the words
group of properties and values declared on the selector. “keyframe” and
“keyframes” in ways that
If you don’t set a keyframe at 0% (or from), then the browser constructs a 0% might be confusing.
state using the intrinsic values of the properties being animated. Similarly if Each percentage value
no 100% (or to) keyframe is set, the browser constructs the state from intrin- represents a new
keyframe or state with
sic values. Negative percent values or values greater than 100% are ignored.
its own CSS property
Keyframes containing properties that aren’t animatable or contain invalid values. Together the
properties are ignored. properties and values in
each keyframe make up
@keyframes rules don’t cascade. A single animation will never use keyframes a keyframe declaration
from more than one @keyframes rule. When multiple @keyframes have been block. he @keyframes
specified on the animation-name property, the last one in the list (ordered by rule is the special
@ rule that contains all
time) with a matching @keyframes rule controls the animation. the different keyframes
(states) that an animation
It’s valid for an @keyframes rule to be empty, and because of this it can be
runs through.
used to hide keyframes previously defined. The empty @keyframes rule should
come later in your CSS to override any @keyframes rule with the same iden-
tifier that appears earlier in your CSS.
1. Add the following after the @keyframes rules you set in Example 4.1.
@-webkit-keyframes slide {
@keyframes slide {
}
104 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
2. Reload your webpage. The animation should no longer run, since an empty
@keyframes rule is called.
animation-* Properties
CSS animations offer eight different properties for controlling an animation.
Some are comparable to similarly named transition-* properties, and some
will be new.
animation-name Property
The animation-name property defines a comma-separated list of animations
to apply to the given selector. It’s similar to the transition-property in that
it ultimately defines the properties that are animated. With the transition-
property, those properties are explicitly named. With the animation-name,
an @keyframes rule is explicitly named, and that rule contains the properties
that will be animated.
@-webkit-keyframes slide {
properties: values;
@keyframes slide {
properties: values;
}
CHAPTER 4 ANIMATIONS 105
@-webkit-keyframes drop {
properties: values;
@keyframes drop {
properties: values;
-webkit-animation-name: none;
animation-name: none;
Every listed animation-name should have a corresponding value for any oth-
er animation-* properties. If there are too many values in an animation-*
property, any leftover values are ignored. If there aren’t enough values, the list
of values will be repeated until there are enough to match.
animation-duration Property
The animation-duration property defines how long an animation lasts
during one cycle of the animation. It’s similar to the transition-duration
property and takes a time value in seconds (s) or milliseconds (ms).
-webkit-animation-duration: 10s;
animation-duration: 10s;
Note that animation-duration is the length of one full cycle of the animation.
It’s not the length of each keyframe in the @keyframes rule. For example, if you
set an animation-duration of 10s and have the following @keyframes rule
@Keyframes duration {
0% {
property: value;
50% {
property: value;
}
CHAPTER 4 ANIMATIONS 107
100% {
property: value;
the animation will take 10 seconds to get from 0 percent to 100 percent, and
not 10 seconds to go from 0 percent to 50 percent and then 10 seconds more
from 50 percent to 100 percent.
animation-timing-function Property
The animation-timing-function property describes an acceleration curve
for each keyframe in a single animation cycle. It’s similar to the transition-
timing-function. You can use any of the keyword timing functions or cre-
ate one of your own.
animation-timing-function: step-start;
animation-timing-function: step-end;
animation-timing-function: steps();
animation-timing-function: ease;
animation-timing-function: linear;
animation-timing-function: ease-in;
animation-timing-function: ease-out;
animation-timing-function: ease-in-out;
animation-timing-function: cubic-bezier();
<div class="box"></div>
.box {
width: 200px;
height: 200px;
background-color: #393;
}
-webkit-animation-name: slide;
animation-name: slide;
-webkit-animation-duration: 5s;
animation-duration: 5s;
-webkit-animation-timing-function: ease-in;
animation-timing-function: ease-in;
@-webkit-keyframes slide {
0% {
}
CHAPTER 4 ANIMATIONS 109
25% {
50% {
75% {
100% {
@keyframes slide {
0% {
-webkit-transform: translate(0px, 0px);
25% {
-webkit-transform: translate(150px, 0px);
-ms-transform: translate(150px, 0px);
50% {
75% {
100% {
This code adds five keyframes to the @keyframes rule. This should make
it easier to see that the ease-in timing function is running between each
keyframe and not once over the entire animation cycle.
CHAPTER 4 ANIMATIONS 111
5. Load your page in a browser, and observe the timing curve between key-
frames (Figure 4.4).
Figure 4.4
Animation timing functions
You can override the timing function inside each of the keyframes. When a
timing function is applied inside a keyframe, it’s instructing the animation
to use that function moving from the keyframe with the timing function
applied to the next one (Example 4.4).
6. Replace your @keyframes slide rule from Example 4.3 with the follow-
ing rule. Changes in the code are highlighted.
@-webkit-keyframes slide {
0% {
25% {
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
50% {
75% {
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
100% {
@keyframes slide {
0% {
}
CHAPTER 4 ANIMATIONS 113
25% {
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
50% {
75% {
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
100% {
In this code, you override the ease-in timing function on two of the
keyframes.
114 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
7. Reload your page, and observe the difference in the acceleration curve
between keyframes (Figure 4.5).
Figure 4.5
Animation timing functions
on keyframes
The way timing functions work over keyframes and the ability to override them
on a specific keyframe is powerful and perhaps a bit scary. You have great con-
trol over how your animation accelerates, but you also have the responsibility
to exercise that control. Having an animation ease in between every keyframe
is probably not what you want.
animation-iteration-count Property
Transitions run once when triggered and run once in reverse when the trigger
is removed. Animations can run as many times as you want. The animation-
iteration-count property defines how many times an animation runs, and
it takes as a value any number or the keyword infinite. The latter sets your
animation to run in an endless loop.
-webkit-animation-iteration-count: 3;
animation-iteration-count: 3;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
CHAPTER 4 ANIMATIONS 115
<div class="box"></div>
width: 0px;
height: 0px;
border-width: 100px;
border-style: solid;;
-webkit-animation-name: rotate;
animation-name: rotate;
-webkit-animation-duration: 4s;
animation-duration: 4s;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
116 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
-webkit-animation-iteration-count: 3;
animation-iteration-count: 3;
@-webkit-keyframes rotate {
0% {
-webkit-transform: rotate(0deg);
-ms-transform: rotate(0deg);
transform: rotate(0deg);
25% {
-webkit-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
50% {
-webkit-transform: rotate(180deg);
-ms-transform: rotate(180deg);
transform: rotate(180deg);
75% {
-webkit-transform: rotate(270deg);
-ms-transform: rotate(270deg);
transform: rotate(270deg);
}
CHAPTER 4 ANIMATIONS 117
100% {
-webkit-transform: rotate(360deg);
-ms-transform: rotate(360deg);
transform: rotate(360deg);
@keyframes rotate {
0% {
-webkit-transform: rotate(0deg);
-ms-transform: rotate(0deg);
transform: rotate(0deg);
25% {
-webkit-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
50% {
-webkit-transform: rotate(180deg);
-ms-transform: rotate(180deg);
transform: rotate(180deg);
}
118 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
75% {
-webkit-transform: rotate(270deg);
-ms-transform: rotate(270deg);
transform: rotate(270deg);
}
100% {
-webkit-transform: rotate(360deg);
-ms-transform: rotate(360deg);
transform: rotate(360deg);
If you followed the colors in the example for the borders, the green wedge
should start at the top. Each time the green wedge is back at the top is one
Figure 4.6 iteration or one animation cycle (Figure 4.6).
Animation iteration count
0% { 50% { 100% {
transform: rotate(0deg); transform: rotate(180deg); transform: rotate(360deg);
} } }
25% { 75% {
transform: rotate(90deg); transform: rotate(270deg);
} }
CHAPTER 4 ANIMATIONS 119
animation-direction Property
Another new property is the animation-direction property, which defines
whether an animation runs forward or in reverse on some or all of its cycles.
The animation-direction property takes one of four values:
◆ normal specifies that all iterations of the animation are played as specified.
◆ reverse specifies that all iterations of the animation are played in the
reverse direction as specified.
animation-direction: normal;
-webkit-animation-direction: alternate-reverse;
animation-direction: alternate-reverse;
When the animation plays in reverse, the timing functions also run in reverse—
for example, ease-in runs as ease-out.
Until now, the sliding box you’ve been working with slides to the right and
then instantly returns to its initial location. The jump is more than a little jar-
ring. The alternate and alternate-reverse values can remove the jump.
Instead, the box continues to slide right and left until the animation stops.
Let’s go back to the sliding .box div you’ve used through most of this chap-
ter (Example 4.6).
<div class="box"></div>
120 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
.box {
width: 200px;
height: 200px;
background-color: #393;
}
-webkit-animation-name: slide;
animation-name: slide;
-webkit-animation-duration: 5s;
animation-duration: 5s;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
-webkit-animation-iteration-count: 3;
animation-iteration-count: 3;
-webkit-animation-direction: reverse;
animation-direction: reverse;
@-webkit-keyframes slide {
to {
@keyframes slide {
to {
First it jumps 600 pixels to the right (so fast that you might not see the .box
on the left before the jump), and then it slides back to its initial location
and repeats the sequence three times.
6. Change the value for the animation-direction in step 3 to alternate
(Example 4.7).
-webkit-animation-direction: alternate;
animation-direction: alternate;
Now the .box div slides back and forth between the initial and ending
states. This makes for a much smoother overall animation. Experiment with
the normal and alternate-reverse values.
122 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
Figure 4.7
Animation direction
animation-play-state Property
By default, your animations run as soon as the animation-name property is
assigned. You can change that behavior with the animation-play-state
property, which defines whether an animation is running or paused.
-webkit-animation-play-state: running;
animation-play-state: running;
-webkit-animation-play-state: paused;
animation-play-state: paused;
The default value, as you would likely guess, is running. If you change the
value to paused, the animation stops where it is until the animation-play-
state is changed again to running. When paused, the animation displays
whatever state the animation was in at that moment. When the animation is
resumed, it restarts from the state it was paused in.
CHAPTER 4 ANIMATIONS 123
1. Add the animation-play-state property to the .box div from the pre-
vious example. Additions to the code are highlighted.
.box {
-webkit-animation-name: slide;
animation-name: slide;
-webkit-animation-duration: 5s;
animation-duration: 5s;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
-webkit-animation-iteration-count: 3;
animation-iteration-count: 3;
-webkit-animation-direction: alternate;
animation-direction: alternate;
-webkit-animation-play-state: paused;
animation-play-state: paused;
Unlike previous examples, this time the animation doesn’t run when the page
is finished loading. To run the animation, you need to change animation-
play-state to running and reload the page.
This isn’t particularly useful if you have to reload the page after changing
the animation-play-state property, but it becomes much more useful
when changing properties via JavaScript or some other trigger.
<div class="container">
<button id="play">Play</button>
<button id="pause">Pause</button>
</div>
The buttons get ids so your JavaScript code has something to hook into.
Notice that the code adds an id of box to the .box div.
button {
padding: 0.5em 1em;
border-radius: 5%;
margin-top: 3em;
Nothing special. Just a little style to make your buttons look “buttony.” Now
let’s add some JavaScript so the buttons do something.
5. Add the following code in the head of your document between <script>
</script> tags.
<script>
document.getElementById('play').addEventListener(
p 'click', function(){
box.style.webkitAnimationPlayState = "running";
box.style.animationPlayState = "running";
}, false);
document.getElementById('pause').addEventListener(
p 'click', function(){
box.style.webkitAnimationPlayState = "paused";
box.style.animationPlayState = "paused";
}, false);
};
</script>
Hopefully, the script looks somewhat familiar. The last line of code listens
for the page to load and then calls the init function.
Inside the function, you first get hooks to each button and the .box div and
set them to appropriately named variables. Next you add event listeners to each
button, and if a button is clicked, you set the value of animationPlayState
to either running or paused, depending on which button was clicked.
You should see the new play and pause buttons. The green box sits in the
top-left corner until you click the Play button to start the animation. Once
the box begins moving, you can click the Pause button to stop the anima-
tion. Clicking Play starts the animation again from the point at which it
was stopped.
animation-delay Property
The animation-delay property defines when an animation starts. It works
the same way the transition-delay property works. Like transition-
delay, values are in units of time and can be positive, 0, or negative.
126 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
-webkit-animation-delay: 2s;
animation-delay: 2s;
-webkit-animation-delay: 0s;
animation-delay: 0s;
-webkit-animation-delay: -2s;
animation-delay: -2s;
A positive value delays the animation until some point in the future. A value
of 0 (the default) starts the animation instantly. A negative value appears to
start the animation in the past. It starts instantly, but at a point in the middle
of the animation. The delay works as an offset.
Let’s continue to build on Example 4.8.
-webkit-animation-name: slide;
animation-name: slide;
-webkit-animation-duration: 5s;
animation-duration: 5s;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
-webkit-animation-iteration-count: 3;
animation-iteration-count: 3;
-webkit-animation-direction: alternate;
animation-direction: alternate;
CHAPTER 4 ANIMATIONS 127
-webkit-animation-play-state: running;
animation-play-state: running;
-webkit-animation-delay: 2s;
animation-delay: 2s;
The animation does nothing for 2 seconds and then slides back and forth like
before (Figure 4.8). Try using some negative values, and observe the
difference.
Figure 4.8
Animation delay
128 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
animation-fill-mode Property
You probably had an idea what each animation-* property did before I told
you. Some were familiar after working through transitions, and the property
names give a pretty good clue about what the others do.
By default, an animation affects property values only while it’s running. This
is why the example animations you’ve been working with often jump back to
the initial state when the animation stops. Whatever values are set in each key-
frame are the ones used for a property until either the next keyframe changes
it or the animation stops playing. When the animation stops, the CSS property
values are whatever values were set intrinsically on the element.
The animation-fill-mode property overrides this behavior. It takes four
keyword values.
◆ none is the default, and it doesn’t apply any property values in the anima-
tion outside the animation’s execution.
◆ backwards applies the property values defined in the first keyframe that
starts the first iteration to the period defined by animation-delay. The
values come from either the 0% (from) or 100% (to) keyframes, depend-
ing on the value of the animation-direction property.
◆ forwards applies the property values after the animation stops. If the
animation-iteration-count value is greater than 0, the values applied
are those at the end of the last completed iteration. If the count value equals
0, the values applied are those that start the first iteration.
◆ both does what you might expect and applies both the forwards and
backwards values to the animation-fill-mode property.
CHAPTER 4 ANIMATIONS 129
Once again let’s expand the example we’ve been working with.
-webkit-animation-name: slide;
animation-name: slide;
-webkit-animation-duration: 5s;
animation-duration: 5s;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
-webkit-animation-iteration-count: 1;
animation-iteration-count: 1;
-webkit-animation-direction: alternate;
animation-direction: alternate;
-webkit-animation-play-state: running;
animation-play-state: running;
-webkit-animation-delay: 2s;
animation-delay: 2s;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
130 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
As before, the green box slides to the right when the animation begins.
However, now when it stops it doesn’t jump back to its initial state. Setting
the animation-fill-mode to forwards allows the .box div to hold the
final state in the animation after the animation completes (Figure
4.9).
Figure 4.9
Animation fill mode
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
animation-fill-mode: backwards;
Or add a 0% (or from) keyframe in the @keyframes rule so the .box begins
the animation in a different location.
0% {
}
CHAPTER 4 ANIMATIONS 131
animation-fill-mode: backwards;
For both changes, observe where the .box div is located before and after the
animation as well as during the animation-delay.
If you replace each of the properties with a value, you get something like this: Note
Animation Events
You learned in the last chapter that when a transition completes, it generates
a DOM event. Animations also fire events. They fire an event at the start and
end of an animation as well as the end of each iteration.
132 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
For transitions the transitionend event offers three read-only values. Simi-
larly there are three read-only values you can access with animation events:
◆ animationName is the value of the animation-name property of the ani-
mation that fired the event.
◆ elapsedTime is the amount of time the animation has been running, in
seconds, when this transitionend event fires. This time excludes any time
the animation was paused. The elapsedTime for an animationstart
event is 0.0s.
◆ pseudoElement is the name (beginning with two colons) of the CSS pseudo-
element on which the animation runs or an empty string if the animation
runs directly on an element. At the moment only Firefox version 23 and
newer supports reading this value, but in practice I’ve yet to get it working.
Animations can have multiple properties animating at the same time. These
can be properties set on a single animation or on multiple animations. An
event is generated for each animation-name value and not for each individ-
ual property being animated.
Let’s try an example so we can experiment with these events and their associ-
ated values. You’ve been working with variations of the same example. Why
stop now? You’ve made the box slide to the right and most of the time back to
the left. Let’s add another @keyframes rule that generates a downward slide.
You’ll apply this new rule after the .box finishes the slide animation.
The animation will slide to the right, slide back to its initial position, and then
slide down and back up again (Example 4.11).
The class is used to style the div like you’ve been doing all along, and the
id is used to hook into the element via JavaScript. The class name and
id name don’t need to be the same. You just need to make sure to match
the names you give them in the appropriate place in the code.
<div class="box" id="box"></div>
2. Give the .box div dimensions and a background color so you can see it
on the page, and then add an animation using the animation shorthand.
.box {
width: 200px;
height: 200px;
background-color: #393;
Before creating the @keyframes rule, take a look at the shorthand in this
code, and make sense of what it’s doing. It’s calling an @keyframes rule
named slide. Each animation cycle runs a total of 2 seconds. The timing
is linear so there is no acceleration. There’s no delay, and the animation
completes two cycles. It runs once normally and then runs in reverse. Ani-
mation elements hold their state both before and after the animation runs.
134 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
3. Create the @keyframes rule using translation to move the element 600
pixels to the right.
@-webkit-keyframes slide {
100% {
@keyframes slide {
100% {
The familiar green .box slides to the right and then slides back left to its
starting point. This animation fires events. You’ll capture those events using
a little JavaScript. Don’t worry, it’s no more complicated than what you did
in the last chapter with transition events, and you’re free to copy the code.
What you’re going to do is listen for one of the animation events and when
it occurs, start a second animation. The .box is probably getting tired of
sliding across the page so a slidedown @keyframes rule seems in order.
CHAPTER 4 ANIMATIONS 135
to {
@keyframes slidedown {
to {
You can reload your page if you’d like, but I’ll save the suspense. Nothing
changes. You’ve created a @keyframe rule, but it’s not attached to any ele-
ment yet. That’s where the events and JavaScript come in.
<script>
box.addEventListener("webkitAnimationStart",
p updateAnimation , false);
box.addEventListener("oTAnimationStart",
p updateAnimation , false);
box.addEventListener("animationstart",
p updateAnimation , false);
box.style.webkitAnimationName = "slidedown";
box.style.animationName = "slidedown";
};
</script>
The JavaScript is a modified version of what you saw with transition events.
The last line of code, window.addEventListener('DOMContentLoaded',
init, false);, once again runs an init function after the page con-
tent loads.
In the init function, you first get the element with an id of box and assign
it to a variable named box. Next, you add an event listener (with and without
vendor prefixes) to the box to capture an animationstart event. When
the event is captured, it’s passed to an updateAnimation function. Finally
the updateAnimation function changes the animation-name value to
the slidedown animation created in step 5.
The second animation (slidedown) runs, but the first one (slide) doesn’t.
This happens because the JavaScript captures the event that fires at the start
of the animation and changes which animation is used before slide can run.
box.addEventListener("webkitAnimationIteration",
p updateAnimation , false);
box.addEventListener("oTAnimationIteration",
p updateAnimation , false);
box.addEventListener("animationiteration",
p updateAnimation , false);
box.style.webkitAnimationName = "slidedown";
box.style.animationName = "slidedown";
};
</script>
The slide animation starts and completes a single cycle before it jumps
back to its initial state and begins and completes both iterations of the
slidedown animation.
This time you listened for the event that fires at the end of each iteration
of an animation. The slide animation completes one iteration, the
animationiteration event is fired, and your code starts the slidedown
animation. The slidedown animation completes because the JavaScript
code runs only a single time. No code is listening for the events that the
slidedown animation fires in this example.
138 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
10. Change your JavaScript code to listen for animationend, and change
its vendor-prefixed variations (Example 4.13). Changes in the code are
highlighted.
<script>
box.addEventListener("webkitAnimationEnd",
p updateAnimation , false);
box.addEventListener("oTAnimationEnd",
p updateAnimation , false);
box.addEventListener("animationend",
p updateAnimation , false);
box.style.webkitAnimationName = "slidedown";
box.style.animationName = "slidedown";
};
</script>
This time both animations start and complete. First slide moves the .box
to the right before returning. As soon as it completes, an animationend
event is fired. Your JavaScript hears the event and starts the slidedown
animation, which also completes.
Figure 4.10
Animation events summary
Let’s do one more thing: read the read-only values. You can access and read
them at each of the three fired events.
12. Change your JavaScript code to listen for the animationstart event,
and set an alert to display the animationName that fired the event and
the elapsedTime it’s been running. Changes in the code are highlighted.
<script>
box.addEventListener("webkitAnimationStart",
p updateAnimation , false);
box.addEventListener("oAnimationStart",
p updateAnimation , false);
box.addEventListener("animationstart",
p updateAnimation , false);
140 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
};
</script>
As soon as the page loads, the slide animation runs, and an alert pops up
with the message, “The slide animation has been running for 0s.”
Figure 4.11 shows the alerts that display when listening for the
animation- iteration event.
Figure 4.11
Animation event read-only
values
Transition or Animation
One of the questions you might be asking yourself is when you should use a
transition and when you should use an animation. You can create most of the
examples in this and the previous chapter using either transitions or anima-
tions. So which should you choose when you want to animate something?
Similarities
You can start to answer that question by thinking about the similarities and dif-
ferences of transitions and animations. One thing they both have in common
is their properties. About half the animation-* properties have a counterpart
transition-* property. The timing functions, for example, are the same, except
for using the word animation or transition to start the property name.
Both can listen for changes to CSS property values and interact with JavaScript
events. Triggering events like those in the following list can make changes in
CSS property values that start either animations or transitions:
◆ :hover
◆ :link
◆ :active
◆ :visited
◆ :focus
◆ :checked
◆ :disabled
You can also start transitions and animations through changes in media que-
ries or class changes via simple JavaScript that changes the appropriate prop-
erty values.
142 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
Differences
Let’s switch gears and think about the differences. Although both transitions
and animations can run in response to a trigger, only animations can run auto-
matically on page load. Transitions require a trigger to run. If you need your
animation to run automatically, you have only one choice.
Transitions are limited to initial and final state keyframes. Animations can
build as many intermediate keyframes as necessary or desired. This gives you
more control over your animation and allows you to create more complex and
sophisticated animations. Transitions are for simple animations.
Transitions don’t change properties. You set values up front in the CSS intrin-
sic to the specific elements. Transitions define the change only between prop-
erty values and not the values themselves. Animations can change property
values inside each keyframe. The values don’t need to be declared outside the
animation either, making animation more dynamic.
Transitions can’t loop. They run once when triggered and then run in reverse
when the trigger is removed. Otherwise they don’t run. You can loop anima-
tions as many times as you want and set them to run in reverse or alternate
between forward and reverse. Once again CSS animations offer you more con-
trol than CSS transitions.
Once you start using JavaScript to further control your transitions and ani-
mations, it quickly becomes clear that transitions are easier to work with. It’s
more difficult making changes to the values inside keyframes than it is the
intrinsic values on elements.
As a general rule, you’ll write more code using CSS animations as opposed to
CSS transitions, assuming both are trying to do the same thing.
When you get down to it, animations are abstractions of transitions. States are
pulled out from the specific case to work in a more modular fashion. Tran-
sitions are a specific case of the more general animation. If you find yourself
using the same transition code over and over, you might decide to rewrite it
as an animation.
CHAPTER 4 ANIMATIONS 143
In general, choose CSS transitions for simple animation that require less con-
trol, but better integration with JavaScript. Choose CSS animations for more
complex and flexible animations that offer you greater control.
There’s one question in regard to transitions and animations you might still
be wondering about: Does one perform better than the other? To answer that
question, let’s look at performance.
Performance
The short answer is that you shouldn’t see any performance difference between
transitions and animations, assuming both are doing the same thing in the same
way. Performance has more to do with what properties are being changed as
opposed to whether those changes happen through transitions or animations.
To render webpages, a browser first calculates the CSS styles that apply to the
HTML elements. Then it lays out the page by working through the geometry
and position for each element. Next comes painting where pixels are filled in
before finally drawing everything to the screen on composite layers. Browsers
use two different execution threads to do all these things and render webpages:
the main thread and the compositor thread.
The main thread is responsible for laying out pages and painting elements. It
computes the CSS styles applied to HTML elements, and it runs JavaScript.
The compositor thread draws bitmaps to the screen via the graphic processing
unit (GPU). It determines which parts of the page are visible or will soon be
visible. It determines what’s likely to be scrolled to next and moves the parts
of the page when someone does scroll. Both threads communicate with each
other, sending and requesting information.
144 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
The main thread tends to be busier for longer periods of time, and while busy
it’s not responsive to input. The compositor thread, on the other hand, tries to
remain responsive because it has to be aware of scrolling.
Main thread responsibilities are more CPU intensive, while compositor respon-
sibilities look to the GPU more frequently. GPUs can draw the same bitmaps
over and over in different positions quickly. They can scale and rotate bitmaps
quickly as well.
To create more performant animations, you generally want to stay away from
layout and painting changes and instead make compositing changes. Com-
positing changes can be made on separate compositing layers, so the browser
doesn’t need to repaint or rework the layout of other layers.
TaBle 4.1 lists CSS properties that affect layout and painting.
It turns out that there are currently five things browsers can animate cheaply
in terms of performance: translation, scale, rotation, opacity, and some CSS
filters. The first three should have you thinking back to transforms. Anything
beyond animating these five types of properties probably won’t run as smooth.
It won’t matter whether you use CSS transitions or CSS animations when it
comes to performance, but you should think about performance when creat-
ing either. Transforms, opacity, and some CSS filters don’t require layout or
painting changes and so are preferred properties for animating.
This doesn’t mean you shouldn’t animate other properties. You can still cre-
ate a smooth animation with other properties. Just realize that if you have the
choice, you should opt for changing a transform, opacity, or CSS filter instead
of another property.
CHAPTER 4 ANIMATIONS 145
height border-style
padding border-radius
margin visibility
display background
border-width text-decoration
border background-size
top background-image
position background-position
font-size background-repeat
float outline-color
text-align outline
overflow-y outline-style
font-weight outline-width
overflow-y box-shadow
left
right
font-family
line-height
vertical-align
clear
white-space
bottom
min-height
146 CSS A NIMATIO NS AND TR A NSITIO NS FOR THE MODERN WEB
Summary
In time, you can expect browsers to make more CSS properties quicker to
animate. For now, do what you can with what you have. You have enough
control over both transitions and animations to animate many CSS proper-
ties smoothly and create more realistic-looking animations, which brings us
to the next chapter.
298 INDEX
Index
Symbols realistic, 147–148, 295
resources on, 294–296
* (asterisk), 47
restraint in, 197, 198, 209
@keyframes rule
running and pausing, 122–125
defining and using, 101–104
running forward or in reverse, 119–122
naming with animation-name property, 104–
secondary action in, 195–196
105
slow in and slow out, 182
opening and closing sidebar navigation with,
smoothing, 72, 98–100
249–254
solid drawing in, 206–207
squash and stretch, 155–157
squash and stretch in, 149–158
staging for, 164–167
A starting, 100, 106
absolute positioning, 284 straight-ahead and pose-to-pose actions for, 168
accelerating/decelerating transitions, 68–78 subtle transitions in, 5–6
ease curves, 74 timing, 196–197
ease-in curves, 76 transitions vs., 57, 94, 141–145, 217, 294
ease-out curves, 77 trends in, 211, 289
linear curves, 75 types of, 6–7
actions uses for, 211
adding anticipation to, 159 using multiple, 100
follow-through and overlapping, 168–181 animation-* properties, 104–131
overlapping and secondary action, 195–196 adding to off-canvas sidebar navigation, 248–249
staging, 165 animation-delay, 125–127
straight-ahead and pose-to-pose, 168 animation-direction property, 119–122
triggering transitions with, 58 animation-fill-mode, 95, 128–131
all keyword, 62, 63 animation-iteration-count property, 114–
alternating animation directions, 19 118
animation, 93–146. See also animation-* properties; animation-name, 104–106
twelve principles of animation animation-play-state, 122–125
about, 93–94 animation-timing-function, 107–114
additional principles of, 208–209 animation-duration, 106–107
anticipation in, 158–164 shorthand animation property, 131
appeal in, 207–208 animationend event, 132, 139
arcs in, 182–195 animationiteration event, 132, 139
CSS positioning creating, 96–98 animationstart event, 132, 139
defining with @keyframes rule, 101–104 anticipation, 158–164
delaying, 125–127 appeal, 207–208
Disney’s 12 principles of, 148–149, 295–296 arcs, 182–195
ending, 105
events in, 131–140 B
exaggeration in, 197–206
backface-visibility() property, 43–51
firing events, 131
background color transitions
follow-through and overlapping action in, 168–
delaying, 79–81
181
duration of, 66–68
importance in web design, 3–5, 289
occurring when hovering, 60–61, 63–65
iteration counts for, 114–118
progressive, 60–61
JavaScript vs. CSS, 96
step functions for timing changes in, 69–71
keyframes for, 94
:before and :after pseudo-elements, 58, 62
looping, 114–118, 142, 143
body property, 212–213
practicing, 290
INDEX 299