CSS animation: pause, resume, and keep the last keyframe
CSS animation plays styles defined in @keyframes over time. In this example, a checkbox pauses and resumes playback. Change the direction and animation-fill-mode to see which frame remains when it finishes. For a single change driven by a state, compare the CSS transition example.
Connect keyframes to playback settings
The 0%, 50%, and 100% steps in @keyframes travel are positions in one iteration. animation uses that name and sets the duration and direction on an element. Two seconds is the length of one pass. Here a two-second forward pass alternates with a two-second reverse pass. See CSS Animations · Keyframes.
| Setting | Example and meaning |
|---|---|
animation-name |
travel: name of the keyframes to apply |
animation-duration |
2s: duration of one pass |
animation-timing-function |
linear: steady progress between keyframes |
animation-delay |
0s: no delay at the start |
animation-iteration-count |
infinite: repeat without a limit |
animation-direction |
alternate: switch between forward and reverse |
animation-fill-mode |
none: use the underlying style outside the active period |
animation-play-state |
paused: wait for the checkbox before advancing |
Try pausing and resuming with a checkbox
Save the code as animation.html and open it. No external files are required. Click the checkbox or reach it with Tab and press Space. Uncheck it to pause at the current point, then check it again to continue from there.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Control a CSS animation with a checkbox</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
label { display: inline-block; padding: 10px 4px; }
.stage { max-width: 500px; min-height: 130px; padding: 24px 16px;
border: 1px solid #8ba8bb; background: #eef4f8; }
.box { width: 64px; height: 64px; background: #1768a5;
transform: translateX(0) scale(1); transform-origin: center;
animation: travel 2s linear 0s infinite alternate none paused; }
@keyframes travel {
0% { transform: translateX(0) scale(0.7); background: #1768a5; }
50% { transform: translateX(70px) scale(1.15); background: #d99d33; }
100% { transform: translateX(140px) scale(1.3); background: #087b76; }
}
#play:checked ~ .stage .box { animation-play-state: running; }
@media (prefers-reduced-motion: reduce) {
.box { animation: none; }
}
</style>
</head>
<body>
<h1>Compare playback and pause</h1>
<input id="play" type="checkbox">
<label for="play">Play the animation</label>
<div class="stage"><div class="box"></div></div>
<p>Uncheck to pause. Check again to continue from the same point.</p>
</body>
</html>
The blue square passes through the orange 50% frame after one second and reaches the larger teal square on the right after two. The reverse pass takes two more seconds. scale() makes it look bigger while preserving the element’s 64×64px footprint in the document.
Compare duration and number of passes
Changing 2s to 4s in .box takes four seconds to cover the same path in one direction. Restore 2s after comparing. Set the iteration count to 2 instead of infinite to finish after two passes, or four seconds with alternate. A positive delay runs once before playback, and changing the second time value from 0s to -1s starts at the 50% point of the first pass. Restore the original count and delay before the next exercise.
Use fill mode to compare ending frames
Add the rules below after the animation shorthand in .box. Reload, then play one forward pass. forwards keeps the larger teal square on the right. With none, the box returns to the unanimated blue 64px square.
animation-iteration-count: 1;
animation-direction: normal;
animation-fill-mode: forwards;
Now change the added iteration count to 2 and the direction to alternate, keeping forwards. The reverse pass finishes at 0%, so the small blue square remains on the left. backwards applies the first frame it will play during a positive delay. Try a 1s delay with the normal direction to see the small square before playback begins. both combines the two fill behaviors. See CSS Animations · Fill Mode.
Replay a finished animation
Unchecking and rechecking resumes playback. It does not restart a finite animation that already finished. To repeat the exercise, reload the page or remove and reapply the animation. If a shorthand comes later in the stylesheet, it can reset earlier iteration, direction, and fill settings.
With prefers-reduced-motion: reduce, the example removes animation and shows the default blue box. If the box fails to move in other cases, check the keyframe name, a nonzero duration, and the animation-play-state after checking the box. See Media Queries · Reduced Motion.




