Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Box Model and Sizing

The contain property

Reference for the CSS contain property: isolate a subtree so layout, paint, style and size work stays local. Values, strict and content shorthands, examples.

The contain property is a promise to the browser: "what happens inside this element does not affect the rest of the page, and the other way round". With that promise, the browser can skip work, for instance by not re-laying-out the whole document when one widget changes. The visible side effects are that some kinds of containment also change how the element behaves.

CSS
.widget {
  contain: content;
}

Try it

Paint containment clips and isolates

In the first frame the pink child paints outside its parent. With contain: paint it is clipped to the parent's padding box, because the browser is allowed to assume that nothing inside is visible outside.

Syntax

CSS
contain: none | strict | content | [ size || layout || style || paint ]
  • Initial value: none
  • Applies to: all elements (size and paint have no effect on non-atomic inline boxes, and size has no effect on table elements that do not generate a box)
  • Inherited: no
  • Animatable: no (discrete)
  • Computed value: as specified

Values

Value Description
none No containment.
size The element's size does not depend on its children. It lays out as if empty, so give it an explicit size.
layout Nothing inside affects layout outside, and the element becomes a containing block for fixed and absolute descendants and gets its own stacking context.
style Counters and quotes set inside do not escape the element.
paint Descendants are clipped to the element's box and do not paint outside it. The element also becomes a containing block and a stacking context.
content Shorthand for layout paint style.
strict Shorthand for size layout paint style.
inline-size Containment of the inline size only; this is what container-type: inline-size uses behind the scenes.

Usage notes

  • Visible side effects. layout and paint change containing-block and stacking behavior. A position: fixed child of a contain: paint element is positioned against it, not the viewport.
  • strict needs a size. With size containment and no explicit width or height, the element collapses to zero in that dimension.
  • Good candidates are self-contained components that update often: list items, comment cards, ads, embedded widgets.
  • Measure before using it. Containment helps most when a page has many independent parts. On a simple page it has no measurable effect.
  • Related automatic mode. content-visibility: auto applies containment for you while the element is off screen.

Browser support

Supported in current versions of all major browsers. The size, layout, style and paint keywords have been available for several years, so the property is part of Baseline.