Animation in Depth: Fill Modes, Direction, Composition, Events and Interruptions
Controlling an animation before, during and after it runs: animation-direction and reversed easing, fill modes and the delay phase, animation-composition, animation events and chaining, pauses between iterations, and restarting animations.
kvi5hnuCode Sprout team member9 min readOn this page
Article 109 covered defining and running keyframe animations. This article covers what happens around them: which way they play, what the element looks like before and after, how animations combine with each other and with the element's own values, and how to react to them from JavaScript.

animation-direction
| Value | Plays |
|---|---|
normal (default) | 0% to 100% every iteration |
reverse | 100% to 0% every iteration |
alternate | forward, then backward, then forward |
alternate-reverse | backward, then forward, then backward |
alternate makes smooth back-and-forth loops, like a pulse or a swinging element, from a single to keyframe: animation: sway 1s ease-in-out infinite alternate.
Playing backward also plays the timing function backward, so an ease-in curve looks like an ease-out on the way back. Tested in Chrome, with animation: grow 1s ease-in 2 alternate from 0 to 100px:
- 250ms into the first iteration, the width was 9.3px, the ease-in value at 25%.
- 250ms into the second, backward iteration, it was 62.2px, the ease-in value at 75%: the same curve, traveled the other way.
Tested in Chrome, with reverse and linear, the value 25% of the way through was 75px.
If the motion should feel the same in both directions, use a symmetric curve like ease-in-out, or write the return trip as keyframes.
animation-fill-mode
By default, an animation affects the element only while it's running. During a positive delay, the element shows its normal styles; when the animation ends, it snaps back to them. animation-fill-mode extends the animation's effect:
| Value | During the delay | After the end |
|---|---|---|
none (default) | normal styles | normal styles |
forwards | normal styles | the values of the last keyframe played |
backwards | the values of the first keyframe played | normal styles |
both | the first keyframe | the last keyframe |
Tested in Chrome, with a 1s delay animating from 0 to 100px on a 200px-wide element:
- During the delay, the width was 200px with
none, and 0px withbackwardsandboth. - After the end, it was back to 200px with
none, and 100px withforwardsandboth.
"First" and "last" depend on the direction and iteration count: with reverse, the first keyframe played is 100%; with alternate and an even count, the animation ends on 0%.
both is what you usually want for entrance animations with a delay: the element starts hidden, instead of appearing, disappearing and fading in again:
.item { animation: fade-up 0.4s ease-out both; }
.item:nth-child(2) { animation-delay: 0.1s; }
@keyframes fade-up { from { opacity: 0; translate: 0 1rem; } }
A filled animation still overrides the element's normal declarations, as a running one does (article 109). If an element should keep its final state and stay changeable afterward, set the final values as normal styles and let the animation start from elsewhere, as fade-up above does with its from-only keyframes.

In the fill-mode table, only the first and last columns change between rows: during the active phase, every fill mode looks the same.
animation-composition
By default, an animation replaces the property's value. animation-composition makes it combine with the underlying value instead, the value from the element's styles or from animations earlier in the list:
| Value | Effect |
|---|---|
replace (default) | the animation's value is used |
add | added to the underlying value; for transform lists, the functions are appended |
accumulate | combined component by component; for transforms, translations are summed, scales multiplied and rotations added |
Tested in Chrome, with an element at translate: 20px 0 and @keyframes add { to { translate: 50px 0 } }, halfway through:
replacegave 35px, halfway from the underlying 20px to 50px.addgave 45px: 20px plus half of 50px.
For transforms, the two combining modes differ. With transform: rotate(90deg) and an animation to translateX(100px), halfway through:
addappended the translation after the rotation, so it moved the element down, along the rotated x-axis.accumulatecombined the parts, so it moved it right.
add is useful for layering independent effects on the same property, like a hover wiggle on an element that's already rotated, without writing every combination into the keyframes. With the individual transform properties (article 103), you can often avoid the problem altogether by animating rotate in one animation and translate in another.

The measured offsets match the tests above: add and accumulate agree for a simple translation but split apart once a rotation is involved.
Animation events
| Event | Fires |
|---|---|
animationstart | when the animation starts, after any delay |
animationiteration | at the end of each iteration except the last |
animationend | when the animation completes |
animationcancel | when it's removed or the element is hidden before it completes |
Each event has animationName, elapsedTime and pseudoElement. Events bubble, so one listener on a container can handle many elements; check animationName to tell them apart. No animationiteration events fire for iterations skipped by a negative delay: tested in Chrome, with a 1s animation, 10 iterations and a -3.5s delay, the animation started halfway through its fourth iteration.
The Web Animations API is often easier than events: el.getAnimations() returns the running CSSAnimation objects, and each has a finished promise.
el.classList.add('leaving');
await Promise.all(el.getAnimations().map(a => a.finished));
el.remove();
Chaining and pausing between iterations
To run animations one after another, list them with delays that add up:
.hero { animation: slide-in 0.5s ease-out both, glow 0.8s ease-in-out 0.5s both; } /* glow starts when slide-in ends */
There's no "pause between iterations" property. Build the pause into the keyframes: for a 1s movement every 4s, use a 4s duration and do the motion in the first 25%:
@keyframes nudge {
0%, 25%, 100% { translate: 0 0; } /* the last 75% is the pause */
12.5% { translate: 0.4rem 0; }
}
.hint { animation: nudge 4s ease-in-out infinite; }
Restarting and interrupting
- Changing
animation-name, or removing and re-adding the animation, restarts it from the beginning, including its delay. - Changing other properties, like
animation-duration, updates the running animation in place (article 109). - Setting
display: noneon the element or an ancestor cancels the animation and firesanimationcancel. Making it visible again starts the animation over. animation-play-state: pausedfreezes it, including during its delay, andrunningcontinues it.
Unlike transitions, animations don't reverse smoothly when their trigger ends: removing a hover animation snaps back to the normal style. For effects tied to a state, like hover or open, transitions are usually better (articles 106 and 108). Use animations for things that play by themselves.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.ctrl { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; }
select, button { font: inherit; }
.lane { margin-block: 0.6rem; padding-block: 0.3rem; border-block: 1px dashed #d8cfc0; }
.box {
inline-size: 9rem; padding: 0.4rem; border-radius: 6px; color: white; background: #8a3b12;
opacity: 0.35; /* the normal style, shown with fill: none */
}
.box.run { animation: slide 1s ease-in 1s 2 var(--dir, normal) var(--fill, both); }
@keyframes slide {
from { translate: 0 0; opacity: 1; background: #1d3557; }
to { translate: 9rem 0; opacity: 1; background: #2f7d32; }
}
.log { color: #555; min-block-size: 2lh; }
.pair { display: flex; gap: 2.5rem; padding: 1rem 0 0.5rem 1rem; }
.tile {
inline-size: 4.5rem; padding: 0.6rem 0; text-align: center; border-radius: 6px; color: white; background: #1d3557;
transform: rotate(20deg);
animation: wiggle 1.2s ease-in-out infinite alternate;
}
.replace { animation-composition: replace; } /* the 20° rotation is lost while it plays */
.add { animation-composition: add; } /* the wiggle is added to the 20° rotation */
@keyframes wiggle { from { transform: rotate(-8deg); } to { transform: rotate(8deg); } }
@media (prefers-reduced-motion: reduce) { .tile { animation: none; } }
Try each fill mode. With none, the box stays faded during the 1s delay and snaps back at the end; with both, it waits at the first keyframe and stays at the last. alternate returns the box to the start after two iterations. The events appear below as they fire. In the bottom row, the replace tile loses its 20° tilt while it wiggles, and the add tile wiggles around it. Tested in Chrome, halfway through the wiggle, the replace tile wasn't rotated at all, and the add tile was at 20°.
What's current
- Directions, fill modes and animation events: all browsers.
animation-composition: all browsers, since Safari 16 and Firefox 115.
Common questions
Why does my element jump back after the animation?
The default fill mode is none. Use forwards or both, or set the final values as the element's normal style.
Why does my delayed animation flash its final state first?
During the delay, the element shows its normal style. Use animation-fill-mode: backwards or both.
How do I add a pause between iterations? Make the duration longer and leave the extra time in the keyframes without changes.
How do I know when an animation ends?
Listen for animationend, checking animationName, or await animation.finished from getAnimations().
Summary
animation-directionplays forward, backward or alternating; backward plays the timing function backward too.animation-fill-modeapplies the first keyframe during the delay (backwards), keeps the last after the end (forwards), or both.animation-composition: addoraccumulatecombines an animation with the underlying value instead of replacing it.animationstart,animationiteration,animationendandanimationcancelreport progress;getAnimations()gives promises.- Chain animations with delays, build pauses into keyframes, and use transitions for effects that must reverse smoothly.
Practice questions
A list fades items in one after another, using animation-delay. Each item is visible before its animation starts, then disappears and fades in. Fix it.
Answer
Add animation-fill-mode: backwards, or both, so the first keyframe, opacity: 0, applies during the delay:
.item { animation: fade-in 0.3s ease-out both; }
With animation: grow 1s 2 alternate forwards, does the element end at the 0% or the 100% keyframe?
Answer
The 0% keyframe. The first iteration plays forward, the second backward, so the last values played are those of 0%, and forwards keeps them. Tested in Chrome: animating a width from 0 to 100px this way, it ended at 0px.
An icon is already rotated with transform: rotate(45deg). A hover animation, @keyframes jiggle { 50% { transform: rotate(10deg) } }, removes the 45° tilt while it plays. Fix it without rewriting the keyframes.
Answer
.icon:hover { animation: jiggle 0.3s; animation-composition: add; }
With add, the animated rotation is applied on top of the underlying rotate(45deg).
Write an animation that shakes a field for 0.4s every 3 seconds.
Answer
@keyframes shake {
0%, 13.3%, 100% { translate: 0; }
3.3%, 10% { translate: -4px; }
6.6% { translate: 4px; }
}
.field { animation: shake 3s infinite; }
0.4s is about 13.3% of 3s, and the rest of the time is the pause.
Previous: Keyframe Animations: @keyframes and the animation Properties
Next: Animation Performance: The Rendering Pipeline, Compositing and will-change

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.