Animating transform and opacity is cheap because the browser can hand the change to the graphics processor without recalculating the page. Animating width, top or margin is expensive because every single frame forces the browser to work out the geometry of the document again. The difference is not subtle once a list gets long.
The previous lesson in this module covered "How Do Hover and Focus States Feel Deliberate?", and this lesson is the last one in the CSS Animations and Transitions module.
What happens on every frame of an animation?
Drawing the page is a pipeline with four stages, and a property change decides how many of them run.
- Style works out which declarations apply and computes their values.
- Layout calculates the position and size of every box.
- Paint fills in the pixels for each box, including borders, shadows and text.
- Composite stacks the painted layers in the right order.
A change to transform or opacity can be handled entirely in the last stage, because the browser can take the layer that is already painted and move it or fade it. A change to width cannot, because a wider box may push everything below it, so layout has to run, then paint, then composite. A change to background-color sits in the middle: the box keeps its size, but its pixels have to be redrawn.
This is why the property you choose matters more than the duration you choose. A 400ms animation of a layout property can cost more than a 2 second animation of a transform.
Which properties are cheap, and which are not?
| Property | Stages it forces | Verdict |
|---|---|---|
| transform | Composite | Cheap |
| opacity | Composite | Cheap |
| color, background-color | Paint, composite | Acceptable |
| box-shadow | Paint over a growing area | Costly with a large blur |
| filter, backdrop-filter | Paint plus a read of what is behind | Costly |
| width, height | Layout, paint, composite | Expensive |
| top, left, margin, padding | Layout, paint, composite | Expensive |
So a card that lifts on hover should move with a translate and not by changing its margin, and a list that reveals items should fade and slide rather than grow.
.order {
opacity: 0;
transform: translateY(10px);
transition: opacity 300ms ease, transform 300ms ease;
}
.order.is-visible {
opacity: 1;
transform: none;
}
A large drop shadow is the part to watch. A big blur radius means the browser has to repaint a wide area of the screen on every frame of the transition, so either keep the blur small or animate the shadow only when the pointer leaves.
How do you grow a box without animating its height?
Animating height from zero to a fixed value gives every element in the document below it a new position, so the browser must lay the page out again on each frame. There are two workarounds and they suit different situations.
/* layout animation: correct height, layout on every frame */
.panel {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 260ms ease;
}
.panel.is-open { grid-template-rows: 1fr; }
.panel > .panel__body { overflow: hidden; }
/* compositor animation: cheap, but it squashes the content */
.panel--cheap {
transform: scaleY(0);
transform-origin: top;
transition: transform 260ms ease;
}
.panel--cheap.is-open { transform: scaleY(1); }
The grid version is right for a single expandable panel, where one layout pass per frame is a fair price for content that ends at its natural height. The scale version is right for dozens of elements at once, but it distorts the text inside while it moves, so it suits a coloured panel rather than a readable one.
For readable content at scale, the technique to reach for is FLIP: measure the element's final position, apply the final layout, apply the inverse transform that puts it back where it started, then animate that transform back to nothing. The browser only ever animates a transform, so the whole sequence stays on the cheap path. The measurement steps need a few lines of script; the animation itself is one transition on transform.
What does will-change actually do?
will-change is a hint that tells the browser a property is about to change, so it can prepare for it in advance, usually by creating a compositing layer.
.order {
will-change: transform, opacity;
}
Every extra layer costs memory, because each one holds its own texture. Leaving will-change: transform on a thousand rows does not make them faster, it makes the page heavier and can make scrolling worse. Apply it shortly before the animation runs and remove it afterwards, and use it on the handful of elements that actually animate rather than on every child of a list.
How do you keep a long list smooth?
The first thing is to stop animating the list on scroll. Content that moves because the reader scrolled does not need an animation at all, and every scroll-driven transform adds work to the one moment the reader is trying to read.
<ul class="orders">
<li class="order">
<span class="order__id">A-1042</span>
<span class="order__total">124.00</span>
<span class="order__status">Shipped</span>
</li>
<li class="order">
<span class="order__id">A-1043</span>
<span class="order__total">39.50</span>
<span class="order__status">Packing</span>
</li>
</ul>
.orders {
list-style: none;
margin: 0;
padding: 0;
}
.orders > .order {
content-visibility: auto;
contain-intrinsic-size: auto 44px;
}
.order:nth-child(2) { transition-delay: 60ms; }
.order:nth-child(3) { transition-delay: 120ms; }
@media (prefers-reduced-motion: reduce) {
.order { transition: none; opacity: 1; transform: none; }
}
Every row starts at zero opacity and a script adds the is-visible class as the row scrolls into view, which is what starts the transition. Separately, content-visibility: auto tells the browser to skip the rendering work for items that are off screen, which keeps a long list responsive even when every row would otherwise be animated. The contain-intrinsic-size tells it how tall an offscreen row is likely to be so the scrollbar does not jump around as you scroll.
Stagger the reveal with a small delay per item rather than a large one. A delay of 60 milliseconds per row reads as a cascade; a delay of 300 milliseconds means the last row does not appear until several seconds in. And if the reader has asked for reduced motion, remove the transition altogether and leave the rows visible.
Quick reference
- Transform and opacity are composited, so they cost almost nothing per frame.
- Colour changes repaint; size and position changes force a full layout.
- Shadows and filters repaint an area, and the cost grows with the blur and the size of the surface.
- Move with a transform rather than by changing margins, and fade rather than by changing display.
- Use
will-changebriefly and sparingly, because every layer it creates costs memory. - Skip offscreen work with
content-visibilityon long lists, and stagger reveals with small delays.
Try it yourself: Build an orders list with twenty rows and give each row a fade-and-slide reveal using opacity and transform. Open the browser's performance panel, record a reload, and watch the frames while the rows appear. Then change the reveal to animate height from zero instead and record again, comparing how long the frame takes. Restore the transform version, add content-visibility: auto to each list item, and confirm the scrollbar stays steady as you scroll.