With CSS set the element to retain the style values that is set by the first keyframe

The CSS animation-fill-mode property with the backwards value makes an element retain the style values defined in the first keyframe (the from or 0% state) before the animation begins. This is particularly useful when you want the element to appear in its starting animation state during any animation delay.

Syntax

selector {
    animation-fill-mode: backwards;
}

Example

The following example demonstrates how animation-fill-mode: backwards applies the first keyframe styles before the animation starts −

<!DOCTYPE html>
<html>
<head>
<style>
    .box {
        width: 150px;
        height: 100px;
        background: red;
        position: relative;
        animation-name: moveBox;
        animation-duration: 2s;
        animation-delay: 1s;
        animation-fill-mode: backwards;
        margin: 50px;
    }
    
    @keyframes moveBox {
        from {
            left: 0px;
            background-color: green;
        }
        to {
            left: 200px;
            background-color: blue;
        }
    }
</style>
</head>
<body>
    <div class="box"></div>
</body>
</html>
A green box appears immediately (applying the first keyframe styles), waits for 1 second due to the delay, then animates from green to blue while moving from left 0px to left 200px over 2 seconds.

Key Points

When using animation-fill-mode: backwards:

  • The element adopts styles from the first keyframe (from or 0%) before animation starts
  • This is most noticeable when there's an animation-delay
  • Without backwards, the element would keep its original styles during the delay period

Conclusion

The animation-fill-mode: backwards property ensures smooth visual continuity by applying first keyframe styles immediately. This creates a more polished animation experience, especially when delays are involved.

Updated on: 2026-03-15T12:31:21+05:30

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements