content-visibility and Contain
The browser's rendering pipeline runs style calculation, layout, paint, and composition for every element in the document, not just the ones visible in the viewport. On a page with hundreds of cards, a long comment section, or a feed of articles, the browser lays out and paints content the user has not scrolled to yet. CSS containment gives you a way to change that.
The Off-Screen Rendering Problem
The browser keeps full layout information for the entire document so it can correctly size the scrollbar, resolve percentage-based heights, and place anchor links. That means every element participates in layout on every pass, whether it is on screen or not. On a page with two hundred product cards each containing images, text, and nested elements, that work adds up quickly every time something triggers a layout recalculation.
A subtle animation elsewhere on the page, a font swap, or a late-loading image can all force a full layout pass across elements the user cannot see. The impact is most visible on content-heavy pages: news feeds, documentation with long code examples, e-commerce grids, and comment sections. These are exactly the pages where performance matters most.
content-visibility: auto
Adding content-visibility: auto to an element tells the browser it can skip layout, paint, and compositing for that element when it is outside the viewport. The browser maintains a buffer zone slightly larger than the visible area, so elements just off-screen render before the user scrolls to them. The transition is seamless.
.card {
content-visibility: auto;
}
The auto value is what you will use in almost every case. There is also content-visibility: hidden, which skips rendering regardless of scroll position, similar in cost to display: none but without removing the element from layout. That value has narrow use cases and is not covered here.
One thing content-visibility: auto does not do is remove elements from the accessibility tree. Screen readers can still find and navigate off-screen content. The property is a rendering optimization only.
Chrome, Edge, Firefox 125+, and Safari 18+ support content-visibility (about 92% global usage per Can I Use). Opera Mini does not. Check Can I Use before shipping to production.
How CSS Containment Works
content-visibility: auto works by automatically applying CSS containment to off-screen elements. Understanding containment explains why it is safe for the browser to skip layout for those elements. The contain property tells the browser that an element's subtree is independent from the rest of the document. Changes inside the element do not affect anything outside it, and the browser can rely on that guarantee to do less work.
contain: layout
Layout containment isolates an element's internal layout from the rest of the page. Floats do not escape the container. Margins do not collapse with elements outside. Positioned descendants are positioned relative to the contained element. The browser can lay out this element without re-checking anything else on the page.
contain: style
Style containment scopes CSS counters and quotes to the element. Changes to a counter inside do not ripple outward. This value is rarely useful on its own but contributes to full containment.
contain: paint
Paint containment means the element's content is not painted outside its border box. The browser also treats the element as a new stacking context and as a containing block for absolutely positioned descendants. If the element is fully outside the viewport, the browser can skip painting it entirely.
When you apply content-visibility: auto, the browser automatically applies all three of these for off-screen elements. You do not need to set contain manually.
/* content-visibility: auto applies this automatically for off-screen elements */
.card {
contain: layout style paint;
}
Placeholder Sizing with contain-intrinsic-size
When the browser skips layout for an off-screen element, it does not know how tall that element will be. Without any size information it treats the element as zero-height. If all your off-screen cards collapse to zero, the scrollbar thinks the page is much shorter than it is. As you scroll down and cards render in, their real heights push the page taller and the scrollbar thumb lurches upward. The page jumps.
contain-intrinsic-size gives the browser an estimated size to use while an element is off-screen and its layout is skipped.
.card {
content-visibility: auto;
contain-intrinsic-size: auto 200px;
}
The auto keyword at the start is what makes this work well in practice. It tells the browser to use the real rendered height once it has seen this element, and fall back to 200px only for elements it has not rendered yet. Without auto, the browser always uses your estimate, even after it has rendered the element and knows the actual height. Your estimate does not need to be precise. A rough average of your card height is enough to keep the scrollbar stable.
Enabling Paint Flashing in DevTools
Paint flashing highlights every area the browser repaints on each frame. It is the most direct way to see content-visibility: auto working.
Open Chrome DevTools with F12 or Ctrl+Shift+I (Cmd+Option+I on Mac). At the top right of the DevTools panel, click the three-dot menu and choose More Tools, then Rendering. The Rendering panel opens in the drawer at the bottom. Check the box next to Paint Flashing.
Scroll your demo page slowly. Any area the browser repaints that frame flashes green. Without content-visibility: auto, nearly every visible card flashes on each scroll step. With the property applied, only the cards entering the viewport light up. Cards already on screen that are not changing do not flash. Cards below the fold do not flash at all.
Build Your Demo
Download the content-visibility starter files and extract them into your portfolio at unit-5/content-visibility/. The zip includes index.html with 40 cards already in the markup and a linked css/main.css file with step comments for you to build out. Showcase the demo: stack the cards in a single column with comfortable padding and margin (or list gap) so the page scrolls well past the viewport. Style the page first, then add content-visibility: auto and contain-intrinsic-size and compare paint flashing in DevTools before and after.
Teaching Video Checklist
When you record your teaching video, make sure your teammates come away understanding these concepts:
- Why the rendering pipeline processes off-screen content by default and where that cost shows up on long pages
-
What
content-visibility: autoskips, and that off-screen elements remain in the accessibility tree -
How
contain-intrinsic-sizeprevents scrollbar jump when layout is skipped -
What the
containproperty does and why it is the mechanism behindcontent-visibility -
When you would reach for
content-visibility: autoand when you would not -
Your portfolio demo: paint flashing in DevTools before and after you add
content-visibilityandcontain-intrinsic-size
Start with Paint Flashing enabled and scroll your long card list before the performance properties are applied, then add them and scroll again so teammates can see which regions stop repainting.
Resources
- MDN: content-visibility - Full property reference including the hidden value and browser compatibility notes
- MDN: contain - All containment values with examples and interaction notes
- MDN: contain-intrinsic-size - Size estimation syntax including the auto keyword behavior
- Can I Use: content-visibility - Current browser support data
- Can I Use: CSS Containment - Support for the contain property