Sub topic 18
Pitfalls and progressive enhancement
This chapter assumes you know the contain values, the side effects of containment, and how content-visibility: auto works with contain-intrinsic-size. It covers the mistakes that appear most often in real pages, and how to ship the feature safely to browsers that lack it.
Common pitfalls
Scrollbar jumps
A skipped element has no content, so its height comes from contain-intrinsic-size. If that value is far from the real height, the page height changes when the element is first rendered. The scrollbar thumb jumps and the content under the reader shifts.
Use the auto keyword so the browser remembers the last rendered size, and pick a placeholder close to the typical height.
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}Collapsed size
content-visibility: auto applies size containment while the element is skipped. With no intrinsic size, the element collapses to zero height, and many sections can then appear on screen at once. All of them render, which removes the benefit.
Clipped content
Paint containment clips descendants to the element's bounds. Tooltips, dropdown menus, and focus rings that extend past the edge disappear. Layout containment also makes the element a containing block for absolutely and fixed positioned descendants, so a position: fixed child no longer anchors to the viewport.
Anchors and focus
Links to an id inside a skipped section still work, but the browser must render the section to find the target. If the placeholder height is wrong, the scroll position can land off target. Test your in-page navigation.
Containing too much
Applying contain: strict to a wrapper whose size depends on its children collapses it. Start with the weakest value that solves the problem.
| Symptom | Likely cause | Fix |
|---|---|---|
| Scrollbar jumps while scrolling | Placeholder height far from real height | Use `auto` with a realistic estimate |
| Element has zero height | Size containment without explicit size | Set `contain-intrinsic-size` or a real size |
| Tooltip cut off | Paint containment clips overflow | Move the tooltip outside the contained element |
| Fixed child scrolls with the page | Layout containment creates a containing block | Move the child out of the contained subtree |
Progressive enhancement
Containment properties are optimizations. A browser that ignores them still renders a correct page, only more slowly. That makes them a good fit for progressive enhancement: write the page so it works without them, then add them where they are supported.
You can scope the rule with @supports so unsupported browsers receive no half-applied styles.
@supports (content-visibility: auto) {
.chunk {
content-visibility: auto;
contain-intrinsic-size: auto 800px;
}
}Keep these habits:
- Do not rely on containment for correctness. The layout should look the same with and without it.
- Do not hide content in a way that only
content-visibilitycan reveal. - Measure in DevTools before and after, and keep the rule only if the numbers improve.
Exercise
A long article uses .chunk { content-visibility: auto; } with no other rules. Readers report that the scrollbar behaves oddly and the page seems slow to scroll. What is wrong, and how do you fix it?
Show answer
Skipped chunks have no intrinsic size, so each collapses to zero height. Many chunks fit in the viewport and render at once, and the page height changes as chunks render. Add contain-intrinsic-size: auto 800px (or a value near the real height) so skipped chunks reserve space and the browser remembers the rendered size.