Sub topic 3
The contain property at a glance
This chapter assumes you know CSS selectors, the box model, positioning, and overflow. You should also know the idea from the previous chapters: the browser can do less work if you promise that a subtree is independent of the rest of the page. Here you will learn the syntax of the contain property, what each value promises, and how the values combine. Each type of containment gets its own chapter after this one, so this chapter stays at overview level.
What the property does
contain is a promise from you to the browser. You tell it that an element's subtree is isolated in specific ways, and the browser uses that promise to skip work. For example, when something inside the element changes, the browser does not have to recalculate layout for the whole document.
The browser enforces the promise. It applies rules that make the isolation true, so an element with containment behaves differently from one without it. This is why containment is not a free hint. You trade some layout freedom for speed.
Key facts about the property:
- The initial value is
none. - It is not inherited. You set it on each element that needs it.
- It applies to all elements except
display: noneelements, internal table boxes, and non-atomic inline boxes. Plain<span>text is an example of the last kind. Setdisplay: inline-blockorblockfirst if you want containment on it. - Browsers can skip the work for a contained subtree, so the savings grow with subtree size.
The values
The property accepts either none, one of two shorthand keywords, or a space-separated list of individual types.
| Value | What it promises |
|---|---|
| `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` | Size containment in the inline dimension only. |
| `style` | Certain style effects, such as counters, do not escape the subtree. |
| `content` | Shorthand for `layout paint style`. |
| `strict` | Shorthand for `size layout paint style`. |
| `none` | No containment. |
Per the specification, strict and content include style containment. Some browser versions handled this differently while the spec was changing, so avoid relying on the style part of the shorthands.
One example of each main type
Each rule below shows a single value in use.
.widget {
contain: layout;
}Layout containment tells the browser that nothing inside .widget affects layout outside it, and nothing outside affects the layout of its contents.
.card {
contain: paint;
}Paint containment tells the browser that descendants are not drawn outside the element's bounds. The browser can skip painting the element when it is off-screen.
.badge {
contain: size;
width: 120px;
height: 40px;
}Size containment makes the element's size independent of its contents. You must give it an explicit size, or it collapses to zero in that dimension. The explicit width and height above are required, not decoration.
Combining values
List several types separated by spaces. Order does not matter.
.panel {
contain: layout paint;
}This rule is equivalent to contain: layout paint, which is also what content gives you apart from the style part. The rules for combining are simple:
none,strict, andcontentmust appear alone. A value such ascontain: strict paintis invalid.- Individual types can be mixed freely:
layout,paint,size,style, andinline-size. sizeandinline-sizeare alternatives. Use one or the other.- An invalid declaration is dropped, so the element gets no containment at all. A typo fails silently.
Choosing a starting point
You usually do not need to pick types from first principles. Use this decision order:
- Start with
contain: contentfor a self-contained block whose height depends on its contents, such as a card or a list item. It gives layout and paint isolation without forcing you to set a size. - Use
contain: strictonly when you can state the element's size up front, for example a fixed-size widget or an embed area. Without a size, it collapses. - Use a single type when you need exactly one guarantee, or when the other types would break something. For example, use
contain: layoutif you want isolation but need descendants such as dropdowns to be visible outside the bounds. - Use
contain: inline-sizewhen you need a width that does not depend on children, as in container queries. A later chapter covers that link.
Measure before and after. Containment on small elements rarely helps, and each type has side effects, such as creating a new containing block or clipping overflow, which the later chapters describe.
Checking support from JavaScript
Browsers that do not know a value ignore it. You can test for support before relying on it. The optional code below is complete and runs in a browser console.
const supported = {
layout: CSS.supports('contain', 'layout'),
paint: CSS.supports('contain', 'paint'),
size: CSS.supports('contain', 'size'),
style: CSS.supports('contain', 'style'),
inlineSize: CSS.supports('contain', 'inline-size'),
content: CSS.supports('contain', 'content'),
strict: CSS.supports('contain', 'strict'),
};
console.table(supported);In CSS alone, the same check is @supports (contain: paint) { ... }. Because ignoring contain is harmless for correctness, most pages can apply it without a fallback. The page just loses the speed benefit.
Common mistakes at this level
- Setting
contain: sizeorstrictwithout a width and height, then wondering why the element is invisible. - Expecting
containto inherit. A child of a contained element is not itself contained. - Applying
contain: paintto an element whose children must overflow visibly, such as tooltips and dropdown menus. They get clipped. - Adding containment everywhere. Containment has a cost in flexibility, and the benefit only shows for subtrees that are large or change often.
Exercise
A product card has variable height because its text length differs per product. You want the browser to isolate its layout and paint work, but you cannot give it a fixed size. Which contain value fits, and why would contain: strict be a bad choice?
Show answer
Use contain: content, which gives layout and paint containment (plus style) and leaves the size dependent on the contents. contain: strict includes size containment, so the card's size would no longer depend on its text. Without an explicit width and height, it would collapse to zero in the contained dimension and hide the content.
Exercise
What happens to the element's containment if you write contain: strict paint;?
Show answer
The declaration is invalid because strict must appear alone, so the browser ignores the whole declaration and the element gets no containment. Write contain: strict or list individual types, such as contain: size layout paint.