Sub topic 10
content-visibility: containment plus skipped rendering
This chapter assumes you know the contain values (layout, paint, size, style) and their side effects from the earlier chapters. It also assumes you know how style, layout, and paint make up the rendering pipeline.
From isolating a subtree to skipping it
Containment tells the browser that a subtree is independent. The browser can then limit its work to that subtree, but it still renders everything. content-visibility goes one step further. It applies containment and also lets the browser skip rendering the element's contents.
When contents are skipped, the browser does no style, layout, or paint work for the descendants. The elements stay in the DOM, so scripts can still query them and the markup is unchanged. The element's own box is still laid out.
The three values
| Value | Containment applied | Contents skipped |
|---|---|---|
| `visible` | None. This is the default. | Never |
| `auto` | Layout, style, and paint | When the element is not near the viewport |
| `hidden` | Layout, style, paint, and size | Always |
With auto, the browser decides when to skip. While the element is off-screen, it also applies size containment. Once the element approaches the viewport, the browser renders it and drops the size containment.
With hidden, you decide. The element's contents stay skipped until you change the value.
.chunk {
content-visibility: auto;
}This one declaration gives .chunk layout, style, and paint containment. It also lets the browser skip the contents while the element is far from the viewport.
Why size containment matters
Skipped contents cannot contribute to the element's size, because the browser has not laid them out. The element is treated as if it had size containment, and with no explicit size it collapses to zero in that dimension. A page of many collapsed sections is much shorter than the real page. The scrollbar thumb then changes size and position as sections render, which causes visible jumps.
The fix is to give skipped elements a placeholder size with contain-intrinsic-size. The next chapters cover that property and each value in detail.
What the decision looks like
Common mistakes
- Setting
content-visibility: autoon an element that is always on-screen. It adds containment side effects and saves no work. - Forgetting that paint containment clips overflow. Content that spills outside the element's box, such as a tooltip, gets cut off.
- Applying
hiddenand expecting the element to keep its size. It has size containment, so it needs explicit dimensions.
Exercise
A page has fifty long sections, each with content-visibility: auto and no size set. Users report that the scrollbar jumps while scrolling. What causes this, and what is the fix?
Show answer
Off-screen sections are skipped and get size containment, so they collapse to zero height. As each section is rendered, the page height changes and the scrollbar jumps. Give the sections a placeholder size with contain-intrinsic-size, for example contain-intrinsic-size: auto 800px.