Sub topic 6
Size containment
This chapter assumes you know the CSS box model, how width and height resolve to auto, and how overflow works. It builds on the contain property from the previous chapters.
What size containment guarantees
contain: size tells the browser that the element's own size does not depend on its contents. The browser sizes the box as if it had no children. It never has to lay out the descendants to find out how big the element is.
That guarantee helps layout of the surrounding page. If the element's size is known without looking inside, a change inside it cannot resize the element and push its neighbors around.
A working example
.badge {
contain: size;
width: 120px;
height: 40px;
}Because the contents are ignored when sizing, the explicit width and height are the only source of size. Without them, an auto dimension resolves as if the element were empty.
<div class="badge">A label that is much longer than the badge</div>With the CSS above, the badge stays 120px by 40px no matter how long the label is.
The most common error: collapse
Remove the width and height from the rule above and the badge collapses. Its auto height becomes 0, and an inline-block or flex item also gets 0 width. A block-level box still fills its container's width, but its height is 0. The content is still there, but it sits in a box with no height, so it can overlap what follows.
Rule of thumb: whenever you write contain: size, give the element a size in both dimensions in the same rule.
Size containment does not clip
A second common mistake is expecting overflowing content to be hidden. Size containment only changes how the element is sized. It does not clip. A child that is larger than the box still draws outside it, unless you also set overflow or use paint containment.
.badge {
contain: size paint;
width: 120px;
height: 40px;
}Adding paint clips descendants to the box, so the long label no longer spills out.
Where you will meet it again
Size containment is rarely useful alone, because you must already know the element's size. Two features build on it:
content-visibilityapplies size containment while it skips an element's contents. The browser cannot measure content it is not rendering, so it needs a stand-in size. You supply one withcontain-intrinsic-size, covered in a later chapter.container-type: inline-sizeapplies size containment in the inline axis only. The container's width cannot depend on its children, which lets the browser resolve@containerqueries without circular dependencies.
| Value | Axes whose size ignores contents |
|---|---|
| `contain: size` | Both |
| `contain: inline-size` | Inline only |
Exercise
A .thumb element has contain: size but no width or height, and it holds an image. It is a flex item in a row. What do you expect to see, and how do you fix it?
Show answer
It collapses to zero size because its contents no longer contribute to its size, so the image overflows or is invisible. Fix it by giving .thumb an explicit width and height, and add contain: size paint or overflow: hidden if overflowing content must be clipped.