Sub topic 6
Size containment
This chapter assumes you know the CSS box model, how width, height and auto sizing work, and how to inspect an element's computed size in DevTools. You should also have read the previous chapters on layout and paint containment.
What size containment guarantees
Size containment is the value contain: size. It tells the browser that the element's own size does not depend on its contents. The browser can compute the element's box without looking at its children.
Normally the browser must lay out a child before it knows how tall its parent is. With size containment, the browser sizes the element as if it were empty. It then lays out the contents inside that box.
.badge {
contain: size;
width: 120px;
height: 40px;
}Here the badge is always 120px by 40px. Adding text, images or nested elements inside it never changes that. Changes inside the badge also cannot cause the badge's size to be recomputed.
What happens without an explicit size
The guarantee has a cost. If you do not give the element a size, the browser treats it as empty. In the block dimension, a div with contain: size and height: auto gets a height of 0.
The exact result depends on the display type:
| Situation | Result under `contain: size` |
|---|---|
| Block-level box, `width: auto` | Width still fills the container. Height becomes 0. |
| Block-level box with explicit width and height | Both used as written. |
| `inline-block` with `width: auto` | Shrink-to-fit width becomes 0. |
| Flex or grid item with auto size | Its content-based minimum size becomes 0, so it can shrink to nothing. |
The contents are not removed. They still render and can overflow the zero-height box, which often shows up as text drawn on top of the next sibling. The element is only sized as if it were empty.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Size containment demo</title>
<style>
.box {
border: 2px solid #345;
margin-bottom: 1rem;
}
.contained {
contain: size;
}
.sized {
contain: size;
height: 4rem;
}
</style>
</head>
<body>
<div class="box">Normal box: grows to fit its text.</div>
<div class="box contained">This box is collapsed to zero height.</div>
<div class="box sized">This box has an explicit height.</div>
</body>
</html>Open this page and compare the three boxes. The second shows only a thin border, and its text overflows below it.
Placeholder sizes with contain-intrinsic-size
Sometimes you want the element to have a size, but you cannot write it as width and height because the layout around it should stay flexible. For this case there is contain-intrinsic-size. It supplies the size the element uses in place of its contents.
.preview {
contain: size;
contain-intrinsic-size: 300px 200px;
}Here 300px is the width and 200px is the height. A single value sets both. An explicit width or height still takes priority over the intrinsic size. The intrinsic size only fills in where the size would otherwise be auto.
There are also longhands for single dimensions:
contain-intrinsic-widthandcontain-intrinsic-heightfor physical dimensions.contain-intrinsic-inline-sizeandcontain-intrinsic-block-sizefor logical dimensions, which follow the writing mode.
The value can start with the auto keyword, as in auto 600px. This tells the browser to remember the element's last rendered size and use it instead of the fallback 600px. That behavior only matters when the element's contents get skipped, which the content-visibility chapters cover. Without skipped rendering, the element always has its contents available, so auto has little to remember.
Inline-size containment
Full size containment constrains both dimensions. Often you only need one. contain: inline-size applies size containment along the inline axis only. The element's width, in horizontal writing modes, no longer depends on its contents. Its height can still grow to fit them.
.column {
contain: inline-size;
}This is useful because width is usually decided by the parent, while height is naturally driven by the content. A .column with inline-size containment fills the available width, and its text wraps and grows downward.
Container queries rely on this. Declaring container-type: inline-size applies inline-size containment to the element. The width of the container can then be resolved first, and the query can be checked without circular dependency. Declaring container-type: size applies full size containment, which is why size containers need an explicit height. A later chapter covers this in detail.
What size containment does not do
It is easy to expect more from this value than it gives. Size containment alone does not:
- clip the contents. Overflow is still visible unless you set
overflowor add paint containment. - create a containing block for absolute or fixed descendants. Layout containment does that.
- create a stacking context. Layout and paint containment do that.
- skip any rendering work. The contents are still styled, laid out and painted.
Size containment gives the browser one guarantee: this element's size is known without inspecting its subtree. The browser can use that to avoid re-laying out ancestors when the contents change.
Where it fits in the containment family
Size containment is the building block for content-visibility. When the browser skips rendering an element's contents, it must still know how large the element is. That is why content-visibility: auto implies size containment, and why contain-intrinsic-size matters so much for scroll stability.
Common errors
- Collapsed elements. The most common mistake is adding
contain: sizeto an element withheight: auto. Set a height, or usecontain-intrinsic-size. - Overlapping text. Contents that overflow a small contained box draw over what follows. Check the box size in DevTools, not just the contents.
- Flex or grid surprises. A contained flex item can shrink to zero because its content-based minimum size is gone. Give it
flex-basis, a width, or a track size. - Expecting clipping. Add
overflow: hiddenorcontain: paintif contents must stay inside the box.
Exercise
A .thumb element has contain: size and no width or height. It sits in a normal block layout and holds a 200px-tall image. What is its height, what happens to the image, and how can you give it room?
Show answer
Its height is 0, because size containment sizes the box as if it were empty. Its width still fills the container, since it is a block-level box. The image still renders and overflows below the box, overlapping the next sibling. To fix it, set an explicit height, or set contain-intrinsic-size (for example contain-intrinsic-size: auto 200px) so the element uses a placeholder height.
Exercise
You want a sidebar whose width is set by the page grid, but whose height should grow with its content. Which containment value fits, and why not contain: size?
Show answer
Use contain: inline-size. It makes the width independent of the contents while leaving the height content-driven. contain: size would also remove the content's influence on height, so the sidebar would collapse vertically unless you set a height.