Sub topic 3
The contain property at a glance
This chapter assumes you know CSS selectors, the box model, positioning, and overflow. It also assumes you can open browser DevTools. It introduces the contain property and its values. Later chapters cover each value in detail.
What contain does
contain is a promise you make to the browser. You state that an element's subtree is independent of the rest of the page in some specific way. The browser can then limit style, layout, or paint work to that subtree instead of rechecking the whole document after every change.
The property is not inherited, and its initial value is none. Setting a value does not change how the element looks by default. It restricts what the subtree is allowed to affect, and the browser enforces that restriction.
The values
| Value | What you promise |
|---|---|
| `layout` | Nothing inside affects layout outside, and nothing outside affects layout inside. |
| `paint` | Descendants are not drawn outside the element's bounds. |
| `size` | The element's size does not depend on its contents. |
| `inline-size` | The same as `size`, but only in the inline direction. |
| `style` | Certain style effects, such as counters, do not escape the subtree. |
| `content` | A shorthand for several of the values above. |
| `strict` | A shorthand for most of the values above. |
You can combine the individual values by separating them with spaces.
.card {
contain: layout paint;
}The two shorthands, content and strict, have their own chapter. For now, treat them as ready-made bundles.
A first example
Here is a widget that isolates its layout and paint work.
.widget {
contain: layout paint;
width: 300px;
}With this rule, the browser treats .widget as a boundary. If a script adds or resizes a child inside it, the browser does not need to re-lay out the surrounding page because of that change. Anything inside that overflows the widget's box is clipped.
Common errors
The most frequent mistake is using size without giving the element a size. Size containment makes the browser lay out the element as if it were empty, so it collapses.
.badge {
contain: size;
width: 120px;
height: 40px;
}The second mistake is adding paint and then wondering why a tooltip or dropdown is cut off. Paint containment clips descendants to the element's bounds.
A third, subtler effect is that some values change how the element behaves for positioning and stacking. We cover those side effects in a later chapter. Check them before you apply contain to a component that has absolutely positioned children.
Exercise
You add contain: size to a .panel element that has no width or height set, and the panel disappears from the page. Why did this happen, and how do you fix it?
Show answer
Size containment makes the browser size the element as if it had no contents, so it collapses to zero in the contained dimension. Fix it by giving the element an explicit width and height, or by using contain: layout paint if you do not need size independence.