Sub topic 10
content-visibility: containment plus skipped rendering
This chapter assumes you know the contain property and its layout, paint, size, and style values from the earlier chapters. It also assumes you can read the Elements and Performance panels in DevTools. content-visibility builds on those pieces. It lets the browser skip the rendering work for an element's contents entirely.
What the property does
contain limits how far rendering work can spread. content-visibility goes one step further. It can tell the browser not to do the work at all.
When contents are skipped, the browser does not run layout for them. It does not paint them or hit-test them. The element's own box still exists, so its borders, background, and place in the parent's layout remain. Only the subtree inside is left out.
The property takes three values:
| Value | Containment applied | Contents skipped? |
|---|---|---|
| `visible` | None. This is the default. | Never |
| `auto` | Layout, style, and paint. Size is added while skipped. | When the element is not relevant to the user |
| `hidden` | Layout, style, paint, and size | Always |
An element is relevant to the user when it is on screen or near the viewport. It is also relevant when it holds focus or a selection, or when something has asked the browser to reveal it. The browser decides this for auto. You do not set it directly.
Why size containment is part of the deal
A skipped subtree has no layout, so the browser cannot measure it. If the element's height came from its children, it would collapse to zero. That is why skipped elements get size containment, as the earlier chapter on size containment described.
An element with content-visibility: auto still has its normal height while it is rendered. When it scrolls away and is skipped, size containment takes effect and the height may drop to zero. The scrollbar then changes size and the content below jumps. The next section covers the fix.
Reserving space with contain-intrinsic-size
contain-intrinsic-size gives a skipped element a placeholder size. Use it with content-visibility:
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}The auto keyword here has a separate meaning from content-visibility: auto. It tells the browser to remember the last size it rendered. The first time, the element is 600px tall. After it has been rendered and then skipped again, the browser reuses the real measured height. The 600px figure is only the first guess.
You can set one value for both axes, or two values for width and height. A single 600px sets both. The earlier example, auto 600px, sets the width placeholder to auto and the height to 600px. To control only the block axis, use contain-intrinsic-block-size. This helps in vertical writing modes.
Pick a placeholder close to the typical real height. If it is too small, many items fit in the viewport at once, so the browser renders more of them than you expected. If it is too large, the scrollbar thumb is too small until the items have been rendered.
A complete page example
Split a long page into independent sections and apply the property to each one:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Chunked page</title>
<style>
.chunk {
content-visibility: auto;
contain-intrinsic-size: auto 800px;
}
</style>
</head>
<body>
<main>
<section class="chunk"><h2>One</h2><p>Long content...</p></section>
<section class="chunk"><h2>Two</h2><p>Long content...</p></section>
<section class="chunk"><h2>Three</h2><p>Long content...</p></section>
</main>
</body>
</html>On the first load, only sections near the viewport go through layout and paint. The rest cost almost nothing until the user scrolls toward them. The saving is largest on pages with thousands of DOM nodes. On a short page there is little to skip, and you gain nothing.
Choosing the unit of skipping
Apply the property to elements that are:
- Independent of their siblings, so their layout does not depend on the layout of the others.
- Large enough that skipping them saves real work.
- Stable in width, because the width comes from the parent while the height is the part that is skipped.
Avoid applying it to tiny elements. The bookkeeping can cost more than the work saved. A section or card with dozens of descendants is a good target. A single <span> is not.
Side effects you inherit
Because content-visibility turns on containment, the containment side effects come with it, even when contents are being rendered:
- Paint containment clips descendants to the element's padding box. A child's
box-shadowor a tooltip that overflows the card will be cut off. - Layout containment makes the element a containing block for absolutely positioned and fixed-position descendants, and it creates a stacking context.
- Style containment scopes counters and similar features to the subtree.
A common surprise is a dropdown menu inside a .chunk that gets clipped at the section edge. Either move the menu out of the section, or do not apply content-visibility to that element.
Using hidden
content-visibility: hidden skips the contents all the time, whatever the viewport shows. It acts like display: none for the contents, with one difference. The browser keeps the rendering state of the subtree, such as layout results, when the element is skipped. Showing it again can be faster than building it from scratch.
.panel {
content-visibility: hidden;
}
.panel.is-open {
content-visibility: visible;
}This fits tabs, collapsed panels, and cached views that the user may return to. It costs memory, because the browser holds on to the state. For content the user will probably never reopen, display: none is cheaper.
A hidden element still needs a size. Without contain-intrinsic-size, its height is zero.
Common errors
- Forgetting
contain-intrinsic-size. Skipped items collapse, the scrollbar is wrong, and the page jumps while scrolling. Always add it. - Applying
autoto elements that are already near the viewport. Nothing is skipped, and you still pay for containment. - Relying on overflow. Content that used to spill past the box is now clipped by paint containment.
- Reading layout from JavaScript. Calling
getBoundingClientRect()on a descendant of a skipped element forces the browser to render it. Many such calls in a loop can erase the gain. - Assuming support. The property is recent in some browsers. Unsupported browsers ignore it, so the page still works but without the speedup. Treat it as progressive enhancement and check a compatibility table before you rely on it.
Verifying the effect
Open the Performance panel and record a page load, with and without the property. Compare the time spent in Recalculate Style, Layout, and Paint. The savings appear at load time and while scrolling. A later chapter on measuring covers the workflow.
Exercise
A card list uses .card { content-visibility: auto; }. While scrolling, the scrollbar thumb changes size and the page jumps. What is the cause, and what single declaration would you add to fix it?
Show answer
When a card is skipped, size containment applies and its height can collapse, because there is no layout for its contents. Add contain-intrinsic-size: auto 300px; (using a height close to a typical card). The browser then uses 300px as the first placeholder and the last rendered height after that.
Exercise
You are building a settings dialog with five tabs. Users often switch between tabs, and each tab is expensive to build. Do you hide inactive tabs with display: none or content-visibility: hidden, and what is the trade-off?
Show answer
Use content-visibility: hidden. The browser keeps the rendering state of the skipped subtree, so switching back is faster than rebuilding it. The trade-off is memory use, since that state is retained. Also give each panel a contain-intrinsic-size, or it will have zero height while hidden.