Sub topic 3
`content-visibility: auto` in Practice
Assume you know CSS selectors, layout, overflow, positioning, and basic DevTools. This chapter focuses on choosing and measuring skipped rendering.
Use `content-visibility: auto`
content-visibility: auto lets the browser skip rendering an element while it is far from the viewport. The browser keeps the element in the document, so scrolling remains possible. When the element approaches the viewport, the browser renders it.
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}contain-intrinsic-size supplies a placeholder size while the item is skipped. The auto keyword lets the browser remember the last measured size after rendering. Without a reasonable placeholder, the scrollbar can jump when content becomes visible.
Use `hidden` for preserved panels
Use content-visibility: hidden when an interface intentionally hides content but may show it again soon.
.panel {
content-visibility: hidden;
}
.panel.is-open {
content-visibility: visible;
}This preserves the rendering state better than rebuilding the panel with JavaScript. It is useful for tabs, cached views, and closed panels. Unlike auto, it does not become visible because of proximity to the viewport.
Accessibility and state changes
Skipped auto content remains part of the document and accessibility tree in normal use. It can also remain searchable with find-in-page. Test important content with your target browsers and screen readers, especially when scripting visibility.
The contentvisibilityautostatechange event reports when auto content starts or stops being skipped:
const section = document.querySelector(".feed-item");
section.addEventListener("contentvisibilityautostatechange", (event) => {
section.classList.toggle("is-skipped", event.skipped);
});Containment and container queries
Containment also supports container queries. container-type: inline-size makes an element a query container and prevents its inline size from depending on its children.
.sidebar {
container-type: inline-size;
}
@container (min-width: 400px) {
.sidebar .item {
display: flex;
}
}Measure before and after
Record a page in DevTools Performance, scroll through the slow area, and compare rendering and layout time before and after containment. Check that scrolling remains stable and that visible content appears without delay.
> callout note > Containment is an optimization, not a guarantee. Keep the rule only when profiling shows less rendering work.
Pitfalls and progressive enhancement
Use realistic intrinsic sizes. A poor estimate causes scrollbar movement. Remember that containment can change positioning, stacking, and overflow behavior. Paint containment can clip descendants that extend outside their element.
Use a fallback because unsupported browsers ignore the declaration:
.feed-item {
contain-intrinsic-size: auto 600px;
}
@supports (content-visibility: auto) {
.feed-item {
content-visibility: auto;
}
}Exercise
Which property helps reserve space while an off-screen element uses content-visibility: auto?
Show answer
contain-intrinsic-size, ideally with a realistic placeholder size.