All articles

CSS keyframe animations: a practical guide

· 3 min read

CSS animations have two parts: an @keyframes rule that describes how styles change over time, and the animation property on an element that says which keyframes to run, for how long and how often. They run without JavaScript and, for the right properties, without blocking the main thread.

Keyframes

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

from and to are aliases for 0% and 100%. Use percentages for more steps:

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

Properties not listed in a keyframe are interpolated from the neighboring keyframes, or from the element's own styles.

The animation property

.toast {
  animation: fade-in-up 300ms ease-out 100ms both;
}

The shorthand accepts these values in any order, except that the first time value is the duration and the second is the delay:

Property Example Meaning
animation-name fade-in-up Which @keyframes to run
animation-duration 300ms Length of one cycle
animation-timing-function ease-out Acceleration curve
animation-delay 100ms Wait before starting (negative starts partway through)
animation-iteration-count 3, infinite How many cycles
animation-direction alternate Play forwards, backwards or alternate
animation-fill-mode both Apply keyframe styles before and after running
animation-play-state paused Pause or run

Timing functions

  • ease (default): slow start, fast middle, slow end.
  • ease-out: fast start, slow end. Feels natural for things entering the screen.
  • ease-in: slow start, fast end. Good for things leaving.
  • linear: constant speed, right for spinners and continuous rotation.
  • cubic-bezier(0.34, 1.56, 0.64, 1): a custom curve; values above 1 overshoot for a springy effect.
  • steps(8): jumps in discrete steps, used for sprite sheets and typewriter effects.

Timing functions apply between each pair of keyframes, not across the whole animation. A keyframe can set its own animation-timing-function for the segment that follows it.

Fill modes

Value Before start (during delay) After end
none Element's own styles Element's own styles
forwards Element's own styles Last keyframe
backwards First keyframe Element's own styles
both First keyframe Last keyframe

Entrance animations with a delay usually need both: otherwise the element is visible during the delay, then disappears and fades in.

Animate transform and opacity

Browsers render a frame in steps: style, layout, paint and composite. Animating width, height, top, left or margin forces layout on every frame; box-shadow, background and color force painting. Both can stutter on slower devices.

transform and opacity can usually be handled by the compositor alone, which is why they're the standard choice:

Instead of Animate
left, top transform: translate()
width, height transform: scale()
display, visibility opacity (plus visibility at the end)
box-shadow opacity of a pseudo-element with the shadow

The last technique is shown in the box-shadow guide. will-change: transform can hint that an element will animate, but use it sparingly: each such element takes extra memory.

Respect reduced motion

Some people get dizzy or nauseous from motion, and operating systems let them ask for less of it. Honor that setting:

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

A tiny duration rather than animation: none keeps animationend events firing for code that depends on them. For less aggressive handling, replace large movements with a simple fade instead of removing all motion. Also avoid flashing more than three times per second, which can trigger seizures.

Useful patterns

Loading spinner

@keyframes spin { to { transform: rotate(360deg); } }
.spinner { animation: spin 0.8s linear infinite; }

Staggered list

.list > li { animation: fade-in-up 300ms ease-out both; }
.list > li:nth-child(2) { animation-delay: 50ms; }
.list > li:nth-child(3) { animation-delay: 100ms; }

Or set style="--i: 2" on each item and use animation-delay: calc(var(--i) * 50ms).

Attention shake for a form error

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
.invalid { animation: shake 400ms ease-in-out; }

Controlling animations from JavaScript

el.addEventListener("animationend", () => el.classList.remove("invalid"));

// Web Animations API: the same keyframes, with full control
const animation = el.animate(
  [{ opacity: 0, transform: "translateY(16px)" }, { opacity: 1, transform: "none" }],
  { duration: 300, easing: "ease-out", fill: "both" },
);
await animation.finished;

Animated gradients need a different approach, since gradient images can't be interpolated directly; that article explains the background-position and @property techniques.

Frequently asked questions

Animation or transition?

Use a transition for a change between two states triggered by something (hover, a class change). Use a keyframe animation for multi-step motion, looping, or motion that starts on its own.

Why does my element jump back after the animation?

After the last keyframe, the element returns to its normal styles. Add animation-fill-mode: forwards (or both) to keep the final keyframe's values.

How do I restart an animation?

Remove the animation class, force a reflow (read el.offsetWidth), and add the class again. Or use the Web Animations API: el.getAnimations().forEach((a) => { a.cancel(); a.play(); }).

Can I pause an animation?

Yes: animation-play-state: paused, for example on hover or when the element is off-screen.

Further reading