animation-fill-mode CSS property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

The animation-fill-mode CSS property sets how a CSS animation applies styles to its target before and after its execution.

It is often convenient to use the shorthand property animation to set all animation properties at once.

Try it

animation-fill-mode: none;
animation-delay: 1s;
animation-fill-mode: forwards;
animation-delay: 1s;
animation-fill-mode: backwards;
animation-delay: 1s;
animation-fill-mode: both;
animation-delay: 1s;
<section class="flex-column" id="default-example">
  <div>Animation <span id="play-status"></span></div>
  <div id="example-element">Select a mode to start!</div>
</section>
#example-element {
  background-color: #1766aa;
  color: white;
  margin: auto;
  margin-left: 0;
  border: 5px solid #333333;
  width: 150px;
  height: 150px;
  border-radius: 50%;

  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

#play-status {
  font-weight: bold;
}

.animating {
  animation: slide 1s ease-in 1;
}

@keyframes slide {
  from {
    background-color: orange;
    color: black;
    margin-left: 0;
  }
  to {
    background-color: orange;
    color: black;
    margin-left: 80%;
  }
}
const el = document.getElementById("example-element");
const status = document.getElementById("play-status");

function update() {
  status.textContent = "delaying";
  el.className = "";
  window.requestAnimationFrame(() => {
    window.requestAnimationFrame(() => {
      el.className = "animating";
    });
  });
}

el.addEventListener("animationstart", () => {
  status.textContent = "playing";
});

el.addEventListener("animationend", () => {
  status.textContent = "finished";
});

const observer = new MutationObserver(() => {
  update();
});

observer.observe(el, {
  attributes: true,
  attributeFilter: ["style"],
});

update();

Syntax

css
/* Single animation */
animation-fill-mode: none;
animation-fill-mode: forwards;
animation-fill-mode: backwards;
animation-fill-mode: both;

/* Multiple animations */
animation-fill-mode: none, backwards;
animation-fill-mode: both, forwards, none;

/* Global values */
animation-fill-mode: inherit;
animation-fill-mode: initial;
animation-fill-mode: revert;
animation-fill-mode: revert-layer;
animation-fill-mode: unset;

Values

none

The animation will not apply any styles to the target when it's not executing. The element will instead be displayed using any other CSS rules applied to it. This is the default value.

forwards

The target will retain the computed values set by the last keyframe encountered during execution. The last keyframe depends on the value of animation-direction and animation-iteration-count:

animation-direction animation-iteration-count last keyframe encountered
normal even or odd 100% or to
reverse even or odd 0% or from
alternate even 0% or from
alternate odd 100% or to
alternate-reverse even 100% or to
alternate-reverse odd 0% or from

Animated properties behave as if included in a set will-change property value. If a new stacking context was created during the animation, the target element retains the stacking context after the animation has finished.

backwards

The animation will apply the values defined in the first relevant keyframe as soon as it is applied to the target, and retain this during the animation-delay period. The first relevant keyframe depends on the value of animation-direction:

animation-direction first relevant keyframe
normal or alternate 0% or from
reverse or alternate-reverse 100% or to
both

The animation will follow the rules for both forwards and backwards, thus extending the animation properties in both directions.

Note: When you specify multiple comma-separated values on an animation-* property, they are applied to the animations in the order in which the animation-names appear. For situations where the number of animations and animation-* property values do not match, see Setting multiple animation property values.

Note: animation-fill-mode has the same effect when creating CSS scroll-driven animations as it does for regular time-based animations.

Formal definition

Initial valuenone
Applies toall elements, ::before and ::after pseudo-elements
Inheritedno
Computed valueas specified
Animation typeNot animatable

Formal syntax

animation-fill-mode = 
<single-animation-fill-mode>#

<single-animation-fill-mode> =
none |
forwards |
backwards |
both

Examples

Setting fill mode

You can see the effect of animation-fill-mode in the following example. It demonstrates how you can make the animation remain in its final state rather than reverting to the original state (which is the default).

HTML

