Sub topic 8
What containment does to your layout
This chapter assumes you can write a basic @container rule and know container-type. You should also know how flexbox and grid size items, and what a containing block is. Here you will see which side effects container-type has on layout, because they cause most "my layout broke" reports.
Containment is the price of querying
A container query only works if the browser can measure the container without looking at its children. Otherwise a child's style could change the container's size, which would change the query result, which would change the child's style. This loop would never settle.
To prevent the loop, container-type applies CSS containment to the element. That tells the browser which parts of the element are isolated from the rest of the page.
| `container-type` | Containment applied |
|---|---|
| `normal` | None (style queries only) |
| `inline-size` | Layout, style, and inline-size containment |
| `size` | Layout, style, and size containment |
Size containment: the width or height stops depending on content
Inline-size containment means the container's inline size (its width, in horizontal writing modes) is computed as if it had no content. Its height still follows its content.
This matters when the container's width was previously "whatever the content needs." That happens with floats, inline-blocks, absolutely positioned boxes, and flex or grid items that size to content. Such a container can collapse to zero width.
.row {
display: flex;
}
.card-wrapper {
container-type: inline-size;
/* Without a width source, this flex item collapses. */
}In this example, .card-wrapper has no content-based width, so it shrinks to nothing. Give it a width from somewhere else:
.row {
display: flex;
}
.card-wrapper {
container-type: inline-size;
flex: 1 1 20rem;
}With container-type: size, both dimensions ignore content. An auto-height container collapses to zero height, so you must set an explicit height or aspect-ratio or place it in a sized parent.
Layout containment: a new, isolated box
Layout containment makes the container behave as a self-contained box. These are the effects you will notice:
- The container becomes an independent formatting context. Its margins do not collapse with those of its children, and it contains its own floats.
- It becomes the containing block for absolutely and fixed-positioned descendants. A
position: fixedchild now positions relative to the container, not the viewport. - It creates a stacking context, so
z-indexvalues inside it cannot interleave with elements outside.
.card-wrapper {
container-type: inline-size;
}
.card-wrapper .badge {
position: absolute;
inset: 0 0 auto auto; /* top-right of .card-wrapper */
}If you wrap an existing component whose tooltip used position: fixed, it will now be placed and clipped relative to the wrapper. Move the container to a lower-level element, or restructure the tooltip.
Style containment
Style containment scopes counters and quotes to the container. A counter-increment inside the container does not affect counters outside it. Most pages never notice this, but numbered headings that span a wrapper can restart unexpectedly.
The container cannot style itself
A rule inside @container can only style descendants of the container, never the container itself. If you apply container-type to .card, a rule for .card inside @container will not match the container that .card creates. That is why the earlier examples use a wrapper and a child.
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card-wrapper {
padding: 2rem; /* Never applies: the container is not its own descendant. */
}
.card {
padding: 2rem; /* Applies. */
}
}Quick checks when layout breaks
- Does the container have a width from its parent, a
flexvalue, or agridtrack? If not, inline-size containment collapsed it. - With
container-type: size, does it have a height? - Did a fixed or absolute descendant move? The container is now its containing block.
- Are you styling the container itself inside
@container? Move that rule to a descendant.
Exercise
A developer adds container-type: inline-size to an inline-block .thumb with no width. The thumbnail disappears. Why, and what is one fix?
Show answer
Inline-size containment makes the element's width ignore its content, so an auto-width inline-block collapses to zero width. Give it a width source, such as width: 12rem, or make it a block-level or flex/grid item whose width comes from its parent.