HTML and CSS Reference
In-Depth Information
Chapter 21
Animating with CSS Keyframes
The CSS transitions covered in the preceding chapter create simple animations that smooth changes in an
element's properties from one state to another. But they're limited to the transition between two points. The
only influence you have over what happens between those points lies in the choice of timing function. The CSS
define
keyframes
that specify what an element's properties should be at each stage of the animation. As with
transitions, it's left up to the browser to work out how to animate a smooth change from one keyframe to the next.
So, you need to create keyframes only for significant changes. But you have finer control over reverse transitions,
and you can specify how many times the animation should run or make it repeat endlessly.
In spite of their power, CSS animations aren't designed for heavy graphic manipulation. Consider using
HTML5 video and canvas, or WebGL instead. The principal value of CSS animations is the ability to create more
complex transitions than those described in Chapter 20. For example, a CSS transition can change an element's
background from one color to another, but you can't make it go through a series of color changes. CSS animations
make that sort of complex transition possible.
In this chapter, you'll learn how to do the following:
•
Define animation keyframes
•
Add an animation to page elements
•
Control the duration and number of iterations of an animation
•
Highlight an element by fading in a background color and fading it out again
•
Choose the direction of alternate iterations
•
Pause an animation
•
Control how properties are displayed before and after an animation
•
Add multiple animations to an element
■
CSS animations have many similarities with CSS transitions. You'll find this chapter easier to follow if you
have read the section on CSS transitions in the previous chapter.
Tip
Browser Support for CSS Animations
WebKit-based browsers and Firefox began supporting animations (with browser-specific prefixes) at an early
stage, but Internet Explorer and Opera didn't support animations until IE 10 and Opera 12.0.