Keyframe animations
How @keyframes and the animation properties describe a self-running sequence, including fill modes, direction, negative delays and animating custom properties.
Keyframe animations
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `@keyframes` defines a named timeline of styles. The `animation` properties attach it to an element and say how long it runs, how often, and in which direction.
- Keyframes you leave out are filled in from the element's normal style. `to { transform: rotate(360deg) }` alone is enough for a spinner, because the missing `from` is the element as it already is.
- `animation-fill-mode` decides what the element looks like before the animation starts and after it ends. Without `forwards` or `both`, the element snaps back to its normal style at the end.
- A negative `animation-delay` starts the animation part-way through its timeline, which is how you stagger many copies without anyone waiting.
- Register a custom property with `@property` and you can animate things CSS otherwise cannot, such as the angle in a gradient.
When a transition is not enough
A transition goes from state A to state B when something changes. It cannot start by itself, it cannot loop, and it cannot visit a third stop on the way. Loading spinners, attention pulses, an element that slides in on page load: none of those have a “before” and “after” triggered by the user.
A keyframe animation is a small timeline you write down. You name it with @keyframes, list what the element should look like at points along it, and attach it with the animation properties.
@keyframes pulse {
from { scale: 1; }
50% { scale: 1.08; }
to { scale: 1; }
}
.badge {
animation: pulse 1.5s ease-in-out infinite;
}
The percentages are positions along the timeline, from is 0% and to is 100%.
What the properties do
animation is a shorthand for eight longhands. The ones you will actually reach for:
animation-name: which@keyframesto use.animation-duration: the length of one iteration.animation-timing-function: easing. It is applied between each pair of keyframes, not across the whole animation, which surprises people the first time alinearoverall motion has a bump in the middle.animation-delay: wait before starting. Negative values start mid-way.animation-iteration-count: a number orinfinite.animation-direction:normal,reverse,alternate,alternate-reverse.animation-fill-mode:none,forwards,backwards,both.animation-play-state:runningorpaused, handy for pausing on hover.
In the shorthand the first time value is the duration and the second is the delay.
Missing keyframes are filled in
If you omit from or to, the browser uses the element’s current style for that end. So a slide-in needs only the part that is different from where it ends up:
@keyframes slide-in {
from { translate: -100% 0; opacity: 0; }
}
.drawer { animation: slide-in 300ms ease-out; }
The element ends at whatever translate and opacity it already has. Several keyframes can also share a declaration, as in 20%, 60% { ... }, and the order you write keyframes in does not matter, because they are sorted by percentage.
Fill mode: what happens outside the run
Before the animation starts (during a delay) and after it ends, the element normally shows its ordinary style. That is why a fade-in that ends at opacity: 1 snaps back if the base style is opacity: 0.
forwardsholds the last keyframe after the end.backwardsapplies the first keyframe during the delay.bothdoes both.
Usually the cleaner fix is to put the end state in the base rule and let the keyframes describe only the journey from somewhere else, as in the slide-in above.
Negative delays and staggering
A negative animation-delay tells the animation it began in the past. A quick check in Chrome: a 1 second translateX(100px) animation with ease timing, paused with a -0.5s delay, showed the element at about 80 px, which is where ease sits at the halfway point in time. The practical use is a field of looping elements that should all look busy at once:
.dot { animation: bounce 1s infinite; }
.dot:nth-child(2) { animation-delay: -0.2s; }
.dot:nth-child(3) { animation-delay: -0.4s; }
Combining with individual transform properties
translate, rotate and scale are separate properties from transform. Animating them separately means one animation can spin while another moves, without the two overwriting each other’s transform. Where two animations do touch the same property, animation-composition: add stacks them instead of replacing.
Animating things CSS cannot see: @property
The browser can only interpolate values it knows the type of. A plain --angle custom property is a string. Registering it fixes that:
@property --angle {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
.ring {
background: conic-gradient(from var(--angle), red, blue, red);
animation: spin 4s linear infinite;
}
@keyframes spin { to { --angle: 360deg; } }
I tested in Chrome that a transition on a registered <angle> property produced the half-way value (45deg for a 0 to 90deg change at the halfway point), while the same transition on an unregistered property did not run at all. There is a full lesson on @property.
Motion and accessibility
Infinite and large animations can make some people ill and distract others. Put the decoration behind @media (prefers-reduced-motion: no-preference) or turn it off under reduce. The video covers this, and the accessible CSS lesson does too.
What the video covers
Kevin Powell’s “10 CSS animation tips and tricks” (April 2023, about 20 minutes) has chapters on: different timing functions for different states, the order of keyframes not mattering, declaring multiple keyframes with one declaration, omitting 0% and 100%, animating transform and the individual transform properties, negative animation delays, prefers-reduced-motion, using the same animation several times in one declaration, and animating custom properties with @property. Those chapters map closely onto this lesson. It is a tips video rather than a first introduction, so the fundamentals above carry that part. I could not read a transcript. His shorter “Simplifying CSS animations” (2026) is under further viewing.
Where this fits
This builds on transitions. The next lesson, animation performance, explains which properties are cheap to animate, and scroll-driven animations reuses @keyframes with scroll position as the clock.
Check yourself
A fade-in animation with `animation: fade 1s` ends, and the element jumps back to invisible. Why, and what fixes it?
By default an animation only affects the element while it runs. Once it finishes, the element reverts to its normal style, which is still `opacity: 0` if that is what the base rule says. `animation-fill-mode: forwards` (or `both`) keeps the last keyframe's values after the end. Setting the base rule to the final state and using `from` for the start also avoids the problem.
What is the difference between `animation-direction: alternate` and writing the animation with a mirrored set of keyframes?
`alternate` plays iteration one forwards, iteration two backwards, and so on, so a pulse needs only a `to` keyframe. It also reverses the timing function on the way back. It only matters when `animation-iteration-count` is above 1.
You have ten spinning dots that should look out of phase from the very first frame. How does a negative delay help?
A delay of `-0.3s` starts that dot as if it had already been running for 0.3 seconds, so it appears mid-cycle immediately. A positive delay would hold the dot still until its turn, which leaves a gap at the start.
Why can't you animate a gradient's angle by writing `@keyframes` that change a plain custom property like `--angle`?
An unregistered custom property is just a string to the browser, so there is nothing to interpolate and the value flips at 50%. Registering it with `@property --angle { syntax: '<angle>'; inherits: false; initial-value: 0deg; }` tells the browser it is an angle, and then the in-between values can be computed.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.