Sub topic 7
Describing multi-step motion with @keyframes
This chapter assumes you can write basic CSS selectors, know how the cascade picks a winning declaration, and have used the transition shorthand. It also assumes you know the animation shorthand at a basic level, since the previous chapters introduced it. Here the focus is on the @keyframes rule itself: how to write it, how the browser reads it, and where it behaves in surprising ways.
What @keyframes does
A transition moves between two states: the one before a change and the one after. A keyframe animation lets you describe the states in between. The @keyframes rule defines a named list of waypoints. An element opts in by pointing at that name with animation-name, or with the animation shorthand.
The @keyframes rule only describes the motion. It does nothing until an element references it. The same keyframes can be reused by any number of elements, each with its own duration, delay and easing.
@keyframes pulse {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
}
.badge {
animation: pulse 1.5s ease-in-out infinite;
}Here pulse has two distinct waypoints. The comma list 0%, 100% gives both the start and the end the same declarations, so the animation loops without a jump.
Keyframe selectors
Each block inside @keyframes starts with a keyframe selector, which says how far through one iteration the block applies.
fromis the same as0%.tois the same as100%.- A percentage between
0%and100%marks a point inside the iteration. - Several selectors can share one block, separated by commas.
Percentages are relative to the duration of one iteration, not to the total time. With animation: slide 2s 3, a 50% keyframe is reached at 1 second in each of the three iterations.
Order in the source does not matter. The browser sorts keyframes by their percentage. If two blocks use the same percentage, their declarations merge, and the later one wins for any property set in both.
Invalid keyframes
Browsers discard what they cannot parse, and they do so quietly:
- A selector outside the 0% to 100% range, such as
120%or-10%, makes that whole block invalid. The block is dropped. - A declaration the browser does not understand is dropped, and the rest of the block still applies.
- Declarations marked
!importantinside a keyframe are ignored. - A property that cannot be animated, such as
displayin older engines, has no effect in keyframes.
If part of your motion is missing, check the DevTools console and the Styles panel for dropped values before you suspect the timing.
Implicit start and end values
You are not required to write 0% and 100%. If you omit them, the browser builds the missing keyframe from the element's own computed value for each animated property.
@keyframes grow {
50% {
transform: scale(1.5);
}
}
.icon {
transform: rotate(10deg);
animation: grow 1s ease-in-out;
}The implicit 0% and 100% keyframes are both rotate(10deg), because that is the element's computed transform. The animation therefore swaps the rotation for scale(1.5) at the midpoint, since rotate(10deg) and scale(1.5) are different transform functions. That is a common source of a visible kink. If you want to keep the rotation, repeat it in every keyframe: rotate(10deg) scale(1.5).
This implicit behavior is useful. The animation starts and ends at whatever the element normally looks like, so a later change to the normal style updates the animation endpoints automatically.
How properties interpolate between keyframes
Each property is interpolated independently. For a given property, the browser looks only at the keyframes that mention it, and animates between neighbouring ones.
@keyframes drift {
0% {
opacity: 0;
}
50% {
transform: translateX(100px);
}
100% {
opacity: 1;
transform: translateX(200px);
}
}opacity is set at 0% and 100%, so it fades across the whole run. transform is set at 50% and 100%. Its first keyframe is implicit, so it animates from the computed value to translateX(100px), then on to translateX(200px).
Two rules apply to transforms:
- If two neighbouring
transformvalues use the same function list in the same order, such astranslateX() rotate()followed bytranslateX() rotate(), each function is interpolated on its own. - If the lists differ, the browser falls back to interpolating the full matrices. This can produce paths and rotations you did not intend.
Keep the function lists identical across keyframes when you can.
Easing inside keyframes
The animation-timing-function you set on the element applies to the interval between each pair of keyframes. It does not apply once across the whole animation. With ease-in-out and five keyframes, every segment accelerates and decelerates on its own.
You can override the easing for one segment by declaring animation-timing-function inside a keyframe block. That value controls the segment that starts at that keyframe and runs to the next. The last keyframe's value has no effect, since no segment follows it.
This makes physical motion possible, such as a bouncing ball:
<div class="ball"></div>@keyframes bounce {
0% {
transform: translateY(0);
animation-timing-function: ease-out;
}
40% {
transform: translateY(-120px);
animation-timing-function: ease-in;
}
70% {
transform: translateY(0);
animation-timing-function: ease-out;
}
85% {
transform: translateY(-30px);
animation-timing-function: ease-in;
}
100% {
transform: translateY(0);
}
}
.ball {
width: 40px;
height: 40px;
margin-top: 140px;
border-radius: 50%;
background-color: #2563eb;
animation: bounce 1.2s linear infinite;
}Going up, the ball slows down, so those segments use ease-out. Coming down, it speeds up, so those segments use ease-in. The element-level linear is only a fallback, and every segment except the last overrides it.
Keyframes override the normal cascade
While an animation is running, the values it produces sit above ordinary author declarations in the cascade, apart from !important rules. A class selector with a high specificity cannot beat an animated value. This surprises people who try to override an animated opacity or transform with a stronger selector, and who find the change has no effect until the animation ends or is removed.
Once the animation finishes and has no fill mode, the property returns to its normal value. A later chapter covers fill modes. For now, remember that keyframes only control the element while the animation is active.
Using custom properties in keyframes
A var() reference inside a keyframe is resolved on the element being animated. That lets one @keyframes rule serve elements with different parameters.
@keyframes slide-in {
from {
transform: translateX(var(--distance, -40px));
opacity: 0;
}
to {
transform: none;
opacity: 1;
}
}
.panel-left {
--distance: -80px;
animation: slide-in 400ms ease-out both;
}
.panel-right {
--distance: 80px;
animation: slide-in 400ms ease-out both;
}The custom property itself is not what is animated here. Only the transform result changes over time. If you want to animate a custom property directly, for example a number or an angle, register it first with @property and a syntax. Unregistered custom properties are treated as plain text, and text switches at the 50% point instead of interpolating.
Running several animations on one element
animation-name accepts a comma-separated list, and the other animation longhands match it position by position.
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@keyframes glow {
0%, 100% {
opacity: 0.6;
}
50% {
opacity: 1;
}
}
.loader {
animation:
spin 2s linear infinite,
glow 1s ease-in-out infinite;
}The two animations touch different properties, so they combine cleanly. If two animations set the same property, such as transform, the one later in the list wins entirely, and the earlier one is hidden for that property. Combine the motions into a single keyframe rule, or put them on a parent and a child.
Naming and redefinition
- Names are case-sensitive identifiers.
Pulseandpulseare different. - A name cannot be
none, and a quoted string is also accepted as a name. - If two
@keyframesrules share a name, the last one in the cascade replaces the earlier one completely. The two rules are not merged. - If
animation-namerefers to a name that does not exist, nothing animates, and there is no error message.
Misspelled names are the most frequent reason an animation seems to do nothing. In DevTools, the Animations panel shows whether the browser found the rule.
Common mistakes
- Animating from
autovalues. Keyframes cannot interpolate to or fromheight: auto, so the value jumps. Animatetransformor a fixed size instead. - Forgetting to repeat the other transform functions in each keyframe.
- Using percentages outside the 0% to 100% range and getting an empty animation.
- Expecting
animation-timing-functionon the element to shape the full run, when it applies per segment.
Exercise
An element has transform: rotate(10deg) in its normal style and the animation @keyframes grow { 50% { transform: scale(1.5); } } with animation: grow 1s ease-in-out. Why does the rotation seem to disappear at the midpoint, and how do you fix it?
Show answer
The 0% and 100% keyframes are built from the computed value, rotate(10deg), but the 50% keyframe sets transform to scale(1.5) alone. The function lists differ, so the rotation is not carried through. The fix is to write the full list in the keyframe: transform: rotate(10deg) scale(1.5);. Optionally, also add explicit 0% and 100% keyframes with rotate(10deg).
Exercise
In the bounce example, what happens to the animation-timing-function: ease-in; declared inside the 85% keyframe, and what would a declaration inside the 100% keyframe do?
Show answer
The value in the 85% block controls the segment from 85% to 100%. A declaration in the 100% block has no effect, because no segment starts there.