Sub topic 18
Pitfalls and progressive enhancement
This chapter assumes you know the contain values, content-visibility: auto and hidden, and contain-intrinsic-size, and that you can record a trace in DevTools. It covers the problems you will most likely hit when you ship these properties, and how to ship them so older browsers are not harmed.
Pitfall 1: Clipped content
Paint containment clips descendants to the element's padding box. content-visibility: auto applies paint containment, so it clips too. Anything that visually escapes the box is cut off. Typical victims are:
- dropdown menus and tooltips positioned with
position: absolute box-shadowandoutlineon children near the edge- negative margins and transforms that move a child outside the box
.card {
contain: paint;
}
.card .tooltip {
position: absolute;
top: -40px; /* outside .card, so it is clipped */
}Fix it by moving the overlay out of the contained subtree. A popover can live near the end of <body>, or you can use the top layer through the popover attribute or <dialog>. You can also add padding inside the contained element so the overflow lands inside its box. Removing paint from contain is the last resort, because it gives up the paint savings.
Pitfall 2: Changed containing blocks and stacking contexts
Layout containment and paint containment both make the element a containing block for position: absolute and position: fixed descendants. Both also create a stacking context. This causes two common bugs:
- A
position: fixedbanner inside a.widgetno longer sticks to the viewport. It now positions against.widget. - A child with
z-index: 9999can no longer rise above siblings of its contained ancestor, because itsz-indexonly competes inside the new stacking context.
When a layout bug appears right after adding contain or content-visibility, check the ancestors first. Toggle the property off in DevTools and see whether the bug disappears.
Pitfall 3: Size containment collapses the box
With contain: size, the element ignores its children when it computes its size. If you give no explicit size, it collapses to zero in that dimension.
.badge {
contain: size;
width: 120px;
height: 40px;
}This is a common mistake with contain: strict, which includes size containment. Use contain: content when you do not know the size ahead of time, because it leaves size containment out.
Pitfall 4: Scrollbar jumps
While an element is skipped, content-visibility: auto treats it as size-contained. With no contain-intrinsic-size, its height is zero (only its padding and border count). A long page then looks short, and the scrollbar thumb is too large. As the user scrolls, sections render and grow, so the thumb shrinks and the content can shift under the pointer.
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}The auto keyword makes the browser remember the last rendered size and use it instead of the 600px placeholder once the element has been shown. Your placeholder still matters for the first visit, so pick a number close to the typical real height. A guess that is too low causes large jumps as items render. A guess that is too high leaves blank space.
If heights vary a lot, group content into sections of similar height, or set a different estimate per section type.
.chunk--short { contain-intrinsic-size: auto 300px; }
.chunk--tall { contain-intrinsic-size: auto 1200px; }Pitfall 5: Applying it above the fold
Skipping work only helps for content the user is not looking at. If you put content-visibility: auto on the first screen of content, the browser still has to render it right away. You get the containment side effects with no savings. Apply it to sections below the fold, and leave the hero and header alone.
Also avoid setting it on the whole <body> or on a single huge wrapper. The browser skips on a per-element basis, so one big element is either all rendered or all skipped. Split long pages into many sibling chunks.
Pitfall 6: Reading layout from skipped content
Scripts that call getBoundingClientRect(), offsetHeight, or scrollIntoView() on elements inside a skipped subtree can force the browser to do the rendering work you were trying to avoid. A loop that measures every item defeats the optimization. Measure only what you need, or batch reads after the contents have rendered. Use the contentvisibilityautostatechange event to learn when that happens.
Pitfall 7: Hidden content and discoverability
The two values behave differently for users:
| Value | Rendered state | Find-in-page | Accessibility tree |
|---|---|---|---|
| `auto` (skipped) | Skipped until near the viewport | Text can be found, and the browser renders it on a match | Content stays available |
| `hidden` | Skipped, state kept | Text is not found | Content is not exposed |
Use auto for content that is merely far away, since it remains reachable by search and assistive technology. Use hidden only for content the user should not see, like a closed tab panel. Never use hidden to hide content that people may want to search for.
Progressive enhancement
Both contain and content-visibility are safe to ignore. A browser that does not recognize them renders the page normally, only more slowly. That makes them a good fit for progressive enhancement, as long as your page works without them.
Follow these rules:
- Build the page so it is correct with no containment at all.
- Add containment as an optimization, never as something the layout depends on.
- Wrap the properties that need pairing in
@supports, so a partial implementation cannot give you half of a behavior.
.chunk {
/* baseline: works everywhere */
margin-block: 2rem;
}
@supports (content-visibility: auto) {
.chunk {
content-visibility: auto;
contain-intrinsic-size: auto 800px;
}
}The auto keyword in contain-intrinsic-size was added later than content-visibility itself in some engines. If you rely on it for stable scrolling, test it on its own.
@supports (contain-intrinsic-size: auto 800px) {
.chunk {
contain-intrinsic-size: auto 800px;
}
}Check current browser support tables before you decide how much fallback you need, because support changes between releases.
A debugging routine
After each fix, record a new performance trace and compare it with your earlier one. A change that fixes a bug but removes the gain is not worth keeping.
Exercise
A page of 200 article sections uses content-visibility: auto on each one with no contain-intrinsic-size. Users report that the scrollbar thumb shrinks and the page jumps as they scroll. What causes this, and what is the fix?
Show answer
Skipped sections are treated as size-contained with no content, so each has near-zero height. The page looks short at first, and each section grows when it renders, which moves the content and resizes the thumb. Add contain-intrinsic-size: auto <estimate> (for example auto 800px) so skipped sections reserve a realistic height, and the browser remembers the real height after a section has been shown.
Exercise
You add contain: paint to a .card and its dropdown menu is now cut off. Give two ways to fix it without removing the containment.
Show answer
Move the menu out of the contained subtree, for example into a popover, a <dialog>, or an element near the end of <body>. Or add padding inside .card so the menu's box falls within the padding box.