Sub topic 8
Controlling animations: duration, iteration count, delay and the shorthand
This chapter assumes you can already write a basic @keyframes rule and know what a timing function such as ease-out does. You should also be comfortable with CSS selectors and the cascade. Here the focus is the properties that decide how long an animation runs, when it starts, how many times it repeats, and how to write them compactly.
The animation properties at a glance
A @keyframes rule only describes what changes. It does nothing until an element references it through animation-name. The other animation-* properties then control the timing.
| Property | Controls | Initial value |
|---|---|---|
| `animation-name` | Which `@keyframes` rule to run | `none` |
| `animation-duration` | Length of one cycle | `0s` |
| `animation-timing-function` | Easing between keyframes | `ease` |
| `animation-delay` | Wait before the first cycle starts | `0s` |
| `animation-iteration-count` | Number of cycles | `1` |
| `animation-direction` | Forward, backward or alternating cycles | `normal` |
| `animation-fill-mode` | Styles applied before and after the run | `none` |
| `animation-play-state` | Running or paused | `running` |
Direction, fill mode and play state have their own chapter. This chapter covers name, duration, delay, iteration count, and how the shorthand combines all of them.
Duration
animation-duration sets the length of one cycle. It takes a time value in seconds (s) or milliseconds (ms). Negative values are invalid.
The initial value is 0s. This is the most common reason a new animation seems to do nothing. If you set animation-name but forget the duration, every cycle takes no time and the element jumps straight to the end state.
@keyframes slide-in {
from {
transform: translateX(-24px);
opacity: 0;
}
to {
transform: none;
opacity: 1;
}
}
.panel {
animation-name: slide-in;
animation-duration: 350ms;
}A zero-duration animation is not ignored completely. Its animationstart and animationend events still fire, and fill mode still applies.
Most interface motion falls between 150ms and 500ms. Shorter feels abrupt. Longer makes the interface feel slow when the animation runs on every interaction. Looping decorative effects, such as a pulsing badge, can run for a second or more because they are not blocking the user.
Delay
animation-delay sets how long the browser waits after the animation is applied before the first cycle begins. The delay happens once. It is not added before each repeat.
A negative delay is valid and useful. The animation starts immediately, but as if it had already been running for that long. With a 4s duration and animation-delay: -1s, the first frame shows the state 25% of the way through the cycle.
@keyframes drift {
from {
transform: translateY(0);
}
to {
transform: translateY(-12px);
}
}
.dot {
animation: drift 4s ease-in-out infinite alternate;
}
.dot:nth-child(2) {
animation-delay: -1s;
}
.dot:nth-child(3) {
animation-delay: -2s;
}All three dots move on the same loop, but they sit at different points in it from the first frame. With positive delays, the second and third dots would sit still for one and two seconds before starting. Negative delays avoid that.
Iteration count
animation-iteration-count sets how many cycles run. It accepts three kinds of value:
- A positive integer, such as
3. - A positive decimal, such as
2.5. The animation stops partway through the last cycle. - The keyword
infinite, which repeats until the animation is removed.
Negative values are invalid, and 0 runs no cycles. The total active time of a finite animation follows a simple formula:
total time = delay + (duration × iteration count)With a 300ms delay, a 500ms duration and 3 iterations, the animation finishes at 1800ms. With a count of 2.5 it finishes at 1550ms, and the element stops halfway through the third cycle.
A fractional count combined with infinite makes no sense, since infinite has no end. Use fractions only with finite counts.
Timing functions apply per interval
The animation-timing-function property runs between each pair of keyframes. It does not run once across the whole cycle. With three keyframes at 0%, 50% and 100%, an ease-in-out function restarts its curve in each half. The motion eases out at the middle keyframe and eases in again. If you want the easing to span the whole cycle, use only from and to. If you want different easing in a single interval, set animation-timing-function inside that keyframe block:
@keyframes bounce-in {
0% {
transform: scale(0.6);
animation-timing-function: ease-out;
}
60% {
transform: scale(1.08);
animation-timing-function: ease-in-out;
}
100% {
transform: scale(1);
}
}Here the function written in the 0% block controls the interval from 0% to 60%. The one in the 60% block controls 60% to 100%.
The shorthand
The animation shorthand sets all eight longhand properties in one declaration:
.badge {
animation: pulse 1.5s ease-in-out 200ms infinite alternate both;
}This is equivalent to:
.badge {
animation-name: pulse;
animation-duration: 1.5s;
animation-timing-function: ease-in-out;
animation-delay: 200ms;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-fill-mode: both;
animation-play-state: running;
}How the browser reads the values
Most values can appear in any order, because their types are distinguishable: a keyword like infinite, a number, a time, or an easing function. Two rules are fixed:
- Time values are positional. The first time is the duration and the second is the delay. If you write only one time, it is the duration.
- A keyword that belongs to another property cannot be used as the name. The name
easeorinfinitewould be read as a different property's value, so avoid naming@keyframesrules after CSS keywords.
Writing animation: 200ms 1.5s pulse therefore sets a 200ms duration and a 1.5s delay, which is rarely what you meant.
The shorthand resets what you omit
Any longhand you leave out is reset to its initial value. This causes a common bug:
.badge {
animation-delay: 500ms;
animation: pulse 1s ease-in-out infinite;
}The shorthand comes second, so it resets animation-delay to 0s. Put the shorthand first, then override with longhands:
.badge {
animation: pulse 1s ease-in-out infinite;
animation-delay: 500ms;
}The same applies across rules. A later animation shorthand with equal specificity, such as in a media query or a modifier class, replaces every longhand from the earlier rule. To change only one aspect, such as the delay, use the longhand in the later rule.
Running several animations on one element
Separate the animations with commas. Each comma-separated item in the shorthand is one complete animation:
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@keyframes fade {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.loader {
animation:
fade 300ms ease-out both,
spin 800ms linear infinite;
}With longhands, each property takes a list, and the lists line up by position. The first value in every list belongs to the first name, the second to the second, and so on:
.loader {
animation-name: fade, spin;
animation-duration: 300ms, 800ms;
animation-iteration-count: 1, infinite;
}If a list is shorter than the list of names, it repeats to fill the gap. If it is longer, the extra values are ignored. If two animations change the same property, the one later in the list wins.
One conflict to avoid: both animations above touch different properties (opacity and transform). If fade also changed transform, spin would override it while both run.
Staggering with a custom property
A common pattern is to start a series of items one after another. Store each item's index in a custom property and multiply it by a step:
<ul class="list">
<li style="--i: 0">First</li>
<li style="--i: 1">Second</li>
<li style="--i: 2">Third</li>
</ul>@keyframes fade-in {
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: none;
}
}
.list li {
animation: fade-in 400ms ease-out both;
animation-delay: calc(var(--i) * 80ms);
}The both fill mode matters here. Without it, each item would be fully visible during its delay and then jump to invisible when its animation starts. Fill mode is covered fully in the next chapter.
Changing animation properties on a running element
Browsers treat a running animation as one object identified by its name. If you change animation-duration, animation-delay or animation-iteration-count while it runs, the animation keeps going with the new values and can visibly jump. If you change animation-name, the browser starts a new animation from the beginning.
Re-applying the same declaration does not restart an animation. To replay one from JavaScript, remove the class, force a style recalculation, and add it back:
const badge = document.querySelector('.badge');
function replay() {
badge.classList.remove('is-pulsing');
void badge.offsetWidth; // forces the browser to apply the removal first
badge.classList.add('is-pulsing');
}Common errors
- Nothing moves. The duration is missing, so it defaults to
0s. Check thatanimation-namematches the@keyframesname exactly, including case. - The delay disappeared. A later shorthand reset it. Move the longhand after the shorthand.
- Duration and delay swapped. The first time value is the duration.
- Items flash before they animate. A delay was added without a fill mode that applies the
fromkeyframe during the wait. - Easing looks wrong in a multi-step animation. The timing function runs per interval, not across the whole cycle.
- The animation will not replay. The name did not change. Use the remove, reflow and add pattern, or the Web Animations API covered in a later chapter.
Exercise
An element has animation: pulse 2s ease-in-out 500ms 3;. How long after the animation is applied does it finish, and what changes if you write animation: pulse 500ms 2s ease-in-out 3;?
Show answer
The first version finishes at 6.5 seconds: a 500ms delay plus 3 cycles of 2s each. In the second version, the first time value is the duration and the second is the delay. The duration becomes 500ms and the delay becomes 2s, so the animation finishes at 2s + (3 × 500ms) = 3.5 seconds.
Exercise
This rule has a bug. The author expects a one second delay, but the animation starts immediately. Fix it. .toast { animation-delay: 1s; animation: slide-in 300ms ease-out both; }
Show answer
The animation shorthand comes after animation-delay and resets the delay to 0s. Swap the order, or put the delay in the shorthand: .toast { animation: slide-in 300ms ease-out 1s both; }.