CSS Animation Guide: Keyframes, Transitions and Easing

September 28, 2026  |  12 min read  |  CSS

By Syed · Frontend Engineer & Creator of UITweak

Animation is what separates a static page from a product that feels alive. But most developers only know two moves: a hover transition and a loading spinner copied from somewhere. This guide covers the full CSS animation toolkit: @keyframes, timing functions, the transition vs animation decision, and the performance rules that keep animations smooth on real devices.

Shortcut: pick from 280+ presets in our free CSS Animation Generator and copy production-ready keyframes instantly.

1. Transitions vs Animations: Which One?

This is the first decision, and most people get it backwards:

  • Transition: animates between two states, usually triggered by hover, focus, or a class change. Simple A to B movement.
  • Animation: runs keyframed sequences with multiple steps, loops, delays, and directions. Full choreography.
/* Transition: hover effect, A -> B */
.button {
  background: #4f46e5;
  transition: background 0.25s ease, transform 0.25s ease;
}
.button:hover {
  background: #4338ca;
  transform: translateY(-2px);
}

/* Animation: multi-step, loops forever */
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-12px); }
}
.badge {
  animation: float 3s ease-in-out infinite;
}

2. @keyframes Anatomy

A keyframe animation has two parts: the @keyframes definition and the animation shorthand that applies it:

@keyframes slide-in {
  from { opacity: 0; transform: translateX(-24px); }
  to   { opacity: 1; transform: translateX(0); }
}

.card {
  /* name | duration | timing | delay | iterations | direction | fill-mode */
  animation: slide-in 0.5s ease-out 0.1s 1 normal both;
}

The fill mode matters. both applies the first keyframe during the delay and the last keyframe after finishing. Without it, elements flash their un-animated state before the delay ends, the most common animation bug on the web.

3. Easing: The Secret Ingredient

Linear motion looks robotic. Real movement accelerates and decelerates. The built-in keywords:

  • ease-out: fast start, gentle landing. Best for entrances.
  • ease-in: slow start, fast end. Best for exits.
  • ease-in-out: gentle both ends. Best for loops like floating or pulsing.
  • cubic-bezier(0.34, 1.56, 0.64, 1): custom curves, including overshoot (values above 1) for springy, playful motion.

Rule of thumb: entrances ease-out, exits ease-in, ambient loops ease-in-out. Getting this right is 80% of what makes animation feel "designed".

4. The Performance Rules

Browsers animate cheaply only when they can use the GPU compositor. That means exactly two properties are safe:

  • transform: translate, scale, rotate. Moves pixels without re-layout.
  • opacity: fading is composited, essentially free.

Animating width, height, top, left, margin, or box-shadowforces layout or paint on every frame and will jank on mid-range phones. If you need a "growing" effect, animate transform: scale() instead of width and height.

5. Staggered Entrances (The Pro Touch)

Lists that fade in one item at a time feel premium. The trick is animation-delay per item. Traditionally that meant drilling an index prop through components; sibling-index() now does it in pure CSS, see our staggered animations guide:

@keyframes rise {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}

.list-item {
  animation: rise 0.5s ease-out both;
}
.list-item:nth-child(1) { animation-delay: 0.05s; }
.list-item:nth-child(2) { animation-delay: 0.12s; }
.list-item:nth-child(3) { animation-delay: 0.19s; }
/* or in React: style={{ animationDelay: (i * 70) + "ms" }} */

6. Multiple Animations on One Element

The animation property accepts a comma-separated list. Each entry is a full shorthand, so one element can float, pulse, and fade in at once:

.badge {
  animation:
    rise 0.6s ease-out both,          /* entrance, runs once */
    float 3s ease-in-out 0.6s infinite; /* ambient, starts after */
}

Order matters for readability: put the entrance first, then ambient loops. Delays keep them from fighting over the same frames.

7. Scroll-Driven Animations

The newest primitive: tie animation progress to scroll position instead of time. A progress bar that fills as you read, no JavaScript:

@keyframes grow {
  from { transform: scaleX(0); }
  to   { transform: scaleX(1); }
}

.progress {
  transform-origin: left;
  animation: grow linear both;
  animation-timeline: scroll(root);
}

animation-timeline: scroll(root) binds the timeline to page scroll; view() binds it to an element entering the viewport. For the full technique see our scroll animations guide.

8. Common Animation Bugs

Flash before the delay ends

An element with animation-delay: 0.5s shows its final state for half a second, then jumps back to animate. Fix: add a fill mode (both or backwards) so the first keyframe applies during the delay.

Animation restarts on every render

In React, if the element remounts (key change, conditional render), the animation replays from zero. Keep keys stable across renders, or drive replays intentionally with a changing key.

Jank on mobile

Nine times out of ten, something is animating width, top, or box-shadow. Switch to transform and opacity and the jank disappears. Profile with DevTools before assuming the device is slow.

9. Respect Reduced Motion

Some users experience motion sickness or vestibular disorders. Always provide an escape hatch:

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

This is not just politeness. It is an accessibility expectation, and it costs five lines.

The animation shorthand: order traps

The animation shorthand takes up to eight values in any order, and the parser disambiguates by type. That flexibility hides two traps. Trap one: the two time values. The first time value is always duration, the second is always delay. animation: slide-in 0.3s 0.5s means a 0.3 second animation starting after 0.5 seconds. Reverse them and you get a 0.5 second animation with a 0.3 second delay, which looks exactly like a mistyped delay. Trap two: a bare number is an iteration count, not a duration. animation: slide-in 2 runs the keyframes twice at the default duration of zero seconds, which means it appears to do nothing. Durations need units; iteration counts must not have them. When a shorthand gets hard to read, I split it. animation-name and animation-duration on their own lines are never ambiguous, and the next person reading the code will thank you.

Durations that feel right

Good animation timing is mostly a small set of numbers applied consistently. Micro-interactions, hovers, toggles, pressed states: 150 to 250 milliseconds. Anything shorter feels instant, anything longer feels laggy on a control the user operates repeatedly. Entrances, cards sliding in, modals appearing, page transitions: 300 to 500 milliseconds with an ease-out curve. Exits can be slightly faster than entrances; the eye forgives a quick disappearance. Ambient loops, floating badges, pulsing dots: 2 seconds or more, ease-in-out, infinite. The consistent mistake I see is using one duration everywhere, usually 300ms, which makes ambient motion feel twitchy and entrances feel fine. Pick three durations for your project, name them as tokens, and stop thinking about it per element.

Beyond the global kill-switch: reduced motion done right

Section 9's global rule is the safe default, but it has a flaw: collapsing every duration to near zero can leave content stuck at opacity: 0, mid-entrance, invisible to the very users you are trying to help. For entrance animations, the correct reduced-motion behavior is the end state, not a frozen mid-state.

@media (prefers-reduced-motion: reduce) {
  .card {
    animation: none;
    opacity: 1;
    transform: none;
  }
}

Apply it per component rather than globally: turn off the animation and explicitly set the properties the keyframes would have ended at. For scroll-driven animations the same logic applies. A progress bar tied to scroll is fine to keep, because the user controls the motion. An auto-playing entrance tied to view() should resolve to its final state. In practice I keep the global kill-switch as a backstop and write the per-component overrides for anything the user must be able to read.

FAQ

Should I use CSS or a JS animation library?

CSS for anything declarative: hovers, entrances, loops, scroll effects. Reach for a JS library only when you need physics, drag interaction, or timeline scrubbing that CSS cannot express. Most UI animation never needs JavaScript.

Why does my animation look choppy on phones?

You are probably animating a layout property (width, top, margin) instead of transform or opacity. Switch the property first; that fixes the vast majority of mobile jank.

Can I pause an animation on hover?

Yes: add animation-play-state: paused on hover, for example .card:hover .spinner. Useful for marquees and carousels where users want to read the content.

How do I chain animations in sequence?

Use animation-delay so the second starts when the first ends, or listen for the animationend event in JavaScript for precise control. For pure CSS, delays are usually enough.

Skip hand-writing keyframes

Browse 280+ animation presets, tune duration, easing, delay, and iteration on a live preview, then copy the CSS or Tailwind classes.

Open the Animation Generator, it's free

Share this guide