Rendering PipelineBaseline · Widely

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
containLayoutOn
containPaintOff
containSizeOff
domNodes1200
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.

  1. contain: layout: Turns the element into a layout root and Block Formatting Context. Internal reflows stop at its border.
  2. contain: paint: Clips descendants at the padding edge and creates a CSS Stacking Context. Off-screen paint work can be skipped.
  3. contain: content: Shorthand for layout paint style, safe for auto-sized containers. contain: strict adds size, 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): contain formalized engine-level isolation contracts, later powering @container queries and content-visibility.

Production Pitfalls

Production Pitfall
  • Accidental 0px collapse with contain: strict: Adding size containment ignores child intrinsic height; without an explicit height, the box collapses to zero.
  • Clipped tooltips under contain: paint: Paint containment forces clipping to the padding edge, slicing off position: absolute popovers.
When Not to Use
  • When not to use contain: paint: Do not apply contain: paint or contain: content to containers that must render overflowing dropdowns, focus rings, or shadows outside their bounds.

Connections & Logical Next Steps

Seen in Architecture Autopsies