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"
Example
#.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; }
}<style>@keyframes spin-r { to { rotate: 360deg; } }</style>
<div style="display: flex; align-items: center; gap: 12px;">
<div style="width: 32px; height: 32px; border: 3px solid #333; border-top-color: #a78bfa; border-radius: 50%; animation: spin-r .9s linear infinite;"></div>
<span>Loading lesson…</span>
</div>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
#animation: <name> <time> <timing-function>? <delay>? <iteration-count>? <direction>? <fill-mode>? <play-state>?;Values
#pulse 1.2s ease-in-out infinite
Pulsing in an endless loop.- A constant rotation, like a loading indicator.
- Bouncing.
spin 3s linear infinite, pulse 1s infinite
two animations
Two animations at once. Both change transform, so the last one wins.
Values in practice
#pulse 1.2s ease-in-out infinite
.demo { animation: pulse 1.2s ease-in-out infinite; }spin 2s linear infinite
.demo { animation: spin 2s linear infinite; }bounce 0.8s ease-out infinite
.demo { animation: bounce 0.8s ease-out infinite; }spin 3s linear infinite, pulse 1s infinite
.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: reduceshould get a version without motion or with reduced motion.
Related entries
#- transition vs animationtransition animates a change between two states after a trigger, e.g. :hover. animation plays back steps from @keyframes on its own, even in a loop.
- @keyframesDefines the stages of an animation, known as keyframes.
- animation-nameSpecifies which @keyframes an element plays.
- animation-durationSets how long one animation cycle takes.
- transitionA shorthand that makes CSS value changes happen smoothly instead of abruptly.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.