Sub topic 16
How container queries build on containment
This chapter assumes you know the contain property, size and layout containment, and basic @container syntax. It shows why declaring a query container needs containment, and what that costs you.
The circularity problem
A media query reads the viewport, which no element's styles can change. A container query reads the size of an ancestor. That creates a possible loop:
- A child's styles depend on the container's width.
- The container's width depends on its children.
- The children's styles change the container's width, so the query result changes again.
The browser cannot resolve this safely. Containment breaks the loop by making the container's size independent of what is inside it.
What `container-type` applies
Declaring a container applies containment automatically.
| Declaration | Containment applied | What the query can read |
|---|---|---|
| `container-type: inline-size` | Inline-size, layout, and style | Width (in horizontal writing modes) |
| `container-type: size` | Size, layout, and style | Width and height |
| `container-type: normal` | None for size | Only style queries |
Inline-size containment is a variant of size containment that applies to one axis only. The container's width no longer depends on its contents. Its height still does, so it grows with its children as usual.
.sidebar {
container-type: inline-size;
}
@container (min-width: 400px) {
.sidebar .item {
display: flex;
}
}The query rule styles only descendants of .sidebar. An element cannot query itself, because its own size is what the containment protects.
Side effects you inherit
The containment is real, so the side effects from earlier chapters apply:
- Layout containment makes the container a containing block for absolute and fixed descendants. It also creates a stacking context.
- Style containment scopes counters and quotes to the container.
- Inline-size containment means the container cannot take its width from its content. A container that is
floated orinline-blockwith no width will collapse to zero width.
.card-wrapper {
container-type: inline-size;
width: 100%;
}Block-level containers fill their parent's width, so they work without extra rules. Shrink-to-fit containers need an explicit width.
Choosing `size` or `inline-size`
Prefer inline-size. With container-type: size, the height is also fixed, so you must set an explicit height or the container collapses vertically. Use size only when you need height queries and can supply that height.
.panel {
container-type: size;
width: 300px;
height: 200px;
}
@container (min-height: 150px) {
.panel .body {
display: block;
}
}The relationship in one view
Exercise
A <span class="tag"> has container-type: inline-size and no width set, and its text is "New". It renders as an invisible zero-width box. Why does this happen, and what are two ways to fix it?
Show answer
Inline-size containment stops the element's width from being computed from its contents, and an inline or shrink-to-fit box has no other source for a width, so it becomes zero. Fix it by giving it an explicit width, or by making it a block-level box (for example display: block or placing it in a block wrapper that is the container) so it fills the available width.