CSS Animation

@keyframes

Defines the animation sequence by specifying styles at various points:

Example

HTML
<style>
@keyframes slideIn {
  from {
    transform: translateX(-100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

.slide-in {
  animation: slideIn 0.5s ease-out;
}
</style>
<div class="slide-in">Sliding in from left</div>
▶ Try it Yourself

animation Properties

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

Shorthand

CSS
.animation {
  animation: slideIn 0.5s ease-out 0.2s infinite alternate forwards;
  /* name duration timing-function delay iteration-count direction fill-mode */
}

Common Animations

Fade In

Example

HTML
<style>
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.fade-in {
  animation: fadeIn 1s ease-in;
}
</style>
<div class="fade-in">Fade in animation</div>
▶ Try it Yourself

Bounce

Example

HTML
<style>
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}
.bounce {
  animation: bounce 1s infinite;
}
</style>
<div class="bounce">Bouncing element</div>
▶ Try it Yourself

Loading Spinner

Example

HTML
<style>
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
</style>
<div class="spinner"></div>
▶ Try it Yourself

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:


❓ FAQ

Q What's the difference between @keyframes 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 Will animation: infinite cause lag?
A Reasonable use won't. But don't animate too many elements at once (recommended no more than 20). Use `will-change` to tell browsers which elements will change, helping GPU acceleration. If page drops frames, reduce animated elements or shorten duration.
Q Element jumps back after animation — what to do?
A Set `animation-fill-mode: forwards` — animation maintains the last frame's state after finishing. Default is `none`, which returns to start position.

📖 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.
100%

🙏 帮我们做得更好

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

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