Explain it simply
Explain it simply
A transition is a single trip from state A to state B, and it only starts when something changes. An animation is more like the frames of a cartoon: @keyframes describe how things look at moments such as 0%, 60% and 100%, and the animation starts by itself as soon as the element is rendered. It can also repeat, or play backwards.
Notifications sliding in, a spinning loader and a badge popping into view are animation territory. Whether the animation has finished or is switched off entirely, though, the content must always remain visible and usable.
Problem → Why → Solution
The problem: a delayed fade-in that flashes first
You want the hero heading to fade in 500ms after page load, so you write animation: fade-in 400ms ease-out 500ms. On load, the heading appears fully at once, vanishes at the 500ms mark, then fades back in. The design team files a bug about the flash.
.hero-title {
animation: fade-in 400ms ease-out 500ms; /* no fill-mode */
}
@keyframes fade-in {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: translateY(0); }
}
/* 0-500ms: opacity 1 (base style), then jumps to 0 and fades in */Why does it flash?
The default fill-mode is none: during the delay the keyframes do not apply yet, so the element shows its own base style (opacity: 1). When the animation starts, it jumps to the from keyframe (opacity: 0), hence the flash. Adding backwards (or both) applies the from keyframe during the delay, and both also holds the to keyframe after the end.
Core concept: a keyframe timeline and the properties that play it
@keyframes name { ... } describes the timeline. from equals 0% and to equals 100%, and you can add stops such as 30% or 60% in between. If a keyframe leaves a property out, the browser uses the element's own value, which is why a spin animation can be just to { transform: rotate(360deg) } and start from the current transform. The timing function, whether set on the element or inside a keyframe, applies to each segment between two keyframes, not to the animation as a whole.
| Property | Controls | Example |
|---|---|---|
| animation-name / -duration | Which keyframes, for how long | pop, 400ms |
| animation-timing-function / -delay | Speed curve, wait before starting | ease-out, 300ms |
| animation-iteration-count | How many runs | 3, infinite |
| animation-direction | Forwards, reverse or alternating | alternate |
| animation-fill-mode | What shows while not running | both |
| animation-play-state | Pause or resume | paused |
In the shorthand (animation: pop 400ms ease-out 300ms both), the first time value is the duration and the second is the delay. With fill-mode none, the element shows its base style before the animation starts and after it ends; forwards holds the last keyframe, backwards applies the first keyframe during the delay, and both does both. While an animation runs, its keyframe values beat every normal declaration in the cascade, however specific, though not !important ones, which is why a class cannot override a property that is currently being animated.
The most important design rule: let the base style be the final state, and let the keyframes describe only where the element comes from. Then animation: none under reduced motion simply shows the content in its finished form. If the base style is opacity: 0 and only the animation reveals it, switching the animation off hides the content. As with transitions, animating transform and opacity stays smooth because layout is left untouched.
- Delay: backwards fill shows from
- Run: interpolate between keyframes
- Repeat until iteration-count ends
- End: forwards keeps to, else base
Make the base style the final state, and turning the animation off breaks nothing.
General pattern
@keyframes name {
0% { property: start-value; }
50% { property: middle-value; }
100% { property: end-value; }
}
.element {
animation: name duration timing-function delay iteration-count direction fill-mode;
}
@media (prefers-reduced-motion: reduce) { .element { animation: none; } }Write the timeline once in @keyframes, then any element plays it by name through the animation shorthand. Several elements can share one set of keyframes with different durations.
Example: a notice entrance that respects reduced motion
Here .notice fades in while rising slightly. The base style never touches opacity or transform, so animation: none shows reduced-motion users the finished notice at once. Try it by emulating prefers-reduced-motion in the DevTools Rendering panel.
.notice {
animation: notice-in 240ms ease-out both;
}
@keyframes notice-in {
from {
opacity: 0;
transform: translateY(0.75rem);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@media (prefers-reduced-motion: reduce) {
.notice { animation: none; }
}Code breakdown
- shorthand
animation: notice-in 240ms ease-out both;Plays notice-in once over 240ms with an ease-out curve. With no delay, the backwards half of both has nothing to cover, and the forwards half holds the to keyframe, which equals the base style here. Writing both on entrance animations is still a safe habit, so adding a delay later cannot cause a flash.
- @keyframes
@keyframes notice-in {Declares the named timeline. Misspell the name in the shorthand and the animation silently does nothing.
- from
transform: translateY(0.75rem);Starts invisible and 0.75rem lower (12px at the default font size). The short distance is easier on motion-sensitive users.
- to
transform: translateY(0);The end point matches the base style, opacity 1 and no offset, so handing back to the base style after the end causes no jump.
- reduced motion
.notice { animation: none; }Removes the animation for users who asked for reduced motion. Because the base style is the final state, the notice is simply there.
The notice goes from opacity 0 to 1 and rises 12px into place over 240ms; with prefers-reduced-motion: reduce it appears immediately at opacity 1 with no animation.Animation or transition?
| Need | Use |
|---|---|
| Switch between two states on hover, focus or a class | Transition |
| Run on its own when the element appears | Animation |
| Three or more stops (a bounce, a shake) | Animation |
| Repeat (spinner, pulse) | Animation + iteration-count |
To replay an animation from JavaScript, remove the class, let the browser render a frame (for example with requestAnimationFrame), then add the class again; while animation-name stays the same, the browser will not restart it. Pairing animation-play-state: paused with :hover lets users pause moving content, such as a ticker, while they read it.
In real projects
Where this shows up in real projects
In production apps, CSS animations show up most in loading states: spinning loaders, the shimmer on skeleton screens shown before content arrives, toasts sliding in and modal dialogs appearing. Many animation libraries, Animate.css for example, ship @keyframes behind class names, so the mechanism underneath is exactly what this lesson covers.
Accessibility guidelines ask that moving content which starts automatically and lasts more than five seconds can be paused by the user, so infinite loops are kept for genuine status indicators such as spinners. Removing a spinner under reduced motion would hide the fact that something is loading, so teams slow the rotation down or replace it with "Loading…" text instead.
Common mistakes
Common mistakes
1. Content that only an animation reveals
With .card { opacity: 0; animation: show 300ms forwards; }, a reduced-motion block that sets animation: none leaves every card invisible, and so does a typo in the keyframes name. Keep the base style at opacity: 1, put opacity: 0 in the from keyframe, and use fill-mode both.
2. Hiding with forwards and leaving the element behind
Fading a toast out with forwards to opacity 0 makes it invisible, but it still takes up space, still catches clicks and can still be read by screen readers. Remove it from the DOM, or add the hidden attribute, on the animationend event.
3. Infinite decorative loops
A call-to-action button that pulses forever with animation: pulse 1s infinite keeps pulling attention away from reading and troubles motion-sensitive users. Limit iteration-count to something like 2 or 3.
Best practices
Best practices
- Keep the base style as the final state and write entrance animations with fill-mode both.
- Animate mainly transform and opacity inside keyframes.
- Reserve infinite loops for status indicators, and slow them or make them static under reduced motion.
Mini challenge
Mini challenge: a pop-in badge and a spinner
Animate an inbox badge that pops in after a delay and a loading spinner that keeps turning, then add fallbacks for reduced-motion users.
- pop keyframes: scale(0) and opacity 0 at 0%, scale(1.2) at 60%, scale(1) at 100%
- .badge runs 400ms ease-out after a 300ms delay and stays hidden during the delay (no flash)
- .spinner turns once per second, linear, forever
- Under reduced motion, show the badge at once with no animation and slow the spinner to one turn per 3 seconds
<style>
/* TODO: .badge - pill shape, crimson background, white text,
plays "pop" for 400ms ease-out after 300ms, hidden during the delay */
/* TODO: @keyframes pop - grow from nothing, overshoot at 60%, settle */
/* TODO: .spinner - 24px ring with one blue edge, turning forever */
/* TODO: @keyframes spin - one full turn */
/* TODO: reduced motion - no badge animation, a much slower spinner */
</style>
<p>Inbox <span class="badge">3</span></p>
<p><span class="spinner" role="status" aria-label="Loading"></span></p>Expected: For the first 300ms the badge is invisible at scale(0) and opacity 0, then it grows to 1.2 and settles at 1. The spinner turns once per second, forever. With reduced motion, the badge appears instantly at opacity 1 and the spinner turns three times more slowly.
Show hint
fill-mode both (or backwards) keeps the badge hidden during the delay. For spin, to { transform: rotate(360deg); } on its own is enough. Under reduced motion, change only animation-duration on the spinner; the name and iteration count stay.
Show solution
<style>
.badge {
display: inline-block;
padding: 2px 8px;
border-radius: 999px;
background: crimson;
color: white;
animation: pop 400ms ease-out 300ms both;
}
@keyframes pop {
0% { transform: scale(0); opacity: 0; }
60% { transform: scale(1.2); opacity: 1; }
100% { transform: scale(1); }
}
.spinner {
display: inline-block;
width: 24px;
height: 24px;
border: 3px solid #ddd;
border-top-color: #1a73e8;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
.badge { animation: none; }
.spinner { animation-duration: 3s; }
}
</style>
<p>Inbox <span class="badge">3</span></p>
<p><span class="spinner" role="status" aria-label="Loading"></span></p>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- @keyframes describes a timeline with from/to and percentage stops; the animation shorthand plays it by name.
- In the shorthand, the first time is the duration, the second the delay, and a unitless number the iteration-count.
- fill-mode both shows the from keyframe during the delay and holds the to keyframe afterwards, so nothing flashes.
- Keep the base style as the final state, give reduced-motion users animation: none or a slower version, and prefer transform and opacity.
CSS reference — MDN Web Docs