CSS animation-fill-mode Property

CSS animation-fill-mode Property

CSS animation-fill-mode Property


The animation-fill-mode property sets a style to the element when the animation is not executing (before it starts, after it ends, or both).

The animation-fill-mode is one of the CSS3 properties.

The animation-fill-mode property is the only property that affects the element before the first @keyframe is played or after the last keyframe is played. It can assume the following values: "forwards" to specify that the element should keep the style values set by the last keyframe (depending on animation-iteration-count and animation-direction properties); "backwards" to specify that the element should get the style values set by the first keyframe (depends on animation-direction) and keep it within animation-delay period; "both" to specify that the animation should follow the rules for both "forwards" and "backwards", and "none" (default) to specify that no styles will be applied to the element before or after the animation is executed.

When multiple comma-separated values are specified for any animation property, they will be attached to the animations that are defined in animation-name differently.

Initial Valuenone
Applies toAll elements, It also applies to ::before and ::after pseudo-elements.
InheritedNo.
AnimatableNo.
VersionCSS3
DOM Syntaxobject.style.animationFillMode = "forwards";

Syntax

animation-fill-mode: none | forwards | backwards | both | initial | inherit;

Example of the animation-fill-mode property with the "forwards" value:

<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        width: 100px;
        height: 100px;
        background: #1c87c9;
        position: relative;
        -webkit-animation: element 5s;
        /* Safari 4.0 - 8.0 */
        -webkit-animation-fill-mode: forwards;
        /* Safari 4.0 - 8.0 */
        animation: element 5s;
        animation-fill-mode: forwards;
      }
      /* Safari 4.0 - 8.0 */
      @-webkit-keyframes element {
        from {
          top: 0px;
        }
        to {
          top: 200px;
          background-color: blue;
        }
      }
      @keyframes element {
        from {
          top: 0px;
        }
        to {
          top: 200px;
          background-color: #8ebf42;
        }
      }
    </style>
  </head>
  <body>
    <h2>Animation-fill-mode example</h2>
    <div></div>
  </body>
</html>

Example of the animation-fill-mode property with the "backwards" value:

<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        width: 100px;
        height: 100px;
        background: #1c87c9;
        position: relative;
        -webkit-animation: element 5s;
        /* Safari 4.0 - 8.0 */
        -webkit-animation-fill-mode: backwords;
        /* Safari 4.0 - 8.0 */
        animation: element 5s;
        animation-fill-mode: backwords;
      }
      /* Safari 4.0 - 8.0 */
      @-webkit-keyframes element {
        from {
          top: 0px;
        }
        to {
          top: 200px;
          background-color: blue;
        }
      }
      @keyframes element {
        from {
          top: 0px;
        }
        to {
          top: 200px;
          background-color: #8ebf42;
        }
      }
    </style>
  </head>
  <body>
    <h2>Animation-fill-mode example</h2>
    <div></div>
  </body>
</html>

Values

ValueDescription
noneThis is the default value. Animation will not apply any style to the element before and after it starts.
forwardsSpecifies that the element should keep the style values set by the last keyframe.
backwordsSpecifies that the element should get the style values set by the first keyframe and keep it within animation-delay period.
bothSpecifies that the animation should follow the rules for both forwards and backwards.
initialIt makes the property use its default value.
inheritIt inherits the property from its parents element.
Reactions

Post a Comment

0 Comments

close