Sub topic 12
content-visibility: auto in practice
This chapter assumes you know the contain property, what size containment does, and how contain-intrinsic-size reserves space. It covers how to apply content-visibility: auto to a real page.
What auto does
With content-visibility: auto, the browser applies layout, style, and paint containment to the element. While the element is far from the viewport, the browser also applies size containment and skips rendering its contents. The element stays in the DOM, so selectors, scripts, and querySelector still work. When the user scrolls close to it, the browser renders the contents.
Apply it to a repeated block
Use it on elements that repeat and are usually off-screen: feed items, comment threads, or long article sections.
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}Skipped content has no layout, so size containment would shrink the element to zero height. contain-intrinsic-size gives it a placeholder size. The auto keyword tells the browser to remember the last rendered size and use that instead of 600px once the element has been shown.
Chunk a long page
Split a long page into independent sections and mark each one.
<main>
<section class="chunk">...</section>
<section class="chunk">...</section>
<section class="chunk">...</section>
</main>
<style>
.chunk {
content-visibility: auto;
contain-intrinsic-size: auto 800px;
}
</style>Only chunks near the viewport pay for style, layout, and paint during the initial render. The rest cost almost nothing until the user scrolls to them.
Choosing good targets
- Pick elements that are large enough to save real work. Skipping a one-line paragraph is not worth the bookkeeping.
- Make the placeholder close to the real height. A large gap between the estimate and the real size makes the scrollbar jump as sections render.
- Do not put it on elements that are already in the first screen. They render immediately, so you gain nothing.
- Keep each chunk self-contained. Paint containment clips content that overflows the box, such as a tooltip or a dropdown that extends past the edge.
Check the result
Because skipped content is still in the DOM, text search and the accessibility tree still reach it. Layout values for skipped elements are not up to date, though. Reading offsetHeight of a skipped element's children can return values based on the placeholder. If your script needs real sizes, listen for the state change event:
const section = document.querySelector('.chunk');
section.addEventListener('contentvisibilityautostatechange', (event) => {
console.log(event.skipped ? 'skipped' : 'rendered');
});Exercise
A page has 200 article sections. After you add content-visibility: auto with contain-intrinsic-size: 800px (no auto keyword), the scrollbar thumb jumps when the user scrolls back up past sections they already viewed. What change reduces the jumping, and why?
Show answer
Change the value to contain-intrinsic-size: auto 800px. With auto, the browser remembers each section's last rendered height. When a section is skipped again, it keeps that real height instead of falling back to the 800px guess, so the total page height stays stable.