Like CSS Transitions, CSS Animations also enable changes in CSS property values to occur smoothly over a period of time. With either technology, you could create an animation that fades opacity on an element from 1.0 to 0... but with CSS Animations, you can create an animation where the opacity changes from 1.0 to 0.2, then pulses between 0.2 and 0.5 a few times, then finally fades to 0. CSS Animations give you fine-grained control over the individual keyframes of an animation, whereas with CSS Transitions, all keyframe rendering between two CSS property values is automatically handled by the browser. You can use CSS Animations to create animations that:
and of course, much more.
Code-wise, there are three components needed to define and execute a CSS animation:
A name and definition for the behavior of the animation, written with the CSS at-rule @keyframes.
General parameters for the animation (such as its duration), set by various CSS properties.
Triggering the animation to begin by setting the CSS property animation-name.
Let's create a "pop" animation to get an idea of how each of these three components translates to code. This example will briefly go over the basics; more details will be covered in the next sections, Defining Animations and Running Animations.
The first step is to use the @keyframes at-rule to define the behavior of our animation, and to give it a name. We want to create an animation where a box "pops" onto the screen. The box is initially invisible with a scale factor of 0, then it grows to a size that is slightly larger than its original size. Next it scales down to dimensions slightly less than the original size, then back up to original size. The first step — where the element goes from invisible to slightly larger than original size — should take up most of the time in the animation, and the last few steps should take relatively less time.
The @keyframes rule lets you break the animation into keyframes. Here's how to define this animation using the @keyframes rule, while naming the animation pop . We'll eventually apply this animation to a a <div> with id="box":
Each set of keyframes are assigned a percentage value, to correlate CSS property changes over a portion of the total animation timeline. For example, if the duration of the pop animation were 10 seconds, the box would be scaling from 0 to 1.1 for 8 seconds.
Next, set general parameters for the animation — such as how long it will take — using animation- properties on the element being animated:
And finally, trigger the animation to begin by setting animation-name to the name of the animation you just created. Below, animation-name is set on #box in the static CSS of the page. As a result, animation-name will be applied and rendered just like any other CSS property — when the page is loaded. This means that as soon as the page loads, the box will "pop" into view:
This short code sample introduces the @keyframes rule, plus three CSS properties for defining CSS Animations: animation-duration, animation-timing-function and animation-name. In the next sections you'll learn more details about these properties, plus new ways to define your animations with the @keyframes rule. You'll also learn about additional properties that let you:
iterate an animation several times, or infinitely
delay the start of an animation
play an animation in reverse
pause and resume an animation
As you can already see, CSS Animations are flexible enough for simple animations, yet powerful enough for more complex ones. Let's move on to the next sections to learn more.