HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1192 min read

CSS Containment & content-visibility

Rendering Work You Can Just Skip

Web developmentweb development / rendering performance

The scenario

A settings page with 60 collapsible sections, ~4,000 DOM nodes. No heavy JS, no big images. Yet Lighthouse shows ~480ms of Recalculate Style + Layout on load, and toggling one checkbox in section 57 costs 90ms.

Why? Layout is global by default. The browser can't prove that a change inside section 57 won't shift section 3, so it recomputes the tree.

The idea: containment is a promise you make

The contain property tells the engine "work inside this box cannot leak out," which lets it stop walking.

contain: layout → my children's geometry never affects outside layout
contain: paint  → nothing paints outside my border box (free clip)
contain: size   → my size ignores my children (you must supply one)
contain: style  → counters/quotes don't escape

With layout containment, editing section 57 relayouts section 57. Done.

The payoff: content-visibility

content-visibility: auto = containment PLUS "skip rendering entirely while off-screen."

.section {
  content-visibility: auto;
  contain-intrinsic-size: auto 500px;
}

Off-screen sections get no style, no layout, no paint. Chrome's canonical demo went 232ms → 30ms initial render. Sections render just-in-time as they near the viewport.

What practitioners actually get wrong

  1. Skipping contain-intrinsic-size. Unrendered elements collapse to 0 height, so the scrollbar jumps around as you scroll. auto 500px means "assume 500px, remember the real size once measured."
  2. Putting it on above-the-fold content. Those render anyway — you paid for containment and got nothing. Target the long tail.
  3. Thinking it hides things. It is NOT display:none. Find-in-page, anchor links and tab focus all still work — they force the section to render.
  4. One wrapper around all 60 sections. Zero benefit: the viewport intersects the wrapper, so everything renders. The skipping unit must be per item.
  5. Forgetting that contain: paint/size creates a containing block for absolute/fixed children and a new stacking context. A position: fixed modal inside a contained card gets trapped inside that card.
  6. Measuring a skipped subtree. Calling getBoundingClientRect() on it forces it to render immediately — the same read-forces-work tax as layout thrashing.

Measure, don't sprinkle

DevTools → Performance → compare "Recalculate Style" + "Layout" totals before/after. If style+layout isn't a real slice of your frame time, containment buys you nothing. This is a long-page / long-list tool, not a global reset rule.

Your exercise

Find a page with a long repeated list (a feed, a 500-row table, a docs sidebar). Record a Performance trace and write down the Style + Layout totals. Add content-visibility: auto; contain-intrinsic-size: auto <row-height>px to the repeated item, re-record, compare.

Then scroll fast with contain-intrinsic-size set, delete it, and scroll again. What breaks — and can you explain why the fix needs an estimate at all?