A test page for watching what contain and content-visibility do to the browser's rendering pipeline.
You insert lots of DOM ("work") into a container, choose a containment, hover a trigger that changes something next to
that container, and record it in the DevTools Performance panel.
Live: https://push-based.github.io/css-containment-lab/ — or open index.html straight from disk, there's no build step.
- Insert work into
#container-main:- Insert layout: 750 blocks of zero-sized nested
<div>s (48,000 elements). Costs layout, paints nothing. - Insert paint: 1,700 small rotated, shadowed images. Costs paint and layerize, no layout.
- Insert demo: one card with a hover menu. Shows the design impact of containment (clipping, stacking contexts).
- Insert layout: 750 blocks of zero-sized nested
- Choose where containment goes:
- main / trigger: apply it to
#container-main, to the trigger panel#container-panel, or to both. - container / children: apply it to the element itself or to each of its direct children.
- main / trigger: apply it to
- Choose the containment: None, Layout, Paint, Size, Content, Strict, or
content-visibility: auto. - Optionally move the content out of view:
- Offscreen: pushes
#container-main9,000px down. - Obscure: pushes it 9,000px down inside its block element, so the start of that block stays visible.
- Offscreen: pushes
- Start a Performance recording, hover a trigger for about 2 s, then stop:
| trigger | animates | main-thread work it causes |
|---|---|---|
| hover | nothing | hit test on mouse move |
| layout/hittest | margin-bottom |
style, layout, pre-paint, paint |
| paint | color, border-color |
style, paint (no layout) |
| composite | transform |
none (compositor only) |
| bg-color | background-color |
none: current Chrome runs background-color animations on the compositor |
| all | width, background-color |
layout + everything after it |
The trigger panel has a fixed min-height, so #container-main never moves. Every change a trigger makes happens
outside the content you contain. That is the scenario the lab measures: how much does content cost that didn't change?
ms per main-thread frame, median of 5 runs × 2 s, Chrome 156, Apple M4 Max, no CPU throttling
(node measure/measure.mjs). Absolute values depend on your machine; compare ratios.
Layout work + layout trigger. The layout root is #document unless noted.
| containment | Rendering | Painting | where the time goes |
|---|---|---|---|
| none | 4.32 | 0.22 | Pre-paint 3.18, Layout 1.08 |
contain: layout on main |
2.14 | 0.18 | Pre-paint 3.18 → 0.05, but Layout rises to 2.00 |
| … and offscreen | 1.91 | 0.17 | same as onscreen |
| offscreen, no containment | 4.55 | 0.10 | moving content out of view alone saves nothing |
contain: layout on each child |
2.18 | 1.45 | Paint grows: 750 extra stacking contexts |
contain: strict on the trigger panel |
4.07 | 0.15 | layout root moves to the panel, Layout 1.08 → 0.03, Pre-paint stays 3.99 |
contain: strict on trigger panel and main (check both targets) |
0.31 | 0.25 | both effects together: 14× less |
The two containments do different jobs. Containing the element that changes (the trigger panel) moves the layout root
and shrinks Layout. Containing the big subtree that doesn't change (main) isolates it, so Pre-paint can skip it.
contain: layout on main never moves the layout root here, because the change happens outside of it.
Paint work + paint trigger. No layout happens.
| containment | Rendering | Painting | where the time goes |
|---|---|---|---|
| none | 1.32 | 1.18 | Layerize 1.24, Paint 1.14 |
contain: paint on main |
1.85 | 0.69 | Paint −45%, but Layerize rises to 1.76 |
| … and offscreen | 0.15 | 2.65* | Paint 0.20, Layerize 0.02 |
| offscreen, no containment | 1.15 | 1.20 | barely helps |
content-visibility: auto on main |
1.83 | 0.70 | same as contain: paint while visible |
| … and offscreen | 0.13 | 0.33 | everything skipped |
content-visibility: auto on each child |
0.88 | 0.84 | small win |
| bg-color trigger, no containment | 0 | 0 | composited, no main-thread frames at all |
At 4× CPU slowdown (the DevTools setting used for the workshop slides) the ratios are the same and the per-frame
numbers are large enough to read on stage: layout work 15.5 → 3.9 → 0.3 ms Rendering, paint work 1.6 → 0.9 → 0.1 ms Paint
(node measure/measure.mjs index.html measure/scenarios.json 5 2000 4).
* Commit swings between 0.09 and 2.9 ms in offscreen runs; Paint itself is 0.20. Look at the Paint entry, not the Painting total.
node measure/measure.mjs # index.html, measure/scenarios.json, 5 runs, 2 s each, no CPU throttling
node measure/measure.mjs https://push-based.github.io/css-containment-lab/ measure/scenarios.json 5 2000 4 # live page, 4x CPU throttlingRequires Node 22+ and Chrome (set CHROME=/path/to/chrome if it isn't in the default location; HEADED=1 shows the window).
No npm dependencies: the script talks to Chrome over the DevTools Protocol directly. It drives the lab through its own
buttons, checks that the trigger's hover animation is really running (and retries if not), records a trace per run, and
prints milliseconds per main-thread frame per pipeline stage, grouped the same way the DevTools Performance panel groups them:
- Rendering = Recalculate style + Layout + Pre-paint + Layerize + Hit test
- Painting = Paint + Commit (raster runs on worker threads and is reported separately in the JSON output)
The JSON output also includes the layout root and dirty-object count of every Layout, so you can check whether containment
actually moved the layout root. Edit measure/scenarios.json to add your own combinations.
When you take numbers from this lab, note the Chrome version, the machine and the CPU throttling, and report per-frame values (or a fixed recording length). Totals from recordings of different lengths can't be compared.
This is a fixed port of css-containment-debug (last edited April 2022).
- The paint trigger didn't paint anymore. It animated
background-color, which Chrome now animates on the compositor, so it caused no main-thread paint at all. It now animatescolor+border-color; the old animation is kept as the bg-color trigger to demonstrate the change. - The layout blocks were nested inside each other. A
<div>in the template that should have been</div>left two elements open per block, so all 750 blocks ended up nested in one another, up to the HTML parser's depth limit of 512. Applying containment to children therefore only ever reached a single element. The blocks are now siblings. - Paint work loaded 1,700 images from picsum.photos, so results depended on the network. It now uses
tile.pngfrom this repo. - CSS bugs:
.work.paint::afterwas missing a semicolon (and<img>can't render::afteranyway). Removed..contain-contentused custom properties that were never defined.border-style: var(--noneOc)used a colour as a border style.- A
border: 1px soildtypo.
- Containment is re-applied when work is inserted or a target/radio changes. Previously new children missed it until you clicked a containment button again.
- Added a
measure/script for repeatable measurements.