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 CSS3 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 CSS3 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: Safari has a bug that prevents the animation from finishing correctly. This bug seems to be fixed in Safari using a WebKit nightly build, so future versions of Safari should look the same as Chrome. 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 Keyframe @ 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:
@-webkit-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 the -webkit-
prefix 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 it’s enough to know that the only stable browsers that support these types of animations are WebKit-based ones.
The Keyframe Selectors
Let’s add some rule sets inside the @ rule:
@-webkit-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 { -webkit-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 { -webkit-animation-name: sunrise; -webkit-animation-duration: 10s; -webkit-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:
@-webkit-keyframes sunrise { 0% { background: #f00; left: 340px; bottom: 0; -webkit-animation-timing-function: ease; } 33% { bottom: 340px; left: 340px; background: #ffd630; -webkit-animation-timing-function: linear; } 66% { left: 40px; bottom: 340px; background: #ffd630; -webkit-animation-timing-function: steps(4); } 100% { bottom: 0; left: 40px; background: #f00; -webkit-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 { -webkit-animation-name: sunrise; -webkit-animation-duration: 10s; -webkit-animation-timing-function: ease; -webkit-animation-iteration-count: 1; -webkit-animation-direction: normal; }
This introduces two more properties: one that tells the animation how many times to play, and an indicator that tells the animation to animate back to the start position.
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 { -webkit-animation-name: sunrise; -webkit-animation-duration: 10s; -webkit-animation-timing-function: ease; -webkit-animation-iteration-count: 1; -webkit-animation-direction: normal; -webkit-animation-delay: 5s; -webkit-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 { -webkit-animation-name: sunrise; -webkt-animation-duration: 10s; -webkit-animation-timing-function: ease; -webkit-animation-iteration-count: 1; -webkit-animation-direction: normal; -webkit-animation-delay: 5s; -webkit-animation-play-state: running; -webkit-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: It seems that the animation-fill-mode
property has been removed from the spec, or else was never there to begin with, so this property may not end up in the spec. Also, Chrome and Safari respond differently when it is used. Safari will only apply a value of “forwards” if there are exactly two keyframes defined. It always seems to use the 2nd keyframe as the “forwards” state, which is not how Chrome does it; Chrome uses the final keyframe, which seems to be correct behaviour. Additionally, I’ve confirmed that the most up to date WebKit nightly does not have this bug, so future versions of Safari will render this correctly.
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.
If you view the page in anything but a WebKit browser, it won’t work. Firefox does not currently support keyframe-based animation, but support is expected for Firefox 5, and the -moz
-based syntax for animations is supported in the latest Aurora build. So, to make the source code as future-proof as possible, I’ve included all of the -moz
prefixes along with the standard syntax.
Here are the browsers that support CSS3 keyframe animations:
- Chrome 2+,
- Safari 4+,
- Firefox 5+,
- iOS Safari 3.2+,
- Android 2.1+.
Although no official announcement has been made, support in Opera is expected. There’s no word yet on support in IE.
Further Reading
- “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
(al) (vf) (il)
© Louis Lazaris for Smashing Magazine, 2011. |
Permalink |
Post a comment |
Smashing Shop |
Smashing Network |
About Us
Post tags: animations, CSS3