Sub topic 12
content-visibility: auto in practice
This chapter assumes you know the contain values (layout, paint, size, style), what content-visibility does at a high level, and how contain-intrinsic-size reserves space. Here we apply content-visibility: auto to real pages, cover the edge cases, and fix the errors that show up most often.
What auto does
content-visibility: auto lets the browser decide when to render an element's contents. The element always gets layout, style, and paint containment. When it is not close to the viewport, the browser also applies size containment and skips rendering its contents.
Three consequences follow:
- Off-screen elements cost almost nothing during the first render, so the page becomes interactive sooner.
- The element's own box is still laid out, so it takes up space in the page.
- Because skipped contents have no layout, the element is sized as if it were empty unless you reserve space for it.
The browser starts rendering an element shortly before it scrolls into view. The exact distance is up to the browser, so do not rely on a specific margin.
A first working example
Take a long feed in which every item is a card. Apply the property to each card:
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}The auto keyword in contain-intrinsic-size tells the browser to remember the last rendered size. Until the element has been rendered once, it uses 600px as its height. After that, it uses the measured height even when it is skipped again.
Without any intrinsic size, every skipped item would have a height of zero. The scrollbar would then reflect only the rendered items, and it would jump as you scroll.
Choosing the unit of work
Apply the property to elements that are:
- Many and similar, such as feed items, comments, or table sections.
- Tall enough that skipping them saves real work.
- Independent of each other for layout purposes.
Applying it to tiny elements adds overhead without saving anything. Applying it to a single huge wrapper gains little, because the wrapper is usually near the viewport and renders in full.
A common pattern for a long article or landing page is to split it into sections:
<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>Keep the first section or the area above the fold out of this rule, so the content users see first is never delayed. A simple way is .chunk:not(:first-child), or a class you add only to later sections.
Estimating the intrinsic size
The placeholder size does not have to be exact. It only has to be close enough that the scrollbar does not jump noticeably. To pick a value:
- Open the page and measure a few typical elements in DevTools.
- Choose a value near the median.
- Prefer a slightly low value for lists whose items vary widely, so the scrollbar thumb does not shrink as you scroll down.
If the width is also unknown, you can give two values, such as contain-intrinsic-size: auto 300px auto 600px. The first pair is for width and the second for height. In most pages the block dimension is the only one that matters, and you can use contain-intrinsic-block-size: auto 600px.
Behavior of skipped content
Skipped content is not removed. It stays in the DOM and in the accessibility tree, and the browser keeps the following working:
- Find in page. Text in skipped content can be found, and the browser renders the match when you jump to it.
- Tab order and focus. Focusable elements inside a skipped subtree can still receive focus, and focusing one renders the contents.
- Fragment navigation. Following
#anchorto an element inside skipped content scrolls to it. - Screen readers. Content stays available to assistive technology, so headings and landmarks are still announced.
The next chapter covers these topics in more detail. What matters here is that auto is safe for content that people need to search and read, which is why it is the preferred choice for long pages. hidden behaves differently and is covered separately.
What you can and cannot measure inside
Because skipped elements have no layout, layout-dependent APIs return results that do not reflect the contents:
getBoundingClientRect()on a descendant of a skipped element may return zeros or stale values.offsetHeighton the skipped element itself reflects the placeholder size, not the real contents.
If your script needs to measure items, either do it after the element has rendered or listen for the state change event:
const section = document.querySelector('.chunk');
section.addEventListener('contentvisibilityautostatechange', (event) => {
console.log(event.skipped ? 'skipped' : 'rendered');
});Side effects to remember
Since auto applies paint and layout containment, everything from the earlier side-effects chapter applies:
- The element becomes a containing block for absolutely and fixed positioned descendants.
- It creates a stacking context.
- Content that overflows the box is clipped. A dropdown or tooltip that extends past the edge of a section will be cut off.
- Skipped content gets size containment, so the element's size cannot depend on its children while skipped.
If a section has a popover that must escape its box, either move the popover outside the section in the DOM or do not apply content-visibility to that section.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The scrollbar thumb jumps or shrinks while scrolling | No `contain-intrinsic-size`, or a value far from the real height | Add `contain-intrinsic-size: auto <height>` close to the median height |
| Items collapse to zero height | Size containment with no placeholder | Add an intrinsic size |
| A menu or shadow is cut off | Paint containment clips overflow | Move the overlay outside, or remove the property from that element |
| A fixed-position child sits in the wrong place | The element is now a containing block | Move the child out of the section |
| The first screen renders late | The property was applied above the fold | Exclude the first sections |
| Layout shifts when jumping to an anchor | Estimated height differs from the real height | Improve the estimate and use the `auto` keyword |
Progressive enhancement
Browsers that do not support content-visibility ignore it and render everything, so the page still works, only slower. You do not need a fallback for that case. If you want to apply the rule only where it is supported, use a feature query:
@supports (content-visibility: auto) {
.chunk {
content-visibility: auto;
contain-intrinsic-size: auto 800px;
}
}This is rarely required, but it keeps related declarations together and makes intent clear.
Verifying that it helps
Do not assume a speedup. Record a performance trace before and after, and compare the time spent in Recalculate Style, Layout, and Paint during load. The gain is largest on pages with thousands of nodes. On small pages, the change may be invisible or may slightly add cost from the extra containment work.
Exercise
A page lists 500 comments, each about 150px tall. You add content-visibility: auto to every comment without any other change. What problem will users see while scrolling, and what single declaration fixes it?
Show answer
Skipped comments have size containment and no content, so their height is zero. The page is short at first, and the scrollbar thumb jumps and grows as comments are rendered. Adding contain-intrinsic-size: auto 150px reserves space for skipped comments, and the auto keyword remembers each comment's real height after it has rendered.
Exercise
A section with content-visibility: auto contains a dropdown menu that extends below the section's bottom edge. The menu is cut off. Why does this happen, and what are two ways to fix it?
Show answer
content-visibility: auto applies paint containment, which clips descendants to the element's box. You can move the dropdown's markup outside the section so it is not a descendant, or remove content-visibility from that section.