animation-fill-mode
Decides how the element looks before the animation starts and after it ends.
- Initial value
none- Inherited
- No
- Animatable
- no
- Applies to
- all elements and the ::before and ::after pseudo-elements
- JavaScript
element.style.animationFillMode = "forwards"
Example
#.card {
animation: fade-up 0.6s ease-out 0.3s both;
}
@keyframes fade-up {
from { opacity: 0; transform: translateY(12px); }
}<style>@keyframes fade-up2 { from { opacity: 0; transform: translateY(12px); } }</style>
<div class="box" style="animation: fade-up2 .6s ease-out .3s both;">The card slides in smoothly</div>Definition and usage
#By default an animation affects the element only while it runs. Before the start (during the delay) and after the end, the element returns to its normal styles. The animation-fill-mode property lets you change that.
forwards keeps the element in the state of the last keyframe, backwards applies the first keyframe already during the delay, and both combines the two behaviors.
For entrance effects with a delay, both is usually the best choice: the element does not flash before the start and does not return to the initial state after the end.
Syntax
#animation-fill-mode: none | forwards | backwards | both;Values
#- Before the start and after the end, the element stays in its normal place.
- After the end, the element stays in the state of the last keyframe.
- During the delay, the element already sits in the state of the first keyframe.
- The first keyframe before the start and the last one after the end.
Values in practice
#none
.demo { animation-fill-mode: none; }forwards
.demo { animation-fill-mode: forwards; }backwards
.demo { animation-fill-mode: backwards; }both
.demo { animation-fill-mode: both; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Set delayed entrance effects with
both, otherwise the element will flash in its final state before the start. - Instead of a permanent
forwards, consider putting the target styles in a regular rule. They are easier to override later.
Related entries
#Spotted a mistake or missing an example? Write to us.