animation-fill-mode 5y6946
The animation-fill-mode property defines what values are applied by an animation delay time, and after the animation has finished execution. 351c5z
By default, an animation will not affect property values between the time it is applied (the animation-iteration-count
properties). The animation-fill-mode
property can override this behavior.
A CSS animations will not affect the element you’re animating until the first keyframe has completed. This can leave you with some awkward jumps and cuts. You can use animation-fill-mode
to smooth the transitions into the animation styles and outside them, when the animation is finished playing.
You can “prepare” your element for the animation by applying the styles of the first animation animation delay time; that is, after you’ve applied the animation to the element, but before it actually starts playing. This will prevent a sudden jump from the initial state of the element to the first state in the animation, if the latter is not the same as the initial state.
You can also have the element keep the styles applied to it at the end of the animation in the last keyframe, once the animation is finished. This is usually useful to prevent a “jump” back to the initial styles. For example, if you’re animating a ball by moving it from one side to another, you may want it to remain at its new position, and not jump back to its initial position once the animation is over. This is where the animation-fill-mode
property also helps.
You can also do both of the above: apply the styles of the beginning of the animation to the element before the animation begins (in the delay time) and have the element keep the final styles applied by the animation when it’s finished. These are usually easier to understand with a live example, so make sure you check the live demo section below for a live example.
You can specify one or multiple comma-separated animation-fill-mode
values. When you provide a list of comma-separated values, this list is usually mapped to a list of animations specified using the animation-name
property.
For example, if you provide two animation-fill-mode
values, then the first value determines the fill mode of the first animation in the list of animation names provided by animation-name
, and the second fill mode specifies the fill mode of the second animation.
The animation-fill-mode
property is usually specified as part of the animation
shorthand property.
Official Syntax 55184y
- Syntax:
animation-fill-mode: none | forwards | backwards | both
- Initial: none
- Applies To: all elements; and
::after
pseudo-elements - Animatable: no
Values 584j31
- none
- The animation has no effect when it is applied but not executing.
- forwards
-
After the animation is done executing (has played the number of times specified by its
animation-direction
). If the animation didn’t complete an entire iteration (if the iteration count was 0 or a value less than 1) the values specified or implied for its ‘0%’ keyframe are used.Note that if
animation-iteration-count
is a non-integer value, the animation will stop executing partway through its animation cycle, but a forwards fill will still apply the values of the ‘100%’ keyframe, not whatever values were being applied at the time the animation stopped executing. - backwards
-
Before the animation has begun executing (during the period specified by
animation-direction
isreverse
oralternate-reverse
, the values specified or implied for its ‘100%’ keyframe are used.Note that if the animation’s first keyframe styles are the same as the element’s styles before the animation was applied, then the
backwards
value may not have a noticeable effect. - both
-
The effects of both
forwards
andbackwards
fill apply.
Examples 3e5p5z
The following example applies two different fill modes to two animations applied to an element, so that the element keeps the styles applied at the end of both animations, and the first styles applied by the first animation during its delay time.
.element { animation-name: first-animation, second-animation; animation-duration: 2s, 4s; animation-delay: 2s, 0s; animation-fill-mode: both, forwards; animation-timing-function: ease-in, steps(3, start); /* ... */ }
Live Demo 2x4j2k
In the demo, the following animation keyframes are applied:
@keyframes rotate { 0% { background-color: orange; transform: rotate(0deg); } 100% { background-color: #0098D8; transform: rotate(90deg); } }
Elements with animation-fill-mode
values of forwards
and both
will remain “hanging” down when their animation ends, and their background color will be #0098D8
which is a blue color. These are the styles specified in the last (‘100%’) keyframe.
Elements with animation-fill-mode
values backwards
and both
have an animation delay time of ‘1s’, which will allow you to see how the first keyframe styles are applied to the element before the animation begins. These elements will get an orange background color as soon as the animation is applied to them, which is the style applied in the first (‘0%’) keyframe, but they will not be rotated. Once the animation starts, they will be rotated. The element with animation-fill-mode: both
will get the orange background at the beginning and will keep the final rotation and blue background color when the animation ends.
To apply the animation to the elements, hover over their container.
View this demo on the Codrops PlaygroundBrowser k5t66
CSS Animation 4x1j4h
Complex method of animating certain properties of an element
W3C Working Draft
ed from the following versions:
Desktop 3n671n
- 43
- 16
- 10
- 12
- 9
Mobile / Tablet 4q224p
- 9.0
- 66
- No
- 66
- 60