CSS: CSS Animation
1. @keyframes
Defines the animation sequence by specifying styles at various points:
▶ Example
▶ Try it Yourself2. animation Properties
(1) Quick Reference
| Property | Values | Purpose |
|---|---|---|
animation-name |
@keyframes name |
Animation name |
animation-duration |
1s, 500ms |
How long animation takes |
animation-timing-function |
ease, linear, ease-in, ease-out |
Speed curve |
animation-delay |
0.5s, -1s |
Delay before start |
animation-iteration-count |
1, 2, infinite |
Number of plays |
animation-direction |
normal, reverse, alternate |
Play direction |
animation-fill-mode |
none, forwards, backwards, both |
Style before/after |
(2) Shorthand
CSS
/* Animation effect */
/* Animation effect */
/* Animation effect */
/* Animation effect */
.animation {
/* Animation effect */
/* Animation effect */
/* Animation effect */
/* Animation effect */
animation: slideIn 0.5s ease-out 0.2s infinite alternate forwards;
/* name duration timing-function delay iteration-count direction fill-mode */
}
3. Common Animations
(1) Fade In
▶ Example
▶ Try it Yourself(2) Bounce
▶ Example
▶ Try it Yourself(3) Loading Spinner
▶ Example
▶ Try it Yourself4. Performance Tips
🔥 Performance warning: Don't animate too many elements at once. Animating 20-30 elements simultaneously causes jank. Use
will-change to tell browsers which elements will animate, but don't abuse it — will-change elements consume extra GPU memory.
Best properties to animate:
Avoid animating:
width,height,margin,padding(causes reflow)box-shadow(expensive to calculate)
❓ FAQ
Q How do I write @keyframes and reference it with animation?
A Define the keyframes first, then reference them:
@keyframes slide { from { transform: translateX(0); } to { transform: translateX(100px); } } followed by animation: slide 1s ease;. Frames contain property values — 0% can be written as from, 100% as to. Names are case-sensitive; a mistyped reference fails silently with no console error, the sneakiest bug there is.Q How do I make an infinite looping animation in CSS?
A
animation: spin 2s linear infinite; — the keyword infinite does it. Pair it with @keyframes spin { to { transform: rotate(360deg); } } for the classic loading spinner. Use linear as the timing function for loops — with ease, each cycle stalls briefly and the rotation loses continuity.Q What's the difference between animation and transition?
A transition only handles "state A to state B" simple changes. @keyframes can define multiple keyframes (0%, 50%, 100%) for complex multi-stage animations. Simple hover effects use transition; complex auto-playing effects use @keyframes.
Q The element jumps back after the animation ends — what to do?
A Set
animation-fill-mode: forwards — the element holds the last frame's state after finishing. The default is none, which snaps back to the start position.Q Will infinite loop animations make the page laggy?
A Not with reasonable use. Don't animate too many elements at once (keep it under 20). Declare
will-change to hint the browser which elements will change, helping GPU acceleration. If frames drop, reduce the number of animated elements or shorten durations.Q How do I write a bounce animation?
A The easy way is
alternate playback: animation: bounce 0.6s ease-in-out infinite alternate; with @keyframes bounce { to { transform: translateY(-20px); } }. For believable gravity, use multiple keyframes: falling with ease-in, a squash to scaleY(0.9) at the bottom, recovering with ease-out — far livelier than plain up-and-down.Q How do I make an animation start after a delay?
A
animation-delay: 2s;, or the fourth value of the shorthand: animation: slide 1s ease 2s forwards;. Negative values have a trick use — animation-delay: -0.5s starts the animation as if it's already halfway through, great for staggering multiple elements with offset rhythm.Q How do I pause an animation on hover?
A One property:
.card { animation: float 3s infinite; } .card:hover { animation-play-state: paused; } — switch back to running to resume. Carousels that pause on hover and spinning icons that stop on hover both run on this.Q Which CSS properties animate smoothly and which cause jank?
A The iron rule is
transform and opacity only — the compositor handles them on the GPU without reflow. Animating width, height, top, left, margin re-lays out the page every frame and drops frames as soon as there are many elements. When you truly need a size change, swap width for transform: scale() — similar look, far better performance.📖 Summary
@keyframesanimations are more powerful thantransition— can define multi-stage animation sequences- Through
animationsub-properties, control play count, direction, delay, fill mode, etc. - Prioritize
transformandopacityin animations for best performance - For infinite loops (like loading spinners), use
infinite
📝 Exercises
- Create a "bouncing ball" animation — ball drops, bounces, gradually weakens (using multi-stage keyframes).
- Create a "spinning loader" — a ring continuously rotating (hint: border + border-top-color + border-radius + animation).
- Create a "fade-in slide" page: title slides from top, paragraph from bottom, image from left (different animations).
- Create a "breathing light" button — button brightness cycles.
🧩 Related Examples
Pure CSS LoadersFour classic loading indicators on one page: spinning ring, bouncing dots, indeterminate bar and breathing p…
Skeleton ScreenGray placeholders before content arrives: avatar ring, text lines and an image block in position while a lin…
conic-gradient Progress RingA conic-gradient paints an angle-filled ring; cap it with an inner circle and it becomes a progress bar. Rot…