Sub topic 9
Fill mode, direction and pausing
This chapter covers three animation controls that decide what an element looks like before, after and during a keyframe animation: animation-fill-mode, animation-direction and animation-play-state. It assumes you can write @keyframes rules and use the animation shorthand, and that you know how to toggle a class with JavaScript.
What an animation does outside its run
By default, a keyframe animation only affects the element while it is running. Before it starts, the element uses its normal styles. When it ends, the element snaps back to those same normal styles. Two problems follow from this:
- An element with a delay shows its final, normal look during the delay, then jumps to the
fromkeyframe when the animation begins. - An element that fades in snaps back to its starting appearance when the animation ends. If the normal style is fully visible, nothing seems wrong. If the normal style is hidden, the element disappears again.
Fill mode fixes both problems.
Fill mode
animation-fill-mode sets which keyframe values apply outside the active run.
| Value | Before the animation starts | After the animation ends |
|---|---|---|
| `none` (default) | Normal styles | Normal styles |
| `backwards` | First keyframe values (during any delay) | Normal styles |
| `forwards` | Normal styles | Last keyframe values |
| `both` | First keyframe values | Last keyframe values |
@keyframes fade-in {
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: none;
}
}
.card {
animation: fade-in 400ms ease-out both;
}With both, the card is transparent and shifted down before the animation starts. It stays at its final values afterward. The card does not flash or snap.
Direction
animation-direction controls the order in which keyframes play on each iteration.
normal: from 0% to 100% every time.reverse: from 100% to 0% every time.alternate: forward on the first iteration, backward on the second, and so on.alternate-reverse: backward first, then forward.
alternate is useful for effects that should move out and back without a visible jump. Without it, an infinite animation jumps from the last keyframe back to the first at the start of every cycle. Note that alternate only has an effect when the iteration count is greater than 1, or infinite.
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(120px); }
}
.marker {
animation: slide 1s ease-in-out infinite alternate;
}Easing is also reversed on backward iterations. An ease-in curve becomes ease-out when it plays in reverse, so the motion looks symmetrical.
Pausing and resuming
animation-play-state accepts running (default) or paused. A paused animation keeps its current frame and continues from that point when set back to running.
.spinner {
animation: spin 2s linear infinite;
}
.spinner:hover,
.spinner:focus-visible {
animation-play-state: paused;
}
@keyframes spin {
to { transform: rotate(360deg); }
}Pausing on hover or focus is a useful pattern for moving content that users need time to read. You can also set the property from JavaScript by toggling a class:
const toggle = document.querySelector('#toggle');
const spinner = document.querySelector('.spinner');
toggle.addEventListener('click', () => {
spinner.classList.toggle('is-paused');
});.spinner.is-paused {
animation-play-state: paused;
}Common errors
- Using
forwardsand then wondering why a later style change has no effect. A filled-forward animation value overrides normal styles for that property, because animations sit above regular rules in the cascade. Remove the animation or the fill mode to regain control. - Expecting
animation-delayto hide the element. Withoutbackwardsorboth, the element shows its normal styles during the delay. - Writing
alternatewith a single iteration and seeing no change. - Resetting the
animationshorthand later in the cascade, which also resets fill mode, direction and play state to their defaults.
Exercise
A badge uses animation: pop 600ms ease-out 1s; where pop goes from opacity: 0 to opacity: 1, and the badge's normal style is opacity: 1. What does the user see during the first second and after the animation ends? Which fill mode fixes the first-second problem?
Show answer
During the first second (the delay) the badge is fully visible, because the normal style applies. When the animation starts it jumps to opacity: 0 and fades in. After it ends it stays visible because the normal style matches the last keyframe. Adding backwards (or both) applies the from keyframe during the delay, so the badge stays hidden until the animation starts.