Skip to content
Tutorials Center
My learning

Topics

Lesson 104 of 139 ยท Transforms and Animation

Motion, Performance and prefers-reduced-motion

Make CSS animation smooth and comfortable. Learn the rendering pipeline, compositor-friendly properties, will-change, and how to respect reduced motion.

You now know four ways to make things move: transitions, keyframe animations, scroll-driven animations and view transitions. This lesson is about doing that well.

Good motion has two qualities. It is smooth: it never stutters, even on a cheap phone. And it is comfortable: it helps people understand what changed without distracting them or making anyone feel unwell. You will learn why some properties animate smoothly and others don't, when will-change helps, and how to honor the prefers-reduced-motion setting.

Frames and the rendering pipeline

Animation is an illusion made of still frames. Most screens refresh 60 times per second, which gives the browser about 16 milliseconds to produce each frame. On 120 Hz screens, it's about 8 milliseconds. If a frame takes longer, the browser skips it, and the user sees a stutter, often called jank.

To draw a frame, the browser can go through up to four stages:

  1. Style: work out which CSS rules apply to each element and compute the values.
  2. Layout: calculate every box's size and position. Changing one element's width can move hundreds of others.
  3. Paint: fill in the pixels: text, colors, borders, shadows, images, often onto several separate layers.
  4. Composite: stack the painted layers in the right order, with their positions, transforms and opacity, and send the result to the screen.

The key insight: an animation only costs as much as the earliest stage it forces the browser to redo, because every later stage has to run again as well.

What you animate Stages redone each frame Cost
width, height, top, left, margin, padding, font-size Layout, paint, composite High
color, background-color, box-shadow, border-radius Paint, composite Medium
transform (and translate, rotate, scale), opacity Composite only Low

filter is often composited too, though blurs on large areas can still be expensive. The exact rules vary between browser engines, but the table is a reliable guide.

The compositor and the main thread

There's a second reason transform and opacity are special. Most work in a page, including style, layout, paint and all of your JavaScript, happens on one main thread. Compositing happens on a separate compositor thread, often helped by the GPU.

When you animate only transform or opacity with CSS, the browser can hand the whole animation to the compositor. It keeps running smoothly even while the main thread is busy running a slow script. An animation of left, on the other hand, needs layout, so it freezes whenever the main thread is blocked.

Main thread vs compositor animation

Press the button. In most browsers the red ball, animated with left, freezes for two seconds, while the green ball, animated with translate, carries on. Real pages block the main thread all the time: while parsing a big script, rendering a framework component or handling a long event. Compositor-friendly animations shrug that off.

So the practical rule is simple: animate transform and opacity whenever you can. Instead of animating top, animate translate. Instead of animating width to grow a card, try scale. Instead of animating visibility or display, fade opacity.

Faking layout animations. When an element really must change position in the layout, a technique often called FLIP helps: measure where it is (First), apply the layout change (Last), use a transform to move it back to where it was (Invert), then animate that transform to none (Play). View transitions do a version of this for you automatically.

will-change

Before animating an element on the compositor, the browser may need to put it on its own layer, which takes a moment. The will-change property warns the browser ahead of time:

CSS
.drawer {
  will-change: transform;
}

That can remove a small stutter at the start of an animation. But it is not a free speed boost, and it is easy to misuse:

  • Each layer costs memory. Promoting hundreds of elements, for example with * { will-change: transform; }, can make a page slower and even crash tabs on phones.
  • It changes rendering. will-change: transform creates a stacking context and a containing block for fixed-position children, exactly like a real transform.
  • Browsers are already good at this. CSS animations and transitions on transform and opacity are usually promoted automatically.

Use it as a targeted fix: add it to a few elements that you have measured to stutter, ideally just before the animation (for example when a parent is hovered) and remove it afterwards. Don't add it "just in case."

Measuring instead of guessing

Browser DevTools can show you exactly what happens during an animation:

  • The Performance panel records frames, and long red bars point to frames that missed their budget. Turn on CPU throttling to simulate a slower phone.
  • In Chromium's Rendering tab, Paint flashing highlights areas being repainted in green. A transform animation should barely flash; a left animation flashes on every frame. Layer borders shows which elements are on their own layers.
  • The Animations panel lets you slow down, replay and inspect running CSS animations.

For the bigger picture of page speed, including render-blocking CSS and content-visibility, see CSS Performance.

prefers-reduced-motion

For some people, motion on screen is more than a distraction. People with vestibular disorders, migraines or certain other conditions can get dizzy, nauseous or develop headaches from parallax effects, zooming, spinning and large sliding movements. Others find animations distracting when they're trying to concentrate.

Every major operating system has a setting to reduce motion: "Reduce motion" on macOS and iOS, "Show animations" (turned off) on Windows, "Remove animations" on Android, and similar options on Linux desktops. Browsers expose it through a media query with two values:

  • prefers-reduced-motion: reduce: the user has asked for less motion.
  • prefers-reduced-motion: no-preference: they haven't.

You don't need to change your operating system to test it. In Chromium DevTools, open the Rendering tab and use "Emulate CSS media feature prefers-reduced-motion"; Firefox and Safari have similar options.

Three strategies

1. Opt out. Write your animations normally, then remove or tone them down for reduce:

CSS
.banner { animation: slide-in 600ms ease-out both; }

@media (prefers-reduced-motion: reduce) {
  .banner { animation-name: fade-in; } /* swap motion for a fade */
}

2. Opt in. Only add motion when the user has no preference. If you forget an override, the default is calm:

CSS
@media (prefers-reduced-motion: no-preference) {
  .banner { animation: slide-in 600ms ease-out both; }
}

3. Reduce, don't remove. "Reduced" doesn't mean "none". Fades, color changes and small state changes are generally fine, and they help people notice what changed. The things to remove are big movements, zooms, spins, parallax and anything that loops forever. Some motion carries meaning: a loading spinner tells users something is happening, so slow it down or swap it for a pulsing dot rather than removing all feedback.

Reacting to the reduced motion setting

The script uses matchMedia() to read the same setting from JavaScript, and listens for change so it updates if the user flips the setting while the page is open. Use this for motion that JavaScript controls, such as element.animate(), smooth scrollIntoView() calls or carousels that advance automatically.

Don't forget smooth scrolling

scroll-behavior: smooth animates jumps to in-page links. A long smooth scroll is a large movement, so wrap it the same way:

CSS
@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
}

A global safety net

Many projects add a broad rule to their base styles that shortens everything when reduced motion is requested:

CSS
@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;
  }
}

It uses 0.01ms instead of 0 on purpose: animations and transitions still finish, so animationend and transitionend events still fire and scripts that wait for them keep working. The downside is that it's blunt. It removes helpful fades along with harmful motion, so treat it as a backstop, and still design reduced versions of your important animations.

Letting users choose on your site

Not everyone knows about the operating system setting, and some people want calm motion on one site but not everywhere. A site-level switch helps. Custom properties make it easy: drive every duration from a variable, and set that variable from both the media query and a user choice.

A site-wide motion switch

On a real site you'd save the choice, for example in localStorage or the user's account, and apply the attribute before the page renders. Storage isn't available in these sandboxed editors, so the demo keeps it in memory.

Motion and accessibility guidelines

The Web Content Accessibility Guidelines (WCAG) have three success criteria that apply directly to animation:

  • Pause, Stop, Hide (Level A): content that moves, blinks or scrolls automatically for more than five seconds, alongside other content, needs a way to pause, stop or hide it. The marquee in Keyframe Animations has a pause button for this reason.
  • Three Flashes (Level A): nothing should flash more than three times in any one second, because rapid flashing can trigger seizures.
  • Animation from Interactions (Level AAA): motion triggered by an interaction, such as parallax on scroll, should be possible to turn off unless it is essential.

Beyond the rules, a good test is to ask what each animation is for. Motion that explains a change, such as where a panel came from or which item was removed, earns its place. Motion that only decorates should be subtle, short and easy to switch off. More on these topics is in Accessible CSS.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is animating transform usually smoother than animating left?

    Animating left re-runs layout, paint and composite on every frame on the main thread. Transforms and opacity can be handled by the compositor alone.
  2. 2. What is the best way to use will-change?

    Each promoted layer costs memory, and will-change also creates a stacking context. It is a targeted fix, not a general speed boost.
  3. 3. Which media query matches when a user has asked their system for less motion?

    The two values are reduce and no-preference. Use reduce to tone motion down, or no-preference to opt in to motion.
  4. 4. Why do global reduced-motion resets often use 0.01ms instead of 0s?

    With a tiny positive duration the animations still run and finish instantly, so animationend and transitionend fire as usual.
  5. 5. For a user who prefers reduced motion, what should usually happen to a loading spinner?

    Reduced motion means less, not none. The spinner communicates that work is happening, so keep a calmer form of that feedback.

Summary

  • Each frame has a budget of about 16ms; missing it causes visible jank.
  • Rendering runs style, layout, paint and composite; animate properties that skip as many stages as possible.
  • transform and opacity can run on the compositor, so they stay smooth even when JavaScript blocks the main thread.
  • will-change is a targeted hint with real costs; measure with DevTools before reaching for it.
  • Respect prefers-reduced-motion: opt in with no-preference or tone down for reduce, and replace big movement with fades instead of removing all feedback.
  • Offer a site-level motion switch, provide pause controls for long-running motion, and never flash more than three times a second.

Next: CSS Nesting โ€” write related rules inside each other for shorter, better organized stylesheets.