CSS animation-fill-mode Property
The animation-fill-mode property determines how CSS keyframe styles apply before an animation begins and after it finishes.
By default, an animation does not apply its keyframe styles outside its active period. The animation-fill-mode property changes this behavior, allowing an element to use its starting keyframe styles during a delay, retain its ending keyframe styles after completion, or both.
animation-fill-mode Syntax
selector {
animation-fill-mode: forwards;
}
Replace forwards with none, backwards, or both to change when the animation's keyframe styles apply.
.default {
animation-fill-mode: none;
}
.keep-ending {
animation-fill-mode: forwards;
}
.apply-starting {
animation-fill-mode: backwards;
}
.apply-both {
animation-fill-mode: both;
}
For multiple animations, specify a comma-separated list of fill modes.
.multiple {
animation-fill-mode: forwards, both;
}
The property also accepts the global CSS values inherit, initial, revert, revert-layer, and unset.
Property Values
| Value | Description |
|---|---|
none |
Does not apply animation keyframe styles before or after the active period. This is the initial value. |
forwards |
Retains the appropriate ending keyframe styles after the animation finishes. |
backwards |
Applies the appropriate starting keyframe styles during the animation's delay period. |
both |
Combines forwards and backwards, applying keyframe styles before and after the active period. |
The property applies to all elements, is not inherited, and has an initial value of none. Its computed value is the specified list of fill mode keywords.
The animation-fill-mode property itself is not animatable. It controls how the effects of other animated properties extend outside their active periods.
How animation-fill-mode Works
A CSS animation has an active period during which its keyframes determine the animated property values.
For a finite, time-based animation with a positive delay, there are three important stages:
- Before: The animation is waiting for its delay to finish.
- During: The animation progresses through its keyframes.
- After: The animation has completed its iterations.
Without a fill mode, keyframe styles apply only during the active period. Before and after that period, the element uses styles determined by the normal CSS cascade.
@keyframes slide {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
.box {
transform: translateX(20px);
animation: slide 2s linear 1s 1 forwards;
}
Here, the element starts at its underlying position of 20px. After a one-second delay, it animates from 0 to 100px over two seconds.
Because the fill mode is forwards, the final animated position remains in effect after the animation finishes. The underlying transform declaration is not permanently changed.
Understanding the Fill Mode Values
none
The none value allows keyframe styles to affect an element only during the animation's active period.
.box {
animation: fade-in 2s ease 1s;
animation-fill-mode: none;
}
During the one-second delay, the element uses its underlying styles. After the animation finishes, those styles apply again.
forwards
The forwards value retains the animation's ending effect after its active period.
.box {
animation: fade-in 2s ease 1s;
animation-fill-mode: forwards;
}
After the animation completes, the element continues displaying the appropriate final keyframe styles while the animation remains applied.
backwards
The backwards value applies the animation's starting effect during its delay period.
.box {
animation: fade-in 2s ease 1s;
animation-fill-mode: backwards;
}
During the one-second delay, the element uses the appropriate starting keyframe styles. After the animation finishes, it returns to its underlying styles.
both
The both value combines the behavior of backwards and forwards.
.box {
animation: fade-in 2s ease 1s;
animation-fill-mode: both;
}
The element uses its starting keyframe styles during the delay and retains its ending keyframe styles after completion.
For all four values, the animation's direction and iteration count determine which keyframe styles are considered the starting and ending effects.
Fill Mode and Animation Delay
The interaction between animation-fill-mode and animation-delay is especially useful when an element should appear in its starting animation state before playback begins.
Consider a fade-in animation with a two-second delay:
@keyframes reveal {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.message {
animation: reveal 1s ease 2s both;
}
The both fill mode keeps the message transparent during the delay, fades it in over one second, and retains its fully visible state afterward.
Without backwards or both, the message could appear in its normal state during the delay and then suddenly become transparent when the animation begins.
Fill mode does not change the duration or delay itself. It only determines which animation styles apply outside the active period.
Fill Mode and Animation Direction
The animation-direction property changes the order in which keyframes play. As a result, it also affects which keyframe styles are retained by forwards and applied by backwards.
| Direction | Starting Keyframe | Ending Keyframe |
|---|---|---|
normal |
0% |
100% |
reverse |
100% |
0% |
alternate |
0% on the first iteration |
Depends on the iteration count |
alternate-reverse |
100% on the first iteration |
Depends on the iteration count |
For example, a single reverse animation with forwards retains its 0% keyframe styles after completion.
.box {
animation: slide 2s ease 0s 1 reverse forwards;
}
When using alternating directions, the final effect depends on the number of iterations, including whether the last iteration ends partway through a cycle.
Multiple Animation Fill Modes
An element can run several animations, each with a different fill mode.
.animated-element {
animation-name: fade-in, slide-in;
animation-duration: 1s, 2s;
animation-fill-mode: forwards, both;
}
The fade-in animation retains its ending effect, while slide-in applies its starting effect during a delay and retains its ending effect after completion.
Fill mode values are matched to animations in the order specified by animation-name.
If fewer fill mode values are provided than animations, the browser repeats the list as needed.
.three-animations {
animation-name: fade, move, rotate;
animation-fill-mode: forwards, backwards;
}
Here, fade uses forwards, move uses backwards, and rotate uses forwards.
When multiple animations affect the same CSS property, their effects interact according to animation ordering and composition rules.
animation-fill-mode Example
The following example compares all four fill modes using a two-second animation with a one-second delay. Each square begins with an underlying position of 20px and animates between 0 and 100px.
<div class="fill-demo">
<div class="fill-row">
<strong>none</strong>
<div class="fill-track"><div class="fill-box none"></div></div>
</div>
<div class="fill-row">
<strong>forwards</strong>
<div class="fill-track"><div class="fill-box forwards"></div></div>
</div>
<div class="fill-row">
<strong>backwards</strong>
<div class="fill-track"><div class="fill-box backwards"></div></div>
</div>
<div class="fill-row">
<strong>both</strong>
<div class="fill-track"><div class="fill-box both"></div></div>
</div>
</div>
.fill-demo {
display: grid;
gap: 12px;
}
.fill-row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.fill-row strong {
flex: 0 0 85px;
}
.fill-track {
flex: 1 1 140px;
min-width: 0;
height: 40px;
padding: 5px;
background-color: #f1f3f5;
border: 1px solid #ccc;
box-sizing: border-box;
}
.fill-box {
width: 30px;
height: 28px;
background-color: #1769aa;
transform: translateX(20px);
animation: fill-move 2s linear 1s 1;
}
.fill-box.none {
animation-fill-mode: none;
}
.fill-box.forwards {
animation-fill-mode: forwards;
}
.fill-box.backwards {
animation-fill-mode: backwards;
}
.fill-box.both {
animation-fill-mode: both;
}
@keyframes fill-move {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
@media (prefers-reduced-motion: reduce) {
.fill-box {
animation: none;
}
}
All four animations run once and finish after three seconds, including the delay. The fill modes determine which positions the squares use before and after the active period.
During the delay, the backwards and both squares use the starting keyframe at 0, while the other squares use their underlying 20px position.
After the animation finishes, the forwards and both squares remain at 100px. The none and backwards squares return to their underlying 20px position.
The example runs once when loaded. Reload the page to observe the initial delay again. If reduced motion is enabled, all four squares remain stationary.
Fill Mode vs. Permanent Styles
The forwards and both values preserve animation effects, but they do not permanently change an element's underlying CSS declarations.
Consider an element with a normal opacity of 1:
.message {
opacity: 1;
animation: fade-out 1s ease forwards;
}
@keyframes fade-out {
to {
opacity: 0;
}
}
After the animation finishes, the message remains transparent because the animation's final effect is still applied.
However, if the animation is removed, the element returns to its underlying opacity of 1.
For a permanent interface state, such as an expanded panel or a selected tab, update the element's actual styles or state rather than relying exclusively on a finished animation.
Important Considerations
Keep the following points in mind when using animation-fill-mode:
- Default behavior: The initial value is
none, so keyframe styles do not apply outside the active period. - Positive delays: The
backwardsandbothvalues can apply starting keyframe styles during a delay. - Completed animations: The
forwardsandbothvalues retain ending keyframe styles while the animation effect remains applied. - Animation direction: The appropriate starting and ending keyframes depend on the animation direction and iteration count.
- Infinite animations: An animation that repeats indefinitely does not normally reach an after phase, so its forwards fill behavior is not visible during continuous playback.
- CSS cascade: Fill modes do not rewrite underlying styles, and other declarations or animations may affect the final displayed value.
- Shorthand declarations: The
animationshorthand can specify fill mode directly, as inanimation: fade-in 2s ease forwards;. - Accessibility: Respect
prefers-reduced-motionand ensure important content remains accessible without animation.
Use backwards when an element needs its starting animation appearance during a delay, forwards when it should retain its final appearance, and both when both behaviors are required.
Browser Support
The animation-fill-mode property has Baseline Widely available status and is supported in modern browsers, including Chrome, Edge, Firefox, and Safari.
All four fill mode values are broadly supported. Older browser versions may require vendor-prefixed animation properties.
Visit Can I Use? to check current browser compatibility.
Specifications
The animation-fill-mode property is defined in the CSS Animations Level 1 specification. Its behavior is based on the before, active, and after phases of an animation effect.
