CSS animations with @keyframes

Intermediate  Nov 3, 2026

Create loops, entrances and loaders without JavaScript.

CSS animations with @keyframes
The steps of this tutorial at a glance.
The steps of this tutorial at a glance.

A fade-in entrance

@keyframes fade-up {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: none; }
}

.hero h1 {
  animation: fade-up 0.6s ease both;
}

both keeps the first frame before the animation starts and the last frame after it ends.

How the code in this section fits into the page.
How the code in this section fits into the page.

A loading spinner

.spinner {
  width: 32px;
  height: 32px;
  border: 4px solid #e5e7eb;
  border-top-color: #2563eb;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

Stagger a list

li { animation: fade-up 0.5s ease both; animation-delay: calc(var(--i) * 80ms); }

Set style="--i:0", --i:1, and so on on each item.

Rules for good animation

  • Animate transform and opacity only.
  • Keep it short: 150 to 500 ms for interface effects.
  • Never animate large areas continuously.
  • Respect users who ask for less motion:
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
Rules for good animation
Rules for good animation

More tutorials