CSS Keyframe Animations
Lesson 17 of 41 in the CSS3 learning path.
How do I define a reusable @keyframes animation?
@keyframes names a sequence of values the browser interpolates over time. You attach that name to an element with the animation shorthand or the longhands: animation-name, animation-duration, animation-timing-function, and animation-iteration-count. A transition needs a state change. An animation can run on its own, once or forever, which is why a live dot can pulse and a loading block can shimmer without a hover. The sample defines pulse for a live badge and shimmer for a skeleton bar in a course list that is still loading.
Keep the motion small: scale the dot slightly, and move a shimmer highlight with background-position instead of layout. animation-duration is the length of one cycle, and infinite repeats it. Under prefers-reduced-motion: reduce, set animation to none so the badge and the bar hold still. A static muted bar is enough when motion is off. Do not loop a large movement on text someone is trying to read.
Syntax & Parameters
@keyframes pulse { 50% { transform: scale(1.35); } }
.live-dot { animation-name: pulse; animation-duration: 1.6s; animation-iteration-count: infinite; }
@media (prefers-reduced-motion: reduce) { .live-dot { animation: none; } }
Example
Interactive preview available@keyframes pulse {
0%, 100% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.35); opacity: 0.55; }
}
@keyframes shimmer {
from { background-position: 100% 0; }
to { background-position: -100% 0; }
}
body {
margin: 0;
font-family: "Segoe UI", system-ui, sans-serif;
line-height: 1.45;
color: #1c2430;
background: #f4f1ea;
}
.page { max-width: 32rem; margin-inline: auto; padding: 1.1rem; }
h1 { margin: 0 0 0.35rem; font-size: 1.4rem; }
.lead { margin: 0 0 0.9rem; color: #5c6b7a; }
.row {
display: flex;
align-items: center;
gap: 0.75rem;
margin: 0 0 0.7rem;
padding: 0.8rem 0.9rem;
background: #fff;
border: 1px solid #e3ddd2;
border-radius: 12px;
}
.live-dot {
width: 0.7rem;
height: 0.7rem;
border-radius: 50%;
background: #0f766e;
animation-name: pulse;
animation-duration: 1.6s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
}
.row h2 { margin: 0; font-size: 1rem; }
.row p { margin: 0.15rem 0 0; color: #5c6b7a; font-size: 0.85rem; }
.bone {
height: 0.7rem;
border-radius: 999px;
background-color: #e7e5e4;
background-image: linear-gradient(90deg, #e7e5e4 0%, #f8fafc 50%, #e7e5e4 100%);
background-size: 200% 100%;
animation: shimmer 1.4s linear infinite;
}
.bone-title { width: 9rem; }
.bone-line { width: 100%; margin-top: 0.45rem; }
.stack { flex: 1; min-width: 0; }
@media (prefers-reduced-motion: reduce) {
.live-dot,
.bone { animation: none; }
}
Expected Output
A white row shows a small teal dot beside the title "Live workshop" and the line "Starting now". The dot scales up and down and fades, one cycle every 1.6 seconds, and it keeps repeating. Under it, a second white row has no readable title. A short bar and a full-width bar show a light band sliding across a gray fill, one pass every 1.4 seconds, linear, forever. The rows themselves do not move, and the text does not shake. If reduced motion is on, the dot sits still at full opacity and the bars are flat gray with no traveling highlight. The layout of the two rows stays the same either way.
Line-by-Line Walkthrough
- @keyframes pulse names a reusable sequence. At 0% and 100% the dot is full size and opaque. At 50% it scales to 1.35 and fades to 0.55.
- @keyframes shimmer moves background-position from 100% 0 to -100% 0. The gradient highlight slides across the bar. The element itself does not move.
- animation-name: pulse attaches that sequence to .live-dot. The name is the contract between the keyframes and the element.
- animation-duration: 1.6s is one cycle. It is not a delay. A longer duration makes the pulse calmer.
- animation-timing-function: ease-in-out slows the scale at each end so the dot does not tick like a metronome.
- animation-iteration-count: infinite repeats the cycle. A count of 1 would pulse once and stop, which is the wrong signal for a live class.
- The skeleton uses the animation shorthand: name, duration, timing function, and iteration. shimmer 1.4s linear infinite is the same four ideas in one line.
- background-size: 200% 100% gives the gradient room to travel. Without it, background-position has almost nothing to slide.
- The live row keeps real text. The loading row is bars only, so the motion means "not ready" instead of shaking a title the reader is trying to read.
- prefers-reduced-motion: reduce sets animation to none on both the dot and the bars. The dot stays a solid teal circle. The bars stay a flat muted color.
Define the keyframes once and reuse the name. The live dot and any later badge can share pulse without copying the percentages.
Common Pitfalls & Security
Infinite animation on a large area, or animation of layout properties, is how a loading state becomes the most distracting part of the page.
- animation: shimmer 1.4s infinite without a timing function is valid, but the shorthand order matters. Swapping the duration and the name makes the declaration invalid and the animation never starts.
- Animating width or margin on the skeleton reflows the list on every frame. Move a background highlight, or transform a small dot.
- Forgetting animation-fill-mode is fine for a loop that starts and ends on the same values. A one-shot animation will snap back unless you set fill-mode to forwards.
- A keyframes name is global in the stylesheet. Two blocks named pulse overwrite each other. Name them for the motion, and do not reuse a name for a different sequence.
- Leaving the animation running under prefers-reduced-motion ignores an explicit user setting. animation: none is the off switch.
Best Practices & Modern Standards
Keep keyframes small and reusable: a pulse on a status dot, a shimmer on a loading bar, both infinite, both easy to turn off. Set animation-duration and animation-iteration-count where a reader of the CSS should see them, and use the shorthand once you are comfortable with the order. Prefer transform and opacity, or a background position, over top, height, and margin.
Honor prefers-reduced-motion with animation: none and a readable static fallback. Custom properties can hold the duration so a theme can slow every motion from one token. A hover color change still belongs to a transition, not to a keyframe that has to be paused on mouseout. Position a badge with relative or absolute. Do not animate it across the page to get attention.
Real-World Use
A course list shows a live workshop with a pulsing dot so the row reads as "happening now", and the rows that are still loading show shimmer bars instead of fake titles. The pulse lasts 1.6 seconds and repeats. The shimmer lasts 1.4 seconds and repeats. Neither one moves the row.
The same two animations cover a chat presence dot and a dashboard skeleton. When the reader asks for less motion, the dot stays lit and the bars stay gray, and the layout still says which row is live and which row is loading.
Frequently Asked Questions
What is the difference between @keyframes and a transition?
A transition blends from the current value to a new value when a state such as hover changes. @keyframes defines its own sequence, and the animation property plays that sequence on its own. It can repeat forever. Use keyframes for the live dot and the skeleton, and a transition for a hover color.
What do duration and iteration count do?
animation-duration is how long one cycle takes. 1.6s means the dot reaches the 50% keyframe around 0.8s and returns by 1.6s. animation-iteration-count: infinite starts the next cycle immediately, and a count of 1 plays the sequence once. The shorthand puts the duration before the timing function and the count after it.
Why animate background-position instead of the bar width?
Width changes layout, so every frame asks the browser to reflow the list. background-position only repaints the gradient inside a bar that already has a fixed size. transform and opacity are the other cheap choices. The pulse uses both of those on a 0.7rem dot.
How do I respect reduced motion?
Inside @media (prefers-reduced-motion: reduce), set animation to none on the dot and the bars. Leave a static teal dot and a flat gray bar so the loading and live states are still visible. Do not replace the animation with a faster one. The reader asked for less motion, not a different cycle.