Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท At-rules

The @keyframes at-rule

Reference for the CSS @keyframes at-rule: name an animation, define from/to and percentage keyframes, and drive it with the animation properties.

The @keyframes at-rule defines the steps of an animation. It gives the animation a name and lists keyframes: the style an element should have at certain points of the timeline. The browser interpolates the values in between.

A @keyframes block does nothing alone. An element must reference its name through the animation shorthand (or animation-name) and set a duration.

CSS
@keyframes pulse {
  from { transform: scale(1); }
  to   { transform: scale(1.2); }
}

.dot { animation: pulse 1s ease-in-out infinite alternate; }

Try it

A bouncing, colour-changing box

Syntax

CSS
@keyframes <name> {
  from | to | <percentage> [, <percentage>]* {
    /* declarations */
  }
}

/* using it */
animation: <name> <duration> <timing> <delay> <count> <direction> <fill-mode> <play-state>;
  • Name: a custom identifier, or a quoted string. none and the CSS-wide keywords are not allowed.
  • Selectors: from equals 0%, to equals 100%; several offsets may share a block (0%, 100% { }).
  • Missing endpoints: if there is no 0% or 100%, the browser uses the element's own style there.
  • Timing within a block: animation-timing-function can be set inside a keyframe and then applies from that keyframe to the next one.

Values

Syntax and arguments Description
from { ... } The starting state (0%).
to { ... } The ending state (100%).
35% { ... } A state at a point in the animation.
0%, 100% { ... } The same state at several points.
animation-fill-mode: forwards Keeps the last keyframe after the animation ends.
animation-direction: alternate Plays forward then backward on each repeat.

Usage notes

  • Animate transform and opacity for smooth results; these usually run on the compositor without re-layout. Animating width, top or box-shadow is more expensive.
  • Respect motion preferences. Disable large or looping motion in @media (prefers-reduced-motion: reduce).
  • Invalid declarations are ignored inside keyframes, and !important declarations there are dropped. Properties that cannot be interpolated change abruptly at the halfway point (or per steps()).
  • Later definitions win. If two @keyframes share a name, the last one in the cascade is used.
  • Custom properties in keyframes only animate smoothly if registered with @property.
  • Scroll-driven animations use the same @keyframes but attach them to a scroll timeline with animation-timeline.
  • Name collisions. Prefix names in large projects (card-fade-in).

Browser support

Supported in every browser; the unprefixed form has been available for many years.