Sub topic 16
How container queries build on containment
This chapter assumes you know the contain values (layout, paint, size, style) from earlier chapters. It also assumes you can write a basic media query. Here you will see how container queries use containment to work, and why the odd restrictions on query containers follow from that.
The problem container queries solve
A media query asks about the viewport. A component such as a card does not care how wide the viewport is. It cares how much space its parent gives it. The same card might sit in a wide main column, a narrow sidebar, or a modal.
A container query lets a component ask about the size of an ancestor. This creates a possible circularity. If the card's styles depend on the container's width, and the container's width depends on the card's contents, the browser could loop forever: the query changes the contents, the contents change the width, and the width changes the query result.
Containment removes that loop. The browser needs a guarantee that an element's size does not depend on its descendants, at least along the axis being queried. That is the guarantee size containment gives.
container-type applies containment for you
Declaring an element as a query container turns on containment automatically.
.sidebar {
container-type: inline-size;
}
@container (min-width: 400px) {
.sidebar .item {
display: flex;
}
}The container-type value decides which containment types are applied:
| Value | Containment applied | What you can query |
|---|---|---|
| `normal` | None for size queries | Style queries only |
| `inline-size` | Layout, style, and inline-size containment | Width (the inline axis) |
| `size` | Layout, style, and size containment | Width and height |
Inline-size containment is a narrower form of size containment. It makes the element's inline size independent of its contents, and leaves the block size free to grow with them. This suits normal documents, where width comes from the parent and height comes from the content. container-type: size also fixes the height, so you must set an explicit height or the container collapses to zero in that dimension.
What the guarantees buy the browser
Each applied containment type has a job in making queries safe.
- Inline-size containment lets the browser resolve the container's width without laying out its children. It can then evaluate the query and lay out the children afterward, in a single pass.
- Layout containment means nothing inside the container affects layout outside it, and the reverse. The query result only changes the container's subtree, so the browser knows how far the effects of a query can reach.
- Style containment scopes counters and similar style effects to the subtree, so a query-driven change cannot leak out.
This is why container-type is not a plain "give me a size" flag. It is a containment declaration with a query attached.
A working example
This card switches from a stacked layout to a side-by-side layout when its container is wide enough. It works the same whether the container is a sidebar or a main column.
<div class="slot">
<article class="card">
<img src="photo.jpg" alt="A mountain lake" width="160" height="100">
<div class="text">
<h2>Mountain lake</h2>
<p>A short description of the place.</p>
</div>
</article>
</div>
<style>
.slot {
container: card-slot / inline-size;
max-width: 700px;
}
.card {
display: block;
}
@container card-slot (min-width: 450px) {
.card {
display: flex;
gap: 1rem;
}
}
</style>The container shorthand sets container-name and container-type together. The name is optional, but it helps when containers are nested, because an unnamed query matches the nearest ancestor that is a container of a suitable type.
Restrictions that come from containment
Most container query surprises are containment side effects. They are the same effects described for contain earlier, now applied to your query container.
A container cannot style itself
A query applies to descendants of the container, never to the container. Otherwise a rule could change the width that the rule was tested against. If you need to restyle the element, add a wrapper that is the container and query from there. In the example above, .slot is the container and .card is what the query changes.
Width must not come from the contents
With inline-size containment, the container behaves as if it had no content when computing its inline size. In a context where width is normally derived from content, it can collapse. Common cases are floats, inline-blocks, absolutely positioned boxes with width: auto, and flex items with flex: 0 1 auto in a row layout.
.row {
display: flex;
}
/* Collapses: the width would come from the content, which containment ignores. */
.bad {
container-type: inline-size;
}
/* Works: the width comes from the flex layout. */
.good {
container-type: inline-size;
flex: 1;
}Layout containment side effects
Layout containment makes the container a containing block for absolutely and fixed positioned descendants. It also creates a stacking context and an independent formatting context. In practice:
- A
position: absolutetooltip inside the container is positioned relative to the container, not to a farther ancestor. - A
position: fixeddescendant is also positioned relative to the container, so it no longer stays pinned to the viewport. - Margins of the first and last children do not collapse through the container's edges.
z-indexvalues inside the container cannot compete with elements outside it.
Block size is only free with inline-size
With container-type: inline-size, height still comes from the content, so you cannot write @container (min-height: 300px). The height condition has no usable meaning there. Use container-type: size only when the height is set by something else, such as a fixed-height panel or a grid track.
Container query units
Query containers also enable length units relative to the container: cqw and cqh (1% of the container's width or height), cqi and cqb (inline and block size), and cqmin and cqmax. Without a suitable container, they fall back to the small viewport units.
.card h2 {
font-size: clamp(1rem, 4cqi, 2rem);
}Choosing a type
- Use
inline-sizefor most components. It matches how pages flow and it avoids the zero-height collapse. - Use
sizeonly when you truly need height queries and the height is set from outside. - Leave
container-typeoff elements that do not need to be queried. Each query container adds a layout boundary, with the side effects listed above. - Give containers names once you have more than one level of nesting, so each query hits the container you intend.
The browser has an extra benefit here. Because layout containment limits the effects of a query, a change in container size only needs the subtree re-laid out. The performance reasoning from the earlier chapters applies to query containers too.
Exercise
A developer adds container-type: inline-size to a flex item (display: flex on the parent, item left at its default flex value). The item shrinks to nearly zero width, and its @container (min-width: 400px) rule never applies. Why does this happen, and name one fix?
Show answer
Inline-size containment makes the item's width independent of its contents, so the content-based width the flex item would normally get is gone and it collapses. Give the width a source other than the content, for example flex: 1, an explicit width, or flex-basis. The query then has a real width to test.
Exercise
A tooltip inside a card uses position: fixed and used to stay anchored to the viewport. After you set container-type: inline-size on the card, it moves with the card. Which containment type causes this?
Show answer
Layout containment, which container-type: inline-size applies. It makes the container the containing block for fixed and absolutely positioned descendants, so the tooltip is positioned relative to the card.