CSS Reference ยท Transforms, Transitions and Animations
The animation-fill-mode property
Reference for the CSS animation-fill-mode property: none, forwards, backwards and both, with a live example showing styles before and after an animation.
The animation-fill-mode property controls what an element looks like outside the active part of its animation: during the animation-delay before it starts, and after it finishes. By default an animation has no effect in those periods, so the element snaps back to its normal styles the moment the animation ends.
Use forwards to keep the final keyframe, backwards to apply the first keyframe during the delay, or both to do both.
.toast { animation: slide-in 400ms ease-out 1s; animation-fill-mode: both; }
Try it
During the delay, the backwards and both boxes already sit at the first keyframe (orange, shifted right). After the end, only forwards and both stay at the last keyframe (green); the others jump back to grey at the left.
Syntax
animation-fill-mode: none | forwards | backwards | both; /* one value per animation name */ animation-fill-mode: none, both;
- Initial value:
none - Applies to: all elements,
::beforeand::after - Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
none |
The animation affects the element only while it is running. |
forwards |
After the animation ends, the element keeps the values from the last keyframe that was reached. |
backwards |
During the delay, the element takes the values of the first keyframe (as determined by animation-direction). |
both |
Applies both forwards and backwards. |
Usage notes
- "Last keyframe" depends on direction and count. With
forwards, the retained state is the end of the final iteration. Withreverseor an even number ofalternateiterations, that is the 0% keyframe, not 100%. - The usual choice is
bothfor entrance animations with a delay: the element is already in its starting pose while it waits and stays in its final pose afterwards. - Fill is not a real style change. The retained values come from the animation, not from your normal rules, and they sit at the animation level of the cascade. Editing the element's regular styles will not override them unless you remove the animation.
- Staggered lists need
backwards. If items have increasing delays and start invisible in the keyframes, withoutbackwardsthey would flash visible until their turn. - Zero-length effects. If an animation has a duration of
0s,forwardsstill applies the end state, which is a handy way to set styles through the animation layer. - Many stuck animations. Filled animations stay alive and keep being tracked by the browser. For something you only need to be static, put the final values in normal CSS instead.
Browser support
Supported in every current browser.
Related
animation-delayโ the wait before the runanimation-directionโ which keyframe counts as first or lastanimationโ the shorthand@keyframes- Lesson: CSS Animations