Isolate subtrees for faster rendering
CSS containment lets you promise the browser that an element’s subtree is
independent of the rest of the document, so the engine can skip work when that
subtree changes. This reference covers the contain keywords, the
content-visibility shortcut, intrinsic sizing and @container query syntax.
How it works
The contain property accepts one or more independent keywords, plus the
shorthands content (layout paint style) and strict (layout paint style size):
.widget { contain: layout paint; } /* isolate layout + paint */
.card { content-visibility: auto; /* skip off-screen render */
contain-intrinsic-size: auto 200px; }
.panel { container-type: inline-size; } /* become a query container */
@container (min-width: 40rem) {
.panel .title { font-size: 1.5rem; }
}
content-visibility: auto applies layout paint size containment and skips
rendering until the element nears the viewport; contain-intrinsic-size supplies
a placeholder size so scrolling stays stable.
Tips and notes
contain: sizecollapses an element with no explicit dimensions — preferinline-sizefor container queries.content-visibility: autois the highest-leverage value for long lists; always pair it withcontain-intrinsic-size.container-type: inline-sizeenables width-based@containerqueries without breaking block height;container-type: sizerequires a fixed height too.- Name containers with
container-nameto target a specific ancestor:@container sidebar (min-width: …). - Containment can hide overflow and break
position: fixeddescendants — test sticky/fixed children after applying it.