animation-fill-mode

Term side of the Card.
Topic: Animations
Unit title: animation-fill-mode
Categories: Property
Explanation side of the Card

Explanation: The property that determines what styles are applied to the element before and after the animation runs. It can make the element keep its animation styles after the animation ends or reset to its original state.

Browser compatibility:
  • The chrome browser supports this feature starting from version 43.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 16.
  • The safari browser supports this feature starting from version 9.
Availability: 

Example

Wrongcss
.notice {
  animation: fade-out 0.5s;
}
@keyframes fade-out {
  to { opacity: 0; }
}
Rightcss
.notice {
  animation: fade-out 0.5s forwards;
}
@keyframes fade-out {
  to { opacity: 0; }
}

With 'forwards', the element keeps the last keyframe instead of popping back to full opacity.

Common mistakes

  • The element jumps back to its original style when the animation ends.
  • Hover styles stop working because a 'forwards' animation still overrides the property.

Key points

  • Entry animations with a delay use 'backwards' or 'both', so the first keyframe applies during the delay.
  • 'forwards' isn't used for state that other styles must change later, since the filled value overrides them.

Interview questions

What is the difference between forwards, backwards and both?

'forwards' keeps the last keyframe after the end, 'backwards' applies the first keyframe during the delay, and 'both' does both.

Why can't I change a property after a forwards animation?

The filled animation value still overrides normal styles. Remove the animation, or set the final value in the base styles instead.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy