← Back to Frontend, page 1

frontend-0013 · Medium mid

Which kinds of style changes trigger layout, paint or only compositing, and how can you check what a change costs?

Changing geometry such as left, width or font-size forces layout and then paint. Changing colour forces paint without layout. Changing transform or opacity on its own layer can be handled by the compositor with neither. Count layouts with the DevTools Performance panel or the Chrome performance metrics to confirm.

Full answer

After the first render, a style change can restart the pipeline at different points:

  • Layout, then paint, then composite. This happens when a change can affect the geometry: width, height, margin, top, left, font-size, adding or removing nodes. Layout of one element often forces layout of its children, siblings and parents.
  • Paint, then composite. This happens for changes that do not move anything but change pixels: background-color, color, box-shadow, border-color.
  • Composite only. This happens for transform and opacity when the element has its own compositing layer. The browser moves or fades a layer on the GPU, and the main thread is not involved. Chrome promotes layers for animations on these properties, and will-change is a hint.

This is why an animation of left is more likely to stutter on a busy page than an animation of transform: translateX(). The browser must redo layout on the main thread on each frame.

The exact behaviour is up to each browser, so measure instead of guessing. In DevTools, the Performance panel shows the purple Layout and green Paint bars for each frame. You can also turn on Paint flashing in the Rendering tab. In code, the Chrome DevTools Protocol exposes a LayoutCount metric.

The test below changes one property at a time and waits for two frames. A change of left adds layouts, background-color and transform do not.

Do not apply will-change to many elements. Each layer costs memory, and too many layers can make things slower.

html
<style>
  #box { position: relative; left: 0; width: 50px; height: 50px; background: tomato; transform: translateX(0); }
</style>
<div id="box"></div>
<script>
  const frame = () => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
  window.change = async (kind) => {
    if (kind === 'left') box.style.left = '40px';
    if (kind === 'color') box.style.backgroundColor = 'navy';
    if (kind === 'transform') box.style.transform = 'translateX(40px)';
    await frame();
  };
</script>

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The layout count came from the Chrome DevTools protocol (LayoutCount): 0 for the colour and transform changes, and 1 for the left change. Paint and compositing were not counted, and other browsers were not tested.

Key points

  • Geometry changes trigger layout, then paint.
  • Colour changes trigger paint but not layout.
  • transform and opacity can run on the compositor with neither.
  • Measure with the Performance panel or a layout counter.
  • Too many promoted layers cost memory.

Common mistakes

  • Believing every animation is cheap if it uses CSS, when animating left or width still needs layout.
  • Adding will-change to everything.
  • Reading layout values such as offsetWidth between writes, which forces layout early.
  • Judging cost by looking at the animation instead of recording a trace.

Follow-ups

  • What is layout thrashing and how do you avoid it?
  • When does an element get its own layer?
  • Why can opacity be cheap but a large box-shadow animation not?

Tips

  • Group the properties into three cost classes and name one in each.
  • Say that you would verify with a trace, because browsers differ.