html
<p>Move your mouse over the gray box!</p>
<div class="demo">
  <div class="grows-and-stays">This grows and stays big.</div>
  <div class="grows">This just grows.</div>
</div>

CSS

css
.demo {
  border-top: 100px solid #cccccc;
  height: 300px;
}

@keyframes grow {
  0% {
    font-size: 0;
  }
  100% {
    font-size: 40px;
  }
}

.demo:hover .grows {
  animation-name: grow;
  animation-duration: 3s;
}

.demo:hover .grows-and-stays {
  animation-name: grow;
  animation-duration: 3s;
  animation-fill-mode: forwards;
}

Result

See CSS animations for more examples.

Specifications

Specification
CSS Animations Level 1
# animation-fill-mode

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
animation-fill-mode
Chrome – Full support
Chrome 3 (Release date: 2009-09-15)
prefix
prefix Implemented with the vendor prefix: -webkit-
Chrome – Full support
Chrome 43 (Release date: 2015-05-19)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
prefix
prefix Implemented with the vendor prefix: -webkit-
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 5 (Release date: 2011-06-21)
prefix
prefix Implemented with the vendor prefix: -moz-
Firefox – Full support
Firefox 49 (Release date: 2016-09-20)
prefix
prefix Implemented with the vendor prefix: -webkit-
Firefox – Full support
Firefox 16 (Release date: 2012-10-09)
footnote Full support
Opera – No support
Opera 12 – 12.1 (Release date: 2012-06-14)
prefix
prefix Implemented with the vendor prefix: -o-
Opera – No support
Opera 12.1 – 12.1 (Release date: 2012-11-20)
unknown Support unknown
Opera – Full support
Opera 15 (Release date: 2013-07-02)
prefix
prefix Implemented with the vendor prefix: -webkit-
Opera – Full support
Opera 30 (Release date: 2015-06-09)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
prefix
prefix Implemented with the vendor prefix: -webkit-
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
prefix
prefix Implemented with the vendor prefix: -webkit-
Chrome Android – Full support
Chrome Android 43 (Release date: 2015-05-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 5 (Release date: 2011-06-21)
prefix
prefix Implemented with the vendor prefix: -moz-
Firefox for Android – Full support
Firefox for Android 49 (Release date: 2016-09-20)
prefix
prefix Implemented with the vendor prefix: -webkit-
Firefox for Android – Full support
Firefox for Android 16 (Release date: 2012-10-09)
footnote Full support
Opera Android – No support
Opera Android 12 – 12.1 (Release date: 2012-02-25)
prefix
prefix Implemented with the vendor prefix: -o-
Opera Android – No support
Opera Android 12.1 – 12.1 (Release date: 2012-10-09)
unknown Support unknown
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
prefix
prefix Implemented with the vendor prefix: -webkit-
Opera Android – Full support
Opera Android 30 (Release date: 2015-06-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4 (Release date: 2010-06-21)
prefix
prefix Implemented with the vendor prefix: -webkit-
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
prefix
prefix Implemented with the vendor prefix: -webkit-
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
prefix
prefix Implemented with the vendor prefix: -webkit-
WebView Android – Full support
WebView Android 43 (Release date: 2015-05-27)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4 (Release date: 2010-06-21)
prefix
prefix Implemented with the vendor prefix: -webkit-
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
backwards
Chrome – Full support
Chrome 3 (Release date: 2009-09-15)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 5 (Release date: 2011-06-21)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 5 (Release date: 2011-06-21)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
both
Chrome – Full support
Chrome 3 (Release date: 2009-09-15)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 5 (Release date: 2011-06-21)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 5 (Release date: 2011-06-21)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
forwards
Chrome – Full support
Chrome 3 (Release date: 2009-09-15)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 5 (Release date: 2011-06-21)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 5 (Release date: 2011-06-21)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
none
Chrome – Full support
Chrome 3 (Release date: 2009-09-15)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 5 (Release date: 2011-06-21)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 5 (Release date: 2011-06-21)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also