Marcus Andrey Study
Ler em português Library

Motion and Interaction · Step 5 · 7 min read

CSS transitions and animations

CSS has two ways to move things: a transition between two states, and keyframes that run a sequence on their own. What each is for, the few properties that matter, how to animate an element appearing, and why every animation needs a reduced-motion answer from the first line.

A timeline rail with four keyframes, and above it a stage where a block takes its poses along a dashed motion path. Move the pointer along the rail to scrub the playhead: the block jumps, turns and lands, eased between its keys, and the nearest key lights up. That's how every tool sees an animation, from CSS to Figma to Lottie: a few poses on a timeline, with the frames between them filled in. This step has five lessons: CSS transitions and animations, Web Animations and View Transitions, prototyping motion in Figma, Lottie, Rive and handing off motion, and motion performance.Illustration: Marcus Andrey, drawn with Hairline (MIT, Lucas Marques).

A transition answers a change. An animation tells a story by itself. That one sentence decides most of the CSS you'll write for motion. A button that lifts when you point at it is reacting to a change of state, so it's a transition. A loader that pulses while you wait has no state to react to, so it's keyframes. Pick the right one and the code stays short, and the developer reading your spec knows exactly what you mean.

What you'll be able to do after reading:

  • Tell when a motion is a transition and when it needs keyframes
  • Read and write the four parts of a transition: property, duration, easing, delay
  • Describe a keyframe animation in plain numbers, from 0% to 100%
  • Add a reduced-motion version to both, in the same file

1. Two tools, two jobs

TransitionPoint at, tab to or press the button. It moves only because its state changed, and it goes back when the state does.
KeyframesA loop you describe from 0% to 100%. It runs on its own, with no change of state, until you stop it.
A transition animates between two states; keyframes animate a sequence that runs by itself. The button eases 2 px up in 150 ms; the dots pulse in 1.2 s, each one 150 ms after the last. With reduced motion on, the button only changes colour and the dots only fade. Live CSS, the code is in the lesson.

Both tiles are live CSS. The button on the left only moves when its state changes: pointer over it, focus on it, finger down on it. The dots on the right don't wait for anything; they run a loop you described in advance. That's the whole difference (MDN, transition · MDN, @keyframes).

Transition Keyframe animation
Starts when A property's value changes (hover, focus, a class, a :checked) It's applied, or its play state is set to running
Steps Two: from the old value to the new As many as you write: 0%, 40%, 100%
Repeats No, it runs once per change As often as you say, or forever
Goes back By itself, when the state goes back Only if you write it that way
Typical use Hover, press, focus, open and close Loaders, attention cues, entrances with several steps

2. A transition, line by line

This is the code the button in the figure runs:

