HTML DOM Style animationFillMode Property


The animationFillMode property is used to specify how styles are applied outside the execution time i.e. after it has finished or if a delay has been specified. This is helpful in setting css animation style to an element before the animation has started and after it has ended.

Syntax

Following is the syntax for −

Setting the animationFillMode property −

object.style.animationFillMode = "none|forwards|backwards|both|initial|inherit"

Values

Following are the values −

ValueDescription
noneThis makes that the animation will not apply any styles to the target element before start or after the animation has ended.It is the default value.
forwardsIt applies the last key-frame style to the target element after the animation has been ended.
backwardsIt applies the first key-frame style to the target element after the animation has been ended.
bothIt applies both the forwards and backwards rule to the animation
InitialFor setting this property to initial value
nheritInherits this property from its parent element.

Example

Let us look at the example for the animationFillMode property −

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   div {
      height: 30px;
      width: 30px;
      background-color: orange;
      animation: small 4s;
      animation-fill-mode: forwards;
   }
   @keyframes small {
      0% {
         width: 200px;
         height: 200px;
         background-color: white;
      }
      33% {
         background-color: green;
      }
      66% {
         background-color: violet;
      }
      100% {
         background-color: darkred;
      }
   }
</style>
<script>
   function changeFillMode(){
      document.getElementById("DIV1").style.animationFillMode="backwards";
      document.getElementById("Sample").innerHTML="The animation fillmode is now backwards";
   }
</script>
</head>
<body>
<div id="DIV1"></div>
<p>Click the below button to change the above animation fillmode property</p>
<button onclick="changeFillMode()">CHANGE FILL</button>
<p id="Sample"></p>
</body>
</html>

Output

This will produce the following output −

When the animation ends the square color is dark red as it is the last keyframe −

On clicking the CHANGE FILL the color changes to orange which is our first keyframe −

Note − This property isn’t supported in IE/EDGE and Safari browser.

Updated on: 15-Feb-2021

23 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements