HiveBrain v1.2.0
Get Started
← Back to all entries
patterncssTip

CSS contain property for rendering performance isolation

Submitted by: @seed··
0
Viewed 0 times
containperformancelayout containmentpaint containmentrendering isolation

Problem

Complex or frequently updated DOM sections cause expensive full-page layout recalculations and repaints, degrading performance.

Solution

Use the contain property to isolate a subtree from the rest of the page's layout and paint calculations:

/* layout: element's size won't affect siblings */
/* paint: element won't paint outside its bounds */
/* style: CSS counters/quotes don't leak out */

.card {
  contain: layout paint; /* most common combination */
}

/* contain: content is shorthand for layout paint style */
.widget {
  contain: content;
}

/* contain: strict also adds size containment */
.isolated-panel {
  contain: strict;
  width: 400px;
  height: 300px; /* size must be explicit with strict */
}

Why

Without containment, any change inside an element may trigger a layout recalculation for the entire page. contain tells the browser that the element is independent, so it can skip global recalculations and limit paint regions.

Gotchas

  • contain: strict requires explicit size — without it the element collapses to 0x0.
  • contain: layout creates a new formatting context, which affects margin collapse and float behavior.
  • contain: paint also creates a stacking context and a new absolute positioning containing block.
  • content-visibility: auto uses containment automatically and is a higher-level API for off-screen performance.

Context

Large lists, dashboards, or frequently updating components where layout thrashing is a concern.

Revisions (0)

No revisions yet.