CSS Containment
Mental Model
CSS Containment (contain) installs firewalls inside the rendering tree. By promising that a subtree's layout, paint, style, or size is independent, the engine stops invalidation walks at the container boundary instead of recalculating the whole page.
Interactive Simulator
Containment Boundary Impact Simulator
Toggle layout and paint containment to observe subtree invalidation scope.
Parameter Profile
containLayout
On
containPaint
Off
containSize
Off
domNodes
1200
Parameters evaluated in real time.
Under the Hood
Containment acts like watertight bulkheads in a ship hull: a leak inside one compartment cannot flood adjacent cabins.
contain: layout: Turns the element into a layout root and Block Formatting Context. Internal reflows stop at its border.contain: paint: Clips descendants at the padding edge and creates a CSS Stacking Context. Off-screen paint work can be skipped.contain: content: Shorthand forlayout paint style, safe for auto-sized containers.contain: strictaddssize, requiring explicit dimensions.
CSS
/* Safe bulkheads for independent feed cards */
.feed-card {
contain: content; /* layout + paint + style; auto-height preserved */
}
/* Requires explicit dimensions or aspect-ratio */
.widget-slot {
contain: strict;
height: 240px;
}Problem Genealogy
- 2014–2016: Virtualized lists and dashboards suffered O(n) reflows because engines had to assume any child mutation could push siblings.
- 2019 (CSS Containment L1/L2):
containformalized engine-level isolation contracts, later powering@containerqueries andcontent-visibility.
Production Pitfalls
Production Pitfall
- Accidental 0px collapse with
contain: strict: Addingsizecontainment ignores child intrinsic height; without an explicitheight, the box collapses to zero. - Clipped tooltips under
contain: paint: Paint containment forces clipping to the padding edge, slicing offposition: absolutepopovers.
When Not to Use
- When not to use
contain: paint: Do not applycontain: paintorcontain: contentto containers that must render overflowing dropdowns, focus rings, or shadows outside their bounds.
Connections & Logical Next Steps
Useful Prerequisites
CSS Containment
What This Unlocks
What This UnlocksBaseline · Newly
content-visibility & Skipping Offscreen Work
Rendering Pipeline
content-visibility: auto internally activates layout, style, and paint containment.
Useful PrerequisitesBaseline · Widely
Render Tree & Layout
Rendering Pipeline
Layout containment prevents subtree geometry changes from invalidating ancestor boxes.
Useful PrerequisitesBaseline · Widely
Paint & Rasterization
Rendering Pipeline
Paint containment clips descendants and bounds DisplayItem invalidation rects.