CSS Animations & Transitions

Beginner · 15 min

How CSS transitions and keyframe animations work, and why animating transform and opacity stays smooth while animating width or top can visibly stutter.

Before thisThe Box Model & the Cascade

Web Dev Simplified · 15 min

CSS Animations & Transitions

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A transition animates a property between two states (usually triggered by something like :hover or a class change) and needs a property, a duration, and typically a timing function.
  • @keyframes defines a named sequence of styles at points along a timeline, letting an animation run through multiple steps or loop, without needing a trigger event the way a transition does.
  • The transform property (translate, scale, rotate, skew) changes an element's visual position or shape without affecting the layout of surrounding elements.
  • Animating transform and opacity can run on the compositor thread and often stays smooth even under load, because the browser doesn't need to recalculate layout or repaint pixels for every frame.
  • Animating layout-affecting properties like width, height, top, or margin forces the browser to recompute layout (and usually repaint) on every single frame, which is why those animations are more likely to stutter.

Two different problems: “animate a change” vs. “run a sequence”

CSS gives you two distinct tools for motion, and a lot of early confusion comes from not knowing which one a given situation calls for.

A transition answers: “when this property changes value, don’t snap instantly — ease into the new value over time.” It needs some other event to trigger the change — a :hover, a class toggle from JavaScript, a focus state. A transition never animates on its own; it only smooths out a change that was going to happen anyway.

A @keyframes animation answers a different question: “run this specific sequence of steps, on its own, possibly forever.” It doesn’t need an external trigger the way a transition does — once you apply it to an element, it can start automatically on page load, loop indefinitely, or run through several distinct visual states, not just two.

If you need “this button should ease to a darker shade on hover,” that’s a transition. If you need “this loading spinner should rotate continuously,” that’s a @keyframes animation — there’s no “before” and “after” state to transition between, just a repeating sequence.

Transitions: property, duration, timing function

.button {
  background-color: #3b5bdb;
  transition-property: background-color;
  transition-duration: 200ms;
  transition-timing-function: ease-out;
}

.button:hover {
  background-color: #2a46c4;
}

usually written as the shorthand:

.button {
  background-color: #3b5bdb;
  transition: background-color 200ms ease-out;
}

transition-property says what to animate (you can list several, or use all to catch every animatable property — convenient, but it can accidentally animate things you didn’t intend, so naming properties explicitly is usually the safer habit). transition-duration says how long the change takes. transition-timing-function shapes the pace of the change over that duration — linear moves at a constant rate, ease-out starts fast and settles gently, and so on. Without a duration, nothing visibly happens at all — the property still changes, just instantly, which is easy to forget when a transition “isn’t working” and the actual bug is a missing transition-duration.

@keyframes: a named timeline

@keyframes pulse {
  0%   { transform: scale(1); opacity: 1; }
  50%  { transform: scale(1.1); opacity: 0.8; }
  100% { transform: scale(1); opacity: 1; }
}

.notification-dot {
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes defines percentages along a timeline — 0% is the start, 100% is the end, and you can add as many intermediate steps as the animation needs. The animation shorthand on the element then says which keyframe sequence to run, how long one cycle takes, the timing function, and how many times to repeat it (infinite for a continuous loop, or a specific number). This is the right tool for anything with more than two states, or anything that needs to run without a trigger — loading indicators, looping background effects, attention-drawing pulses.

The transform property: move and reshape without touching layout

transform is worth understanding on its own, separately from animation, because it behaves differently from most CSS properties. Rather than changing an element’s actual position in the document — the way top, left, or margin would — transform shifts how the element is drawn, after layout has already happened:

  • translateX(20px) / translateY(20px) — move the element visually, without changing the space it occupies in the document.
  • scale(1.2) — grow or shrink the element visually.
  • rotate(15deg) — rotate the element.
  • skew(10deg) — slant the element.

Because transform doesn’t move the element within document flow, nothing else on the page needs to shift to accommodate it — a translated button doesn’t push its neighbors around the way changing its margin would. This isn’t just a convenience; it’s the exact property of transform that makes it cheap to animate, which is the performance question worth taking seriously.

Why some animated properties stutter and others don’t

Every frame of an animation, the browser has to figure out what the screen should look like. That process has roughly three stages: layout (figuring out the size and position of every element), paint (figuring out the actual pixels — colors, text, shadows — for each element), and composite (assembling all those painted layers into the final image shown on screen).

Properties like width, height, top, left, and margin all affect an element’s size or position in the document. Animating any of them forces the browser to redo the layout stage on every single frame — and since layout changes can ripple outward (if one element resizes, its siblings may need to shift too), that recalculation isn’t even contained to the one element you’re animating. Follow that up with a repaint, and you’re asking the browser to do expensive work 60 times a second just to keep the animation smooth. On a busy page, or a less powerful device, frames start getting dropped, and the animation visibly stutters.

transform and opacity are different. Neither one affects layout at all — a scaled or translated element still occupies the same conceptual space it always did as far as layout is concerned, and a change in opacity doesn’t touch size or position either. Modern browsers can hand these two properties to the compositor — a separate stage, often able to run on its own thread — and skip layout and paint entirely for that animation. That’s why you’ll hear transform and opacity described as “compositor- only” properties, and why performance-conscious interfaces lean on them specifically: an animation built entirely from transform and opacity changes is far more likely to stay smooth under load than one that animates width or top, because it’s skipping the two most expensive stages of the rendering pipeline entirely.

The practical takeaway

When you need to move something, prefer transform: translate() over animating top/left. When you need to resize something, prefer transform: scale() over animating width/height, if the visual effect you want can be achieved that way. It won’t always be a perfect substitute — transform: scale() visually stretches content rather than reflowing it the way an actual width change would — but for the vast majority of hover effects, modals, and UI micro-interactions, reaching for transform and opacity first is the difference between an animation that looks smooth everywhere and one that looks smooth only on your own fast development machine.

Check yourself

  1. 01What three things does a basic CSS transition need to actually animate something?

    A property to animate (set with transition-property, or implied by the shorthand), a duration (transition-duration), and typically a timing function (transition-timing-function) describing the pace of change — plus some state change, like a :hover or a class toggle, to trigger it.

  2. 02How is a @keyframes animation different from a transition in terms of what triggers it?

    A transition only animates between two states and needs an external trigger, like a hover or a class being added. A @keyframes animation defines its own internal timeline of steps and, once applied to an element with the animation property, can run automatically on page load, repeat, or loop, with no external trigger required.

  3. 03Why does animating transform: translateX() tend to perform better than animating the left property to move the same element?

    transform can be handled on the compositor thread without the browser recalculating layout for every frame, since moving an element with transform doesn't change its position in document flow. Animating left changes where the element actually sits in the layout, forcing the browser to recompute layout and repaint on every frame, which costs more and is more likely to drop frames.

  4. 04Besides transform, what's the other property commonly described as compositor-only, and why is it also cheap to animate?

    opacity. Changing an element's opacity doesn't affect the size or position of anything on the page, so like transform, the browser can adjust it purely at the compositing stage without recalculating layout or repainting the element's internal pixels.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark