There are four types of containment: layout, paint, size, and style. Use the contain
property to specify the type or types you want to apply to an element by including any combination of these types.
Layout containment
article {
contain: layout;
}
Layout is normally scoped to the entire document, which means that if you move one element the entire document needs to be treated as if things could have moved anywhere. By using contain: layout
you can tell the browser it only needs to check this element — everything inside the element is scoped to that element and does not affect the rest of the page, with the containing box establishing an independent formatting context.
In addition:
-
float
layout will be performed independently inside the specified element. - Margins won't collapse across a layout containment boundary.
- The layout container is a containing block for
absolute
- and fixed
-positioned descendants. - The containing box creates a stacking context, therefore
z-index
can be used.
Paint containment
article {
contain: paint;
}
Paint containment essentially clips the box to the padding edge of the principal box. There can be no visible overflow. The same additional notes are true for paint
containment as layout
containment (see above).
Another advantage is that if the element with containment applied is offscreen, the browser does not need to paint its child elements — these are also offscreen as they are contained completely by that box.
Size containment
article {
contain: size;
}
Size containment does not offer much in the way of performance optimizations when used on its own. However, size containment means that the size of the size-contained element's children cannot affect the size of the element itself — its size is computed as if it had no children.
If you set contain: size
on an element, you need to specify the size of the element using contain-intrinsic-size
, or the longhand properties contain-intrinsic-width
and contain-intrinsic-height
, in that order. If no size is set, the element risks being zero-sized in most cases.
article {
contain: size;
contain-intrinsic-size: 100vw auto;
}
Style containment
article {
contain: style;
}
Despite the name, style containment does not provide scoped styles such as you would get with the Shadow DOM or @scope
. The main use case for the style
value is to prevent situations where a CSS counter could be changed in an element, which could then affect the rest of the tree.
Using contain: style
ensures the counter-increment
and counter-set
properties create new counters scoped to that subtree only.
You can include more than one containment type by including multiple space-separated values, such as contain: layout paint
or by using one of the two special values.
Special values
There are two special values of contain
that are shorthand for the first three or all four of the containment types:
We encountered the first in the example above. Using contain: content
turns on layout
, paint
, and style
containment. As it omits size
, it is a safe value to apply widely.
The contain: strict
declaration, which behaves the same as the declaration contain: size layout paint style
(which includes four space-separated values), provides the most containment. It is riskier to use as it applies size
containment; the risk exists that a box could end up zero-sized due to a reliance on the size of its children.
To remove this risk, always set a size when using strict
:
article {
contain: strict;
contain-intrinsic-size: 80vw auto;
}
The above is the same as:
article {
contain: size layout paint style;
contain-intrinsic-size: 80vw auto;
}