CSS propertyAnimations

animation

A shorthand that sets all the parameters of an animation based on @keyframes.

Initial value
none 0s ease 0s 1 normal none running
Inherited
No
Animatable
no
Applies to
all elements and the ::before and ::after pseudo-elements
JavaScript
element.style.animation = "pulse 1.5s infinite"
On this page

Example

#
CSS
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #333;
  border-top-color: #a78bfa;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to { rotate: 360deg; }
}
Preview

Definition and usage

#

The animation property combines the keyframes name, duration, timing function, delay, iteration count, direction, fill mode, and play state in a single declaration. A typical use: animation: pulse 1.5s ease-in-out infinite.

Unlike transitions, an animation does not need a state change. It starts as soon as the rule applies and can loop, which makes it a good fit for loading indicators, pulsing buttons, and entrance effects.

The order of values is fairly flexible, but the first time value is always the duration and the second is the delay. Separate multiple animations with commas.

Syntax

#
Syntax
animation: <name> <time> <timing-function>? <delay>? <iteration-count>? <direction>? <fill-mode>? <play-state>?;

Values

#

Values in practice

#

pulse 1.2s ease-in-out infinite

Pulsing in an endless loop.
.demo { animation: pulse 1.2s ease-in-out infinite; }

spin 2s linear infinite

A constant rotation, like a loading indicator.
.demo { animation: spin 2s linear infinite; }

bounce 0.8s ease-out infinite

Bouncing.
.demo { animation: bounce 0.8s ease-out infinite; }

spin 3s linear infinite, pulse 1s infinite

Two animations at once. Both change transform, so the last one wins.
.demo { animation: spin 3s linear infinite, pulse 1s infinite; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Limit looping animations to elements that truly need them, such as loading indicators. Constant motion is distracting.
  • Users with prefers-reduced-motion: reduce should get a version without motion or with reduced motion.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.