Course contentsShow
← Web Development
Lesson 523 of 2,9829. CSS Transitions and AnimationsFree lesson

The animation-fill-mode Property

Define how styles apply before and after animation execution using fill modes.

The animation-fill-mode Property

You'll learn how to control what styles an element displays before an animation starts and after it ends using animation-fill-mode.

What Happens Before and After an Animation?

By default, CSS animations only apply styles while they're running. Once the animation finishes, the element snaps back to its original styles—as if the animation never happened. The animation-fill-mode property gives you control over this behavior, letting you decide whether the animation's styles should persist before, after, or on both sides of the execution.

Think of it like a stage performance: animation-fill-mode determines whether the actor (your element) arrives in costume before the curtain opens, exits in costume after it closes, or changes back into street clothes immediately when done.

The Four Fill Mode Values

  • none (default): Element uses its normal styles before and after the animation. The animation has no effect outside its execution time.

  • forwards: After the animation ends, the element keeps the styles from the final keyframe (100% or to). Great for animations that should stick.

  • backwards: Before the animation starts (during any animation-delay), the element applies styles from the first keyframe (0% or from).

  • both: Combines forwards and backwards—applies first keyframe styles before starting and keeps final keyframe styles after ending.

When to Use It

Use forwards when you want changes to persist (like a button that grows and stays grown). Use backwards when you have a delay and want the starting animation state visible immediately. Use both when you need control over both ends.

Key Takeaway: animation-fill-mode controls whether an element displays animation keyframe styles before the animation starts and after it finishes, preventing awkward "snapping" back to original styles.