Sub topic 8
Controlling animations: duration, iteration count, delay and the shorthand
This chapter assumes you can write a @keyframes rule, select elements with CSS, and know the basics of transition. Here you learn the animation-* properties that control how a keyframe animation plays, and how they combine into the animation shorthand.
What the longhand properties do
A @keyframes rule only describes the steps. Nothing plays until an element has animation-name set to that rule's name. These properties then control the playback.
| Property | Controls | Default |
|---|---|---|
| `animation-name` | Which `@keyframes` rule to run | `none` |
| `animation-duration` | Time for one cycle | `0s` |
| `animation-timing-function` | Easing within each keyframe interval | `ease` |
| `animation-delay` | Wait before the first cycle starts | `0s` |
| `animation-iteration-count` | Number of cycles, or `infinite` | `1` |
| `animation-direction` | Forward, reverse or alternating cycles | `normal` |
| `animation-fill-mode` | Styles applied before and after playback | `none` |
| `animation-play-state` | `running` or `paused` | `running` |
The default duration is 0s. An animation with no duration set runs instantly and appears to do nothing. This is the most common reason a new animation seems broken.
Duration, delay and iteration
The next example runs the pulse animation from the earlier chapter, written with longhand properties.
@keyframes pulse {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
}
.badge {
animation-name: pulse;
animation-duration: 1.5s;
animation-timing-function: ease-in-out;
animation-delay: 500ms;
animation-iteration-count: infinite;
}The badge waits 500 ms, then repeats the 1.5 second cycle forever. The delay applies only once, before the first cycle. It does not repeat between cycles.
A negative delay such as -750ms starts the animation immediately, as if it had already been running for 750 ms. Use it to desynchronize a group of elements that share one animation.
animation-iteration-count also accepts fractions. A value of 2.5 plays two full cycles and then stops halfway through the third.
Timing functions apply to each interval between keyframes, not to the animation as a whole. With three keyframes, ease-in-out is applied twice.
The shorthand
The animation shorthand sets all of these properties in one declaration.
.badge {
animation: pulse 1.5s ease-in-out 500ms infinite;
}The browser sorts the values by type, so most can appear in any order. Two rules apply:
- The first time value is the duration, and the second time value is the delay. Keep them in that order.
- A keyword that is also a valid animation name can be misread. For example, in
animation: none 1s,noneis read as the name. Keep the animation name distinct from keywords such asinfinite,bothandpaused.
Any longhand you leave out is reset to its default. This matters when you override an animation in another rule.
.badge {
animation: pulse 1.5s ease-in-out infinite;
}
.badge.calm {
animation-duration: 4s;
}Here the longhand animation-duration changes only the duration, so the .calm badge keeps the name, easing and iteration count. If you wrote animation: pulse 4s in .calm, the shorthand would also reset the iteration count to 1.
A single element can run several animations by separating them with commas.
.badge {
animation:
pulse 1.5s ease-in-out infinite,
fade-in 400ms ease-out both;
}How the pieces fit together
Common mistakes
- Omitting the duration, so the animation never visibly plays.
- Using a shorthand in an override and losing the iteration count or fill mode.
- Expecting
animation-delayto pause between repeats. It does not. - Misspelling the name in
animation-name, which fails silently because it no longer matches the@keyframesrule.
Exercise
A badge uses animation: pulse 2s infinite;. A later rule for .badge.urgent sets animation: pulse 0.8s;. The urgent badge pulses only once. Why, and what is the smallest fix?
Show answer
The shorthand in .badge.urgent resets animation-iteration-count to its default of 1, because it was not repeated. The smallest fix is to use the longhand animation-duration: 0.8s; in the override, or to write animation: pulse 0.8s infinite;.