CSS: CSS Animation

🗓 Last updated: 2026-11-30👤 Written by: Blake✓ Reviewed by: Gray🏷 Category: CSS Tutorial

Properties in this section: animation-duration animation-timing-function transform opacity width height margin padding box-shadow top left transition animation

1. @keyframes

Defines the animation sequence by specifying styles at various points:

▶ Example

▶ Try it Yourself

2. 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 Yourself

4. 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:


Properties in this section: animation-duration animation-timing-function transform opacity width height margin padding box-shadow top left transition animation

❓ 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

📝 Exercises

  1. Create a "bouncing ball" animation — ball drops, bounces, gradually weakens (using multi-stage keyframes).
  2. Create a "spinning loader" — a ring continuously rotating (hint: border + border-top-color + border-radius + animation).
  3. Create a "fade-in slide" page: title slides from top, paragraph from bottom, image from left (different animations).
  4. Create a "breathing light" button — button brightness cycles.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