Sub topic 8
Which container does a query or unit use?
This chapter assumes you can already declare a container with container-type and write a basic @container rule. It also assumes you know the cqi unit. The question here is what happens when a page has more than one container, and how the browser picks the one a rule or unit refers to.
The nearest-ancestor rule
A query container is always an ancestor of the element being styled. For each @container rule, the browser starts at the parent of the styled element and walks up the tree. It stops at the first ancestor that qualifies for the query. Rules that differ only in which ancestor they match can therefore apply to the same element.
Two details follow from this:
- An element is never its own query container. A
.cardthat is also a container cannot be restyled by its own@containerrule. Only its descendants can. - Distant containers are skipped when they don't qualify. A closer ancestor with
container-type: normalhas no size containment, so a size query passes over it.
Unnamed queries use the closest qualifying ancestor
Here is nested markup with two containers:
<div class="layout">
<aside class="sidebar">
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text.</p>
</article>
</div>
</aside>
</div>Both .layout and .card-wrapper are size containers:
.layout {
container: layout / inline-size;
}
.card-wrapper {
container: card / inline-size;
}
.card {
display: block;
}
@container (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}The unnamed rule matches .card-wrapper, because it is the closest ancestor with size containment. The width of .layout is irrelevant to it, even if .layout is wider than 30rem.
Named queries skip to the matching name
A name in the query narrows the search to ancestors that carry that name:
@container layout (min-width: 60rem) {
.card {
box-shadow: 0 2px 8px rgb(0 0 0 / 0.2);
}
}The browser passes over .card-wrapper because it is named card, and it measures .layout. If no ancestor has the name, the query matches nothing. The rule does not fall back to some other container. A misspelled name is a common reason a rule "does nothing".
Container units resolve the same way
cqi, cqw and the other container units use the nearest ancestor container, with one extra condition: the container must contain the axis the unit measures. Each unit is resolved on its own.
| Unit | Measures | Needs containment on |
|---|---|---|
| `cqw` | width | the horizontal axis |
| `cqh` | height | the vertical axis |
| `cqi` | inline size | the inline axis |
| `cqb` | block size | the block axis |
| `cqmin`, `cqmax` | the smaller or larger of `cqi` and `cqb` | both axes |
In a horizontal writing mode, a card inside a container-type: inline-size wrapper has a usable cqi. Its cqb ignores that wrapper because the block axis is not contained. The browser keeps searching upward for an ancestor that does contain it.
Container units cannot take a name. If you need the width of a particular outer container, you cannot ask for it by name. Instead, set a custom property on that container and inherit the value:
.layout {
container: layout / inline-size;
--layout-pad: 2cqi;
}
.card {
padding: var(--layout-pad);
}This works because --layout-pad is an unregistered custom property. The cqi token is substituted textually, so the value is not resolved on .layout. When .card uses var(--layout-pad), 2cqi resolves against .card's own nearest container, which is .card-wrapper. To pin a computed length, register the property with @property and syntax: "<length>". Then the length is resolved on .layout and inherited as pixels.
Style queries use the same walk
Every element is a style container by default, so @container style(--tone: warning) matches the closest ancestor, whatever its container-type. The query is true if that ancestor's computed --tone equals warning. Since custom properties inherit, the closest ancestor usually has the value too, so a parent without --tone set still matches. To restrict the search, add a name: @container card style(--tone: warning) only considers ancestors named card.
Debugging checklist
- Check that the container is an ancestor and not the element being styled.
- Compare spelling between
container-nameand the query. - Confirm the container has the right
container-typefor the feature, or for the unit's axis. - Look for a closer container that is capturing an unnamed query.
Exercise
In the nested example, .card-wrapper is 20rem wide and .layout is 70rem wide. Does .card become a grid under @container (min-width: 30rem)? What changes if the rule is written @container layout (min-width: 30rem)?
Show answer
No. The unnamed query resolves to the nearest qualifying ancestor, .card-wrapper, which is 20rem wide, so the condition is false. With the name layout, the browser skips .card-wrapper and measures .layout at 70rem, so the condition is true and .card becomes a grid.