.mo-c-btn {
  transition: transform 150ms ease-out, background-color 150ms ease-out;
}
.mo-c-btn:hover, .mo-c-btn:focus-visible { transform: translateY(-2px); background-color: color-mix(in srgb, var(--accent-fill) 80%, #000); }
.mo-c-btn:active { transform: scale(0.97); transition-duration: 50ms; }

Read the transition line as four words: what changes (transform), how long (150ms), how it accelerates (ease-out), and an optional delay (none here). You can list several properties, each with its own timing. If you leave the easing out, the browser uses ease (MDN, transition).

Three details that make it feel right:

  • Name the properties. transition: all animates things you didn't mean to, like a colour change from a theme switch. List what you mean.
  • The press is faster than the hover. The :active rule shortens the duration to 50 ms, because a press should feel instant (see Duration: how long is right).
  • Focus gets the same motion as hover. People who use a keyboard deserve the same feedback (see Interactive states: hover, press, focus).

3. Keyframes, in plain numbers

The dots run this:

.mo-c-dots i {
  animation: mo-c-pulse 1.2s ease-in-out infinite paused;
}
.mo-c-dots i:nth-child(2) { animation-delay: 150ms; }
.mo-c-dots i:nth-child(3) { animation-delay: 300ms; }
.mo-c-tk:has(.mo-c-play:checked) .mo-c-dots i { animation-play-state: running; }
@keyframes mo-c-pulse {
  0%, 80%, 100% { opacity: .3; transform: scale(0.8); }
  40%           { opacity: 1;  transform: scale(1); }
}

Read @keyframes like a storyboard: at 0% the dot is faint and small, at 40% of the 1.2 seconds it's full and big, and from 80% it rests again. The animation line names the storyboard, then gives its duration, easing and how often to repeat. The animation-delay on the second and third dots is a stagger of 150 ms, which makes three dots read as one movement (see Choreography and stagger).

Two more properties you'll meet in every spec (MDN, animation):

  • animation-fill-mode says what the element looks like before and after the animation. both keeps the first frame during the delay and the last frame at the end. Without it, an entrance can flash its final state before it starts.
  • animation-play-state pauses and resumes. The figure starts paused and only runs when you switch it on: a loop you can stop is a loop that respects people (more in WCAG and motion).

4. Animating something that appears

For years, CSS couldn't animate an element appearing, because display: none to display: block has no in-between. Two newer features fix this, and both have been Baseline since August 2024, which means they work in current Chrome, Edge, Firefox and Safari (webstatus.dev, @starting-style · webstatus.dev, transition-behavior):

  • @starting-style gives the values an element starts from the first time it's shown, so a transition has a "from" (MDN, @starting-style).
  • transition-behavior: allow-discrete lets properties like display take part in a transition: the element stays visible until the fade-out ends (MDN, transition-behavior).
.toast { transition: opacity 200ms ease-out, display 200ms allow-discrete; }
.toast[hidden] { display: none; opacity: 0; }
@starting-style { .toast { opacity: 0; } }

This is a sketch for your own code, not a demo on this page. It's the pattern for toasts, popovers and dialogs that fade in and out without a single line of JavaScript.


5. Reduced motion is part of the first draft

Every motion in this course has a second version for people who asked their device for less motion. The figure's version is in the same file, right under the rest:

@media (prefers-reduced-motion: reduce) {
  .mo-c-btn { transition: background-color 150ms ease-out; }
  .mo-c-btn:hover, .mo-c-btn:focus-visible, .mo-c-btn:active { transform: none; }
  .mo-c-dots i { transform: none; animation-name: mo-c-glow; }
}

Notice what it keeps. The button still changes colour, so the feedback is still there; it just doesn't move. The dots still say "working", by fading instead of growing. That's the pattern: reduce the travel, keep the meaning. Step 6 is all about it, starting with Vestibular disorders and reduced motion.


6. Common mistakes

  • transition: all, which animates layout, colour and anything else that happens to change.
  • A transition on height or width to open a panel, which makes the browser lay out the page every frame (see Motion performance).
  • An infinite loop nobody can stop.
  • An entrance that flashes its end state first, because animation-fill-mode was missing.
  • Writing the reduced-motion version last, or never.

7. Vocabulary

Term Meaning
Transition An animation from an old value to a new one, started by a change of state
Keyframes A named sequence of steps, from 0% to 100%, that an animation plays
Duration How long one run takes, in ms or s
Easing How the speed changes during the run (ease-out, cubic-bezier(), linear())
Delay How long to wait before starting
Fill mode What the element shows before and after the animation runs
Play state Whether an animation is running or paused

8. Try it (no tools needed)

  1. Pick three motions in an app you use: one hover, one loader, one panel that opens.
  2. For each, say whether it's a transition or keyframes, and why.
  3. Write each one as a sentence with four parts: what changes, how long, how it accelerates, what starts it.

Reflection questions: Which of the three would you stop first if someone told you it made them dizzy? What would its reduced version keep?


9. If you already practise

  • Write motion in specs the way CSS reads it: property, duration, easing, delay. It removes a translation step.
  • Keep the reduced-motion rule next to the rule it changes, never in a separate file at the end.
  • Review pull requests for transition: all and for loops without a pause.
  • Try @starting-style for your next toast or popover before reaching for a library.

Checklist

Learn more

Books, courses and videos to go deeper

Book links go to Amazon. As an Amazon Associate I earn from qualifying purchases, at no extra cost to you.

Web Animations and View Transitions · Motion performance · Easing curves · Interactive states: hover, press, focus · Designing a reduced-motion version

Saved only on this device.

↑ ↓ to move · Enter to open · Esc to close