Sub topic 9
Fill mode, direction and pausing
A keyframe animation does not control an element for the whole time the element is on the page. It controls the element only while it is running. This chapter covers three properties that decide what happens around and during a run: animation-fill-mode, animation-direction and animation-play-state.
This chapter assumes you can write a @keyframes rule and use the animation shorthand with duration, timing function, delay and iteration count, as covered in the previous chapters. It also assumes you know how the cascade picks between competing declarations.
What an animation does outside its active period
An animation has three phases. If you set animation-delay, the animation waits through a delay phase. Then it runs through its active phase, which is the duration multiplied by the iteration count. After that, it is finished.
By default an animation affects the element only during the active phase. During the delay, the element shows its normal styles. When the animation ends, the element jumps back to its normal styles. That jump is the "snap" or "flash" you see when an animation seems to work but the result does not stay.
.card {
opacity: 1;
animation: fade-in 400ms ease-out 1s;
}
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}This card has two problems. For the first second it is fully visible, because the delay phase applies no keyframe. Then it blinks to transparent, fades in, and ends at opacity: 1. The end state happens to match the normal style here, but the start does not. Fill mode fixes this.
animation-fill-mode
animation-fill-mode tells the browser whether to apply keyframe values outside the active phase.
| Value | Delay phase | After the end |
|---|---|---|
| `none` (default) | Normal styles | Normal styles |
| `backwards` | First keyframe applied | Normal styles |
| `forwards` | Normal styles | Last keyframe held |
| `both` | First keyframe applied | Last keyframe held |
Here is the fixed card. It starts hidden during its one-second delay, fades in, and stays visible.
@keyframes fade-in {
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: none;
}
}
.card {
animation: fade-in 400ms ease-out 1s both;
}In the shorthand, the first time value (400ms) is the duration and the second (1s) is the delay. The keyword both sets the fill mode. You can also write the longhand animation-fill-mode: both;.
Which keyframe is "first" and "last"
"First keyframe" and "last keyframe" depend on direction and iteration count. For backwards, the first keyframe is from (0%) when the direction is normal or alternate. It is to (100%) when the direction is reverse or alternate-reverse.
For forwards, the held value is the end of the final iteration. Consider an animation with animation-direction: alternate and animation-iteration-count: 2. Iteration one runs from to to, and iteration two runs back from to to from. The element ends on the from values. With a fractional count such as 1.5, the animation stops halfway through the second iteration, and forwards holds that midpoint.
Using forwards safely
A held keyframe value is not a normal style. Animations sit in the cascade above ordinary author declarations, so a forwards fill overrides your regular CSS. If you later change opacity in a :hover rule on the same element, the hover rule loses to the held animation value, and it looks like the rule is broken.
.toast {
animation: slide-out 300ms ease-in forwards;
}
@keyframes slide-out {
to { transform: translateX(100%); }
}
/* This rule has no visible effect while the fill is held. */
.toast:hover {
transform: none;
}Use these rules of thumb:
- Use
backwardswhen the animation has a delay and the start state differs from the normal style. - Use
bothfor one-shot entrance effects, so the element is correct before, during and after. - If the end state is the permanent state, consider setting it as the element's normal style instead of relying on
forwards. The animation then only describes the journey.
animation-direction
animation-direction controls whether each iteration runs from from to to or the other way around.
| Value | Behavior |
|---|---|
| `normal` | Every iteration runs forward, from 0% to 100%. |
| `reverse` | Every iteration runs backward, from 100% to 0%. |
| `alternate` | Odd iterations run forward and even iterations run backward. |
| `alternate-reverse` | Odd iterations run backward and even iterations run forward. |
The alternate values only matter when the iteration count is above one. With a single iteration, alternate behaves like normal.
A smooth back-and-forth loop is the classic use. Compare this with the pulse example from earlier chapters, which put the return trip into the keyframes themselves (0%, 100% and 50%). With alternate, you describe only one direction.
@keyframes grow {
from { transform: scale(1); }
to { transform: scale(1.1); }
}
.badge {
animation: grow 750ms ease-in-out infinite alternate;
}The duration is now the time for one leg. A full cycle takes 1.5 seconds, the same as the earlier pulse.
Easing on the way back
When an iteration runs in reverse, the easing curve is also traversed backward. An ease-in curve, which starts slow and ends fast, therefore behaves like ease-out on the return leg. A symmetric curve such as ease-in-out looks the same in both directions, which is why it suits alternate. If you want the same feel in both directions with an asymmetric curve, write the return trip into the keyframes and use normal.
Reverse with a delay
animation-delay still applies once, at the start, and reverse does not change that. For reverse with backwards fill, the element sits at the to values during the delay.
Pausing with animation-play-state
animation-play-state accepts running (the default) or paused. Pausing freezes the animation at its current point. The element keeps the value it had at that moment, with no fill mode involved. Setting the state back to running resumes from the same point, not from the beginning.
.spinner {
animation: spin 2s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner:hover {
animation-play-state: paused;
}Because the pause is part of the state, it works with pseudo-classes and class toggles. Here is a complete example with a button that pauses and resumes the spinner.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pause demo</title>
<style>
.spinner {
width: 48px;
height: 48px;
border: 6px solid #cbd5e1;
border-top-color: #2563eb;
border-radius: 50%;
animation: spin 1s linear infinite;
}
.spinner.is-paused {
animation-play-state: paused;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="spinner" id="spinner"></div>
<button id="toggle" aria-pressed="false">Pause</button>
<script>
const spinner = document.getElementById('spinner');
const toggle = document.getElementById('toggle');
toggle.addEventListener('click', () => {
const paused = spinner.classList.toggle('is-paused');
toggle.textContent = paused ? 'Resume' : 'Pause';
toggle.setAttribute('aria-pressed', String(paused));
});
</script>
</body>
</html>The class changes only animation-play-state. The other animation properties stay untouched, which is why the spinner resumes where it stopped. If you instead removed and re-added the whole animation declaration, the animation would restart from the beginning.
Details that cause surprises
- Play state is per animation. When you list several animations,
animation-play-state: paused, running;pairs values with names by position. If the list is shorter than the list of names, it repeats. - Pausing during the delay. The delay clock also stops, so the animation does not start until you resume.
- Starting mid-way. A negative
animation-delaysuch as-500msmakes the animation begin as if it had already run for 500 milliseconds. This is a common way to desynchronize a group of looping elements. - Transitions have no play state. CSS offers no way to pause a transition. If you need that control, use the Web Animations API, covered in a later chapter.
Summary of choices
| Goal | Use |
|---|---|
| Hide an element during its delay, then fade it in and keep it visible | `animation-fill-mode: both` |
| Keep the end state after a one-shot animation | `forwards`, or set the end state as the normal style |
| Loop back and forth from one set of keyframes | `animation-direction: alternate` with `infinite` |
| Play an existing animation backward | `animation-direction: reverse` |
| Let the user freeze a looping effect | `animation-play-state: paused` toggled by a class |
Exercise
A modal has animation: pop-in 300ms ease-out 200ms; where pop-in goes from opacity: 0 to opacity: 1. Users see the modal fully visible for 200 ms, then it vanishes and fades in. Which single keyword fixes this, and why is forwards alone not enough?
Show answer
Add backwards (or both). During the 200 ms delay no keyframe applies by default, so the modal shows its normal style, which is opacity: 1. backwards applies the first keyframe (opacity: 0) during the delay. forwards only affects what happens after the animation ends, so it does not touch the delay period.
Exercise
An element has animation: slide 1s ease-in 3 alternate forwards; and slide runs from translateX(0) to translateX(100px). Where does the element rest after the animation finishes, and how does it move on the second leg?
Show answer
It rests at translateX(100px). Iterations one and three run forward (0 to 100px) and iteration two runs backward (100px to 0). The final iteration is odd, so it ends at the to keyframe, and forwards holds it. On the second leg the easing curve is also traversed in reverse, so the ease-in feel becomes an ease-out feel.
Exercise
A page sets animation-play-state: paused; on .banner:hover, but the banner restarts from the beginning every time the pointer leaves. A teammate says the hover rule also contains animation: none;. Why does that cause the restart, and what should the hover rule contain?
Show answer
Setting animation: none removes the animation entirely, so the browser discards its progress, and removing none later starts a new animation from 0. The hover rule should contain only animation-play-state: paused;, which freezes the existing animation and lets it resume from the same point.