CSS Reference ยท Transforms, Transitions and Animations
The animation property
Reference for the CSS animation shorthand: name, duration, easing, delay, iterations, direction, fill mode and play state, with syntax, examples and support.
The animation property runs a keyframe animation on an element. You describe the stages of the animation in an @keyframes rule, then attach it to an element and say how long it lasts, how it repeats and what it does before and after.
Unlike a transition, an animation does not need a state change to start: it can begin when the page loads, repeat forever, and pass through any number of steps. The animation property is a shorthand for the eight longhands listed under Values.
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.2); } } .badge { animation: pulse 800ms ease-in-out infinite alternate; }
Try it
The fade shape waits 0.3 seconds (the delay), then fades out and back (alternate) forever (infinite).
Syntax
animation: <single-animation>#
<single-animation> =
<duration> || <easing-function> || <delay> || <iteration-count> ||
<direction> || <fill-mode> || <play-state> || [ none | <keyframes-name> ]
- Initial value:
none 0s ease 0s 1 normal none running(andautofor the timeline) - Applies to: all elements, and the
::beforeand::afterpseudo-elements - Inherited: no
- Animatable: no
- Computed value: as specified, for each longhand
Values
| Part | Longhand | Description |
|---|---|---|
| Keyframes name | animation-name |
Which @keyframes rule to run, or none. |
| First time | animation-duration |
How long one cycle takes. |
| Easing | animation-timing-function |
The speed curve between keyframes. |
| Second time | animation-delay |
Wait before the first cycle begins. |
Number or infinite |
animation-iteration-count |
How many times it repeats. |
normal, reverse, alternate, alternate-reverse |
animation-direction |
Which way each cycle plays. |
none, forwards, backwards, both |
animation-fill-mode |
Styles applied before and after the run. |
running / paused |
animation-play-state |
Whether the animation advances. |
Several animations can be separated by commas: animation: spin 2s linear infinite, fade 1s alternate infinite.
Usage notes
- Time order matters. The first time value is the duration, the second is the delay. All other parts may appear in any order.
- Keyword clashes. An animation named like a keyword (
ease,infinite,none...) can be confused with the other parts. Avoid such names. - The shorthand resets everything. Any longhand you leave out returns to its initial value. Because it also resets the animation timeline, put
animation-timelineandanimation-rangedeclarations after the shorthand if you use scroll-driven animations. - No duration, no animation. The default duration is zero, so
animation: spin infinitedoes not visibly play. - Transform and opacity animate smoothly on most devices. Animating layout properties such as
widthortopis more expensive. - Pausing and fill. Use
animation-fill-mode: forwardsto keep the end state when the animation finishes; otherwise the element jumps back to its normal style. - Events.
animationstart,animationiteration,animationendandanimationcancelfire on the element. - Accessibility. Continuous motion can hurt people with vestibular disorders. Respect
prefers-reduced-motion, and offer a way to pause anything that loops for more than a few seconds.
Browser support
Supported in every browser. The scroll-driven pieces (animation-timeline, animation-range) are newer; check a compatibility table.
Related
@keyframesโ defines the stepstransitionโ simple A to B changesanimation-name,animation-duration,animation-timing-function,animation-delay,animation-iteration-count- Lessons: Animations, Scroll-driven Animations