Core points
- CSS
animation-fill-mode
attribute controls how the style is applied before and after execution of the animation. It has four possible values:none
,forwards
,backwards
,both
, and . -
forwards
Values ??ensure that after the animation is finished, the style defined in the last keyframe of the animation remains on the element instead of being restored to the original style. -
backwards
Value applies the style of the first keyframe to the element during any delay before the animation begins. -
The
both
forwards
value combines the effects ofbackwards
and , applying the style of the first keyframe before the animation starts, and retaining the style of the last keyframe after the animation ends.
Almost all front-end developers have used CSS keyframe-based animations. Some people may even create some rather complex demonstrations and experiments with this feature.
animation-fill-mode
If you need a full introduction to the topic, my post on Smashing Magazine in 2011 is still a good choice. However, in this article, I want to focus on one component of the CSS animation specification: the
animation-name
This is the only animation-based property that is not easy to understand. For example, no one will be confused about animation-duration
,
fill-mode
grammar
@keyframes
You may already know that basic keyframe animations are defined using the
.example { animation: myAnim 2s 500ms 2 normal ease-in forwards; }
Of course, the abbreviation of this same line can be expanded to:
.example { animation-name: myAnim; animation-duration: 2s; animation-delay: 500ms; animation-iteration-count: 2; animation-direction: normal; animation-timing-function: ease-in; animation-fill-mode: forwards; }
animation-fill-mode
In both examples, the forwards
attribute is defined last, and in both cases the value is set to "
animation-fill-mode
Similarly, even if you have never used CSS animations, you may be able to figure out what everything in the above statement does-except
The normative definition of fill-mode
The
attribute defines which values ??are applied outside the animation execution time.
animation-fill-mode
As it continues to explain, the time "outside execution time" specifically refers to the time between the time the animation is applied to an element and the time when the element actually starts to an animation. Basically, using the fill-mode
value, you can define how the animation element looks outside the animation execution time but after the animation is applied. It may sound confusing, but you will soon understand what I mean.
Let's expand this basic definition by looking at each possible value (which may still be a bit confusing).
Decomposition value
Theanimation-fill-mode
attribute can accept one of four values: none
, forwards
, backwards
, or both
. Here is a breakdown of each value.
Value: none
This is the initial or default value of animation-fill-mode
. Defining the none
value will be redundant unless you set it through JavaScript to change it from other values, or you are overwriting something in the cascading.
To understand what the none
value does, here is a CodePen demonstration showing an animation without animation-fill-mode
(so its value is none
):
[CodePen demo link (please replace it with the actual link)]
You can see that in most cases the fill mode of none
is not what you want. Remember that fill-mode
defines how an element looks outside the animation execution time.
In this example, the ball is initially red and then gradually turns pink while moving to the right and changing size at the same time. It would be better if the ball remains small, pink and right after the animation is over. This will prevent the ugly jump back to the starting state after the animation is over.
Value: forwards
Let's change the animation of the ball to fill-mode
value is "forwards
":
[CodePen demo link (please replace it with the actual link)]
Now you can see the benefits of using animation-fill-mode
and why the forwards
value is used more than any other value. When we define it with this value, we tell the browser that we want the animation element to retain its final style set, as defined in the last keyframe. This way, we don't jump back to the initial state before the element starts animation after the animation is over (the "Reset" button I added in the demo can do this for us).
Value: backwards
Let's change the value to backwards
—What happens now?
[CodePen demo link (please replace it with the actual link)]
Note that the behavior in this demo is exactly the same as the first demo with a animation-fill-mode
value of "none
". Why is this happening?
Contrary to forwards
, the backwards
value gives the element its style before the animation begins after the animation ends. This makes more sense when we see the specification's description of the value of backwards
:
During the period defined by
animation-delay
, the animation will apply the attribute value defined in the keyframe where the first iteration of the animation will be initiated. These are the values ??of thefrom
keyframe... or the values ??of theto
keyframe.
To demonstrate this, I made two modifications to the demonstration:
- Added a
from
keyframe to use different colors for the ball. - Add delays were added using the
animation-delay
attribute.
It's here:
[CodePen demo link (please replace it with the actual link)]
Now you can see that once you press the "Start Animation" button, the ball will turn blue. This is because of the obvious delay. Technically, each animation has a default delay, but the delay is 0s, so in this case you won't really see the value of backwards
if your style in the initial keyframe is versus before the animation starts The style is the same, and you certainly won't see it either. If using the same style and the same delay, deleting the backwards
value causes the ball to remain red before the animation starts.
To be honest, I didn't really see many practical uses of the backwards
value. It is hard to imagine that in too many cases the style in the first keyframe of an animation is different from the static state before the element animation. But if you have any ideas, I'd love to listen to some use cases.
Value: both
The last value we are going to view is the both
value. This value tells the browser to apply the effect of forwards
and backwards
.
Let's keep the same style in the demo and see what happens when we assign it to the both
value:
[CodePen demo link (please replace it with the actual link)]
Not Rocket Science. As you can see, during the initial delay, the initial keyframe style is applied, and the animation ends and the style in the last keyframe is frozen (as mentioned earlier, this is usually what you want). Again, this value would not be much different from using forwards
if it weren't for the delay and the alternative colors defined in the first keyframe.
Some precautions and tips
Based on the above content and some details in the specification, the following are some things to note:
- Although I have somewhat minimized the importance of the
backwards
andboth
values, these values ??may come in handy in complex animations controlled by scripts or user input. Use cases will abound (think: games), but only through experimentation and innovation. - When the
animation-direction
property is set toreverse
, the effects of theforwards
andbackwards
values ??are reversed. If you fiddled with the demo, you'll see how it works. - While I said
animation-fill-mode
only accepts four values, remember that you can also use CSS range attribute values. - In earlier versions of the specification,
animation-fill-mode
was not part of the abbreviation animation properties, but it seems that all browsers that support keyframes can use it for abbreviation. - When using animation abbreviation and selecting a custom name for the animation, do not use names that match the valid
fill-mode
value (such as "reverse
" or "forwards
"), otherwise the browser will parse the abbreviation declaration, assuming The name is actually afill-mode
value.
Conclusion
I hope this summary will help you understand this property better. Some research has really helped me understand it better. If you have done something unique about animation-fill-mode
, or have any unique use cases for different values, or have any corrections to this article, please let us know in the discussion.
FAQs about CSS animation fill mode properties
CSS animation-fill-mode
What is the meaning of attributes?
CSS animation-fill-mode
Properties are key aspects of CSS animation. It defines how an animation applies styles to its target before and after execution. This property helps control the intermediate state of elements during animation. It can take four values: none
, forwards
, backwards
, and both
. Each value determines how the animation affects the elements at different stages, thus giving developers greater control over the animation.
value in animation-fill-mode
forwards
work?
The CSS animation-fill-mode
value in the attribute ensures that the element retains the calculated value set for the last keyframe encountered during the animation process. This means that after the animation is over, the style defined in the last keyframe will remain on the element instead of being restored to the original style. forwards
in animation-fill-mode
? backwards
CSS Value in the animation-fill-mode
property applies the style of the first keyframe to the element during the period before the animation begins. This means that if there is a delay before the animation starts, the element will take the style defined in the first keyframe during that delay. backwards
What does the value of
in animation-fill-mode
both
mean?
The value in the animation-fill-mode
CSS both
attribute combines the effects of forwards
and backwards
. This means applying the style of the first keyframe before the animation starts, and retaining the style of the last keyframe after the animation ends.
What happens when the animation-fill-mode
property is set to none
?
When the CSS animation-fill-mode
property is set to none
, the animation does not apply any style to the element before or after it begins. Elements are affected only by animation during active operation of the animation.
Can I use multiple values ??for the animation-fill-mode
attribute?
No, CSS animation-fill-mode
attribute can only take one value at a time. You can select from none
, forwards
, backwards
or both
according to the specific requirements of the animation.
All browsers support animation-fill-mode
attributes?
CSS animation-fill-mode
Properties are widely supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. However, it is not supported in Internet Explorer.
animation-fill-mode
How do attributes interact with animation-delay
attributes?
CSS animation-fill-mode
Properties work with animation-delay
Properties. If animation-fill-mode
is set to backwards
or both
and animation-delay
is present, the style of the first keyframe is applied during the delay.
animation-fill-mode
Can attributes be used with keyframes with percentage values?
Yes, the CSS animation-fill-mode
attribute can be used with keyframes with percentage values. The forwards
and backwards
values ??will apply the style of the closest 0% and 100% of the keyframes, respectively.
animation-fill-mode
What is the default value of the attribute?
The default value of the CSS animation-fill-mode
attribute is none
. This means that by default, the animation does not apply any style to the element before or after it starts.
Please note that I have replaced the CodePen link with a placeholder. You need to provide an actual CodePen demo link to make the article complete.
The above is the detailed content of Understanding the CSS animation-fill-mode Property. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

CSS,orCascadingStyleSheets,isthepartofwebdevelopmentthatcontrolsawebpage’svisualappearance,includingcolors,fonts,spacing,andlayout.Theterm“cascading”referstohowstylesareprioritized;forexample,inlinestylesoverrideexternalstyles,andspecificselectorslik

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis
