CSS animations

Quick reference for CSS animations: @keyframes, default timing, and why animation-fill-mode does not commit styles into the author cascade.

リファレンスページの本文は現在英語です。

CSS animations apply @keyframes over a named animation. The initial animation-duration is 0s, so a name without a duration does not run over time. The initial timing function is ease, the iteration count is 1, and the fill mode is none. Those defaults are easy to miss when a shorthand looks complete but omits the duration.

animation-fill-mode can keep the 0% or 100% keyframe styles in the delay and after phases of that animation. That fill is still the animation’s effect. It is not a way to write those declarations into the element’s author style. If the animation name is removed, or the animation is otherwise cancelled, the filled values go with it.

Examples are complete HTML and CSS fragments. They were parsed with PostCSS and a selector AST parser for syntax and structure. They were not rendered in a browser here, and this is not a visual timing test. Decorative motion should also honour prefers-reduced-motion.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Keyframes and default timing

animation-name points at an @keyframes rule. Selectors inside the at-rule are from, to, or percentages. from is 0%; to is 100%. The shorthand is animation: name duration timing-function delay iteration-count direction fill-mode play-state. Because duration’s initial value is 0s, the duration must be present for a time-based animation.

The initial timing function is ease, not linear, and the animation runs once unless animation-iteration-count is changed. animation-play-state: paused can freeze an animation; it is not the same as fill-mode.

Syntax or propertyInitial / defaultInheritedApplies toNotes
animation-namenonenoall elementsThe @keyframes name. none means no animation.
animation-duration0snoall elementsHow long one cycle takes. The initial 0s produces no time-based animation.
animation-timing-functioneasenoall elementsProgress of each cycle. The default is ease.
animation-iteration-count1noall elementsHow many cycles run. infinite repeats until the animation is otherwise stopped.
animation-delay / direction / play-state0s / normal / runningno—A start delay, alternate or reverse direction, and whether the animation is running or paused.
animationsee longhandsnoall elementsShorthand for the longhands. A bare name with no time still uses the 0s duration.

A name still needs a duration

.label {
  animation-name: fade-in;
  animation-duration: 0.4s;
  animation-timing-function: ease;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

The animation uses the default ease timing and a single iteration. The 0.4s duration is required; the initial duration is 0s.

  • from and to are the 0% and 100% keyframes. The parser records both.
html
<p class="label">Status</p>
<style>
.label { font-family: sans-serif; color: #0B5853; animation-name: fade-in; animation-duration: 0.4s; animation-timing-function: ease; }
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
</style>

What this markup means

animation-name: fade-in is not enough on its own: animation-duration: 0.4s overrides the initial 0s so the fade can run. animation-timing-function: ease matches the initial value and is written here to make that default visible. There is no fill-mode in this rule, so it stays at the initial none: after the 0.4s the animation’s effect ends and the element’s underlying styles apply again. For decorative fades, also provide a prefers-reduced-motion alternative on the media queries page.

Related: Media queries (reduced motion), Transitions

MDN documentation → · CSS specification →

fill-mode is a temporary fill, not a committed style

animation-fill-mode: none (the default) applies keyframe values only while the animation is in its active interval. backwards applies the 0% values during a delay; forwards keeps the 100% values after the last cycle; both does both. Those filled values remain animation effects. They do not become ordinary author declarations, and they lose to a more specific author rule only according to the cascade’s animation-related comparison — they are not “written in” to the stylesheet.

A visible flash often comes from a mismatch between the underlying style and the first keyframe, or from a delay with fill-mode: none, so the un-animated style shows first. Align the 0% keyframe with the pre-animation style, or use backwards or both when a delay would otherwise expose that snap. Removing animation-name later also drops forwards styles; use a real author declaration if the end state must persist as normal CSS.

Syntax or propertyInitial / defaultInheritedApplies toNotes
animation-fill-modenonenoall elementsnone, forwards, backwards, or both. Forwards and both keep the last keyframe only for the after phase of this animation; they do not commit that keyframe as an author style.

forwards keeps the last keyframe only while the animation remains

.label {
  animation: fade-in 0.4s ease both;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

both applies the from style before the active interval and the to style afterwards. Those values last only as long as this animation is applied.

  • both is used to avoid a start-frame snap during any delay and to keep the final opacity while the animation is still attached. It is not a substitute for color: or opacity: in the author stylesheet.
html
<p class="label">Status</p>
<style>
.label { font-family: sans-serif; color: #0B5853; animation: fade-in 0.4s ease both; }
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
</style>

What this markup means

The shorthand sets a 0.4s ease animation and fill-mode: both. During a delay, if one were added, the from opacity would already apply, which avoids a flash of the underlying style. After the cycle, forwards-style filling keeps opacity: 1 while the animation is still in effect. If a later rule sets animation-name: none, that filled opacity is not left behind as an author declaration — the animation’s effect is gone. Persist an end state with a normal property in the stylesheet, not by relying on fill-mode alone.

Related: Colors and opacity

MDN documentation → · CSS specification →