animation-fill-mode
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
The animation-fill-mode CSS property sets how a CSS animation applies styles to its target before and after its execution.
Try it
animation-fill-mode: none;
animation-delay: 1s;
animation-fill-mode: forwards;
animation-delay: 1s;
animation-fill-mode: backwards;
animation-delay: 1s;
animation-fill-mode: both;
animation-delay: 1s;
<section class="flex-column" id="default-example">
<div>Animation <span id="play-status"></span></div>
<div id="example-element">Select a mode to start!</div>
</section>
#example-element {
background-color: #1766aa;
color: white;
margin: auto;
margin-left: 0;
border: 5px solid #333333;
width: 150px;
height: 150px;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
#play-status {
font-weight: bold;
}
.animating {
animation: slide 1s ease-in 1;
}
@keyframes slide {
from {
background-color: orange;
color: black;
margin-left: 0;
}
to {
background-color: orange;
color: black;
margin-left: 80%;
}
}
const el = document.getElementById("example-element");
const status = document.getElementById("play-status");
function update() {
status.textContent = "delaying";
el.className = "";
window.requestAnimationFrame(() => {
window.requestAnimationFrame(() => {
el.className = "animating";
});
});
}
el.addEventListener("animationstart", () => {
status.textContent = "playing";
});
el.addEventListener("animationend", () => {
status.textContent = "finished";
});
const observer = new MutationObserver(() => {
update();
});
observer.observe(el, {
attributes: true,
attributeFilter: ["style"],
});
update();
It is often convenient to use the shorthand property animation to set all animation properties at once.
Syntax
/* Single animation */
animation-fill-mode: none;
animation-fill-mode: forwards;
animation-fill-mode: backwards;
animation-fill-mode: both;
/* Multiple animations */
animation-fill-mode: none, backwards;
animation-fill-mode: both, forwards, none;
/* Global values */
animation-fill-mode: inherit;
animation-fill-mode: initial;
animation-fill-mode: revert;
animation-fill-mode: revert-layer;
animation-fill-mode: unset;
Values
none-
The animation will not apply any styles to the target when it's not executing. The element will instead be displayed using any other CSS rules applied to it. This is the default value.
forwards-
The target will retain the computed values set by the last keyframe encountered during execution. The last keyframe depends on the value of
animation-directionandanimation-iteration-count:animation-directionanimation-iteration-countlast keyframe encountered normaleven or odd 100%ortoreverseeven or odd 0%orfromalternateeven 0%orfromalternateodd 100%ortoalternate-reverseeven 100%ortoalternate-reverseodd 0%orfromAnimated properties behave as if included in a set
will-changeproperty value. If a new stacking context was created during the animation, the target element retains the stacking context after the animation has finished. backwards-
The animation will apply the values defined in the first relevant keyframe as soon as it is applied to the target, and retain this during the
animation-delayperiod. The first relevant keyframe depends on the value ofanimation-direction:animation-directionfirst relevant keyframe normaloralternate0%orfromreverseoralternate-reverse100%orto both-
The animation will follow the rules for both forwards and backwards, thus extending the animation properties in both directions.
Note:
When you specify multiple comma-separated values on an animation-* property, they are applied to the animations in the order in which the animation-names appear. For situations where the number of animations and animation-* property values do not match, see Setting multiple animation property values.
Note:
animation-fill-mode has the same effect when creating CSS scroll-driven animations as it does for regular time-based animations.
Formal definition
| Initial value | none |
|---|---|
| Applies to | all elements, ::before and ::after pseudo-elements |
| Inherited | no |
| Computed value | as specified |
| Animation type | Not animatable |
Formal syntax
animation-fill-mode =
<single-animation-fill-mode>#
<single-animation-fill-mode> =
none |
forwards |
backwards |
both
Examples
>Setting fill mode
You can see the effect of animation-fill-mode in the following example. It demonstrates how you can make the animation remain in its final state rather than reverting to the original state (which is the default).
HTML
<p>Move your mouse over the gray box!</p>
<div class="demo">
<div class="grows-and-stays">This grows and stays big.</div>
<div class="grows">This just grows.</div>
</div>
CSS
.demo {
border-top: 100px solid #cccccc;
height: 300px;
}
@keyframes grow {
0% {
font-size: 0;
}
100% {
font-size: 40px;
}
}
.demo:hover .grows {
animation-name: grow;
animation-duration: 3s;
}
.demo:hover .grows-and-stays {
animation-name: grow;
animation-duration: 3s;
animation-fill-mode: forwards;
}
Result
See CSS animations for more examples.
Specifications
| Specification |
|---|
| CSS Animations Level 1> # animation-fill-mode> |
Browser compatibility
Loading…
See also
- Using CSS animations
- JavaScript
AnimationEventAPI - Other related animation properties:
animation,animation-composition,animation-delay,animation-direction,animation-duration,animation-iteration-count,animation-name,animation-play-state,animation-timeline,animation-timing-function