Full answer
After parsing, the browser turns HTML and CSS into pixels through a pipeline: style (work out which rules apply), layout (compute size and position of every box), paint (draw boxes into layers) and composite (put the layers together on screen). A change at an early stage usually forces the later stages again.
- A change to geometry (width, height, margin, top, font size, content) forces layout, then paint and composite.
- A change that only affects appearance (colour, background, box-shadow) skips layout but still needs paint and composite.
- A change to
transformoropacityon an element with its own layer can often skip layout and paint and only composite. Whether that happens depends on the browser, so verify.
Ways to cut cost:
- Animate
transformandopacityinstead of layout properties. - Avoid layout thrashing in scripts. Reading a layout value (
offsetHeight) right after writing styles forces the browser to compute layout immediately. Batch all reads first, then all writes. - Use
content-visibility: autoon long off-screen sections so the browser skips their rendering until needed. Pair it withcontain-intrinsic-sizeso the scrollbar stays steady. - Use
containto tell the browser that a component's layout and paint do not affect the rest of the page. - Keep the DOM small and selectors simple.
- Use
will-changesparingly and for a short time, since each hinted layer uses memory.
While the section is far below the viewport, content-visibility: auto skips its rendering, and its height is the 300px placeholder. After scrolling there, it renders at its true 1000px height. The script reads and then writes, so the browser computes layout once for reading, and the boxes end up 200px wide each.
Measure before optimising. The Performance panel in DevTools shows time in recalculate style, layout, paint and composite, and which of them a given interaction triggers.
<style>
body { margin: 0; }
.spacer { height: 2000px; }
.section { content-visibility: auto; contain-intrinsic-size: auto 300px; }
.section .inner { height: 1000px; background: #eef; }
.box { width: 100px; height: 20px; background: tomato; margin-bottom: 4px; }
</style>
<div class="spacer"></div>
<section class="section" id="s1"><div class="inner"></div></section>
<div id="boxes"><div class="box"></div><div class="box"></div><div class="box"></div></div>
<script>
// Batched: read all heights first, then write.
const boxes = [...document.querySelectorAll(".box")];
const widths = boxes.map(b => b.offsetWidth); // reads
boxes.forEach((b, i) => { b.style.width = widths[i] * 2 + "px"; }); // writes
document.title = boxes.map(b => b.offsetWidth).join(",");
</script>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Pipeline: style, layout, paint, composite.
- Geometry changes trigger layout; colour changes trigger paint; transform and opacity can be composite-only.
- Layout thrashing comes from alternating style writes and layout reads.
- content-visibility: auto skips rendering of off-screen sections.
- Measure first; do not guess.
Common mistakes
- loops that alternate element.style.width = ... and element.offsetWidth reads. Each read forces a fresh layout. Do all reads first, then all writes.
- adding will-change: transform to many elements permanently. Extra layers cost memory and can slow things down.
Follow-ups
- does content-visibility: auto help accessibility?
- What is layout thrashing and how do you avoid it?
- What does contain-intrinsic-size do together with content-visibility?
Tips
- Fix what you measured, not what you guessed. A page with 20ms of layout and 2ms of paint should not get paint tweaks first.
- Name the stage a property triggers when you recommend a change. It shows you know why.
- Describe how you would find the slow part in the Performance panel.