Sub topic 8
Which container does a query or unit use?
This chapter covers how the browser picks the container for each @container rule and each container query unit. It assumes you can already write a basic @container size query, declare container-type and container-name, and use cqi in a declaration. You should also be comfortable with the cascade and with inheritance of custom properties.
When a page has one container, the question never comes up. Real pages nest containers: a page layout contains a sidebar, the sidebar contains a card wrapper, and the wrapper contains a card. Each query and each unit then needs a rule that decides which of those containers it talks to.
The core rule: nearest eligible ancestor
For a query, the browser starts at the element being styled and walks up the ancestor chain. It never considers the element itself. It stops at the first ancestor that is eligible for that query. An ancestor is eligible when all of these hold:
- It is a container. For size queries that means a non-
normalcontainer-type. For style queries every element counts. - If the query names a container, the ancestor's
container-namelist includes that name. - Its container type supports the features the query asks about.
If no ancestor is eligible, the query matches nothing, and its rules do not apply. There is no error and no warning.
Nested containers with size queries
Here is a three-level structure.
<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 containers.
.layout {
container: layout / inline-size;
}
.card-wrapper {
container: card / inline-size;
}
.card {
display: block;
}
/* Unnamed: nearest inline-size container above .card */
@container (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}
/* Named: skips .card-wrapper and measures .layout */
@container layout (min-width: 60rem) {
.card {
border: 2px solid steelblue;
}
}The first rule has no name. The nearest container above .card is .card-wrapper, so the 30rem threshold applies to the wrapper's width. The second rule names layout. The browser skips .card-wrapper because it does not carry that name, and measures .layout. A single card can therefore respond to its immediate wrapper and to the page-level layout at the same time.
An unnamed query does not mean "unnamed containers only". An unnamed query accepts any container, named or not. Names only narrow the search. They never widen it.
Why names matter in nested layouts
Without names, inserting a new container between two existing ones silently changes which one an unnamed query measures. Say a designer wraps .card in a new container-type: inline-size element for an unrelated reason. Every unnamed @container rule that used to measure .card-wrapper now measures the new element. Naming the containers your component depends on prevents that.
A name belongs to the element that declares it, and one element can carry several names.
.card-wrapper {
container-type: inline-size;
container-name: card media-slot;
}A query using either card or media-slot can match this element.
Container type filters the search
The third eligibility condition catches many people. A container with container-type: inline-size can answer questions about inline size (width in horizontal writing modes) only. If a query asks about height or block size, that container is skipped, even if it is the nearest one.
.outer {
container: outer / size;
}
.inner {
container: inner / inline-size;
}
/* Skips .inner, because inline-size containment does not allow block-size queries.
Measures .outer, which has size containment. */
@container (min-height: 20rem) {
.content {
columns: 2;
}
}
/* Measures .inner, which is nearest and supports inline-size. */
@container (min-width: 20rem) {
.content {
columns: 2;
}
}If .outer did not exist, the height query would find no eligible ancestor and never match. A common bug is putting min-height in a query when only inline-size containment is declared. The rule just never applies, and you may spend a while inspecting the wrong element.
A summary of what each container type supports:
| `container-type` | Size queries it can answer | Style queries |
|---|---|---|
| `normal` | none | yes |
| `inline-size` | `width`, `inline-size`, `aspect-ratio`, `orientation` (inline axis only) | yes |
| `size` | all size features | yes |
Check the exact feature support for the axes you need in the current spec and in your target browsers. The practical rule is that block-axis queries need size, and size containment means the element's height can no longer come from its content. You must give it a height some other way.
Container query units use the same idea, with a different filter
Container query units (cqw, cqh, cqi, cqb, cqmin, cqmax) resolve against the nearest ancestor container that has the right containment on the relevant axis. Three differences from @container rules are worth remembering.
- Units cannot be named.
cqialways means the nearest eligible ancestor. If you need to size something against a specific distant container, expose its measurement some other way, for example through a style query or a custom property set by script. - Eligibility is per axis.
cqwandcqineed containment on the relevant horizontal or inline axis.cqhandcqbneed it on the vertical or block axis. Aninline-sizecontainer does not provide a block-size basis, socqbskips it. - They resolve for the element using them, so the element's own container does not count. A container using
cqiin its own declarations resolves against its ancestor.
.layout {
container: layout / inline-size;
}
.card-wrapper {
container: card / inline-size;
/* 4cqi here is 4% of .layout's inline size, not .card-wrapper's */
padding: 4cqi;
}
.card {
/* 2cqi here is 2% of .card-wrapper's inline size */
padding: 2cqi;
}.card-wrapper and .card both sit inside .layout, but only .card has .card-wrapper as its nearest eligible ancestor. A wrapper that pads itself with cqi is therefore scaling to the container above it. This is a common source of "why is my padding the wrong size" confusion.
Because the filter is per axis, mixed units in a single rule can point to different containers.
.outer {
container: outer / size;
height: 30rem;
}
.inner {
container: inner / inline-size;
}
.content {
/* cqi: nearest container with inline containment, which is .inner */
padding-inline: 3cqi;
/* cqb: .inner has no block-axis containment, so this skips to .outer */
padding-block: 5cqb;
}Here padding-inline is measured against .inner and padding-block against .outer. If .outer were removed, cqb would have no eligible ancestor.
The fallback when nothing is eligible
If no ancestor qualifies for the axis a unit needs, the unit falls back to the matching small viewport unit. cqw becomes 1svw, cqh becomes 1svh, cqi becomes 1svi, cqb becomes 1svb, and cqmin and cqmax become 1svmin and 1svmax. A component with padding: 2cqi placed outside any container will have padding of 2% of the small viewport's inline size. It will not collapse to zero.
This fallback can mask mistakes. If a component looks correct at some viewport widths and wrong at others, check whether it is actually inside the container you think it is.
Style queries use a looser definition of container
For @container style(...), every element is a potential container, because every element has computed custom property values. Consequences:
- An unnamed style query evaluates against the parent of the styled element. The parent needs no
container-type. - A named style query skips ancestors that lack that name, as with size queries. The named element may have
container-type: normal, which gives it a name without any size containment. - The element being styled is never its own style container.
.alert-region {
--tone: warning;
}
.alert {
border: 2px solid gray;
}
@container style(--tone: warning) {
.alert {
border-color: orange;
background: #fff4e0;
}
}Custom properties inherit by default, so a deeply nested .alert still matches. Its parent's computed --tone is warning, inherited from .alert-region. The query is evaluated on the parent, but the value it reads may have come from further up.
This changes if you register the property with @property and inherits: false. Then only the direct parent's own declaration counts. Intermediate wrappers hide the value from descendants. Use a name to reach past them.
.page {
container-name: tone-region;
--tone: warning;
}
.panel {
--tone: info;
}
/* Skips .panel and reads --tone from .page */
@container tone-region style(--tone: warning) {
.alert {
border-color: orange;
}
}An element can serve as a named style container without any size containment, because container-type defaults to normal. The cost of containment (see the pitfalls chapter) is therefore avoided when you only need a named lookup point for style queries.
Combining size and style conditions
A query condition is evaluated against one container. If you write a query that mixes a size feature and a style feature, the container must satisfy both: it needs size containment for the size part. Do not assume the size part is measured on one ancestor and the style part on another. If you need that, use two nested @container rules, and each will resolve its own container.
@container card (min-width: 30rem) {
@container style(--tone: warning) {
.alert {
padding: 2cqi;
}
}
}The outer rule finds the card container and checks its width. The inner rule runs its own search for the style container. Resolution is independent for each rule.
Debugging checklist
When a query or unit does not behave as expected, work through these questions in order.
- Which element is the styled element? Remember that it never matches itself.
- Walk up from its parent. Which ancestors have a
container-typeor, for style queries, any computed value? - Does the query name a container? If so, which ancestors carry that name?
- Does the nearest candidate's type support the features or axis in question?
- For units, is there an eligible ancestor at all, or is the viewport fallback in use?
Browser dev tools mark container elements in the DOM tree in recent versions. Use them to confirm your mental model against the actual page.
Exercise
In the structure .layout (container: layout / inline-size) > .sidebar > .wrapper (container: card / inline-size) > .card, which element does each of these measure for .card? (a) @container (min-width: 40rem), (b) @container layout (min-width: 40rem), (c) padding: 3cqi on .card, (d) padding: 3cqi on .wrapper.
Show answer
(a) .wrapper, the nearest container above .card. (b) .layout, because .wrapper lacks the name layout. (c) .wrapper's inline size, since it is the nearest eligible ancestor. (d) .layout's inline size, because an element's own container status is ignored and its ancestors are searched.
Exercise
A component sits inside a container declared with container-type: inline-size and nothing else. You write @container (min-height: 20rem) { .box { color: red; } } and .box { padding-block: 4cqb; }. What happens to each, assuming no other containers exist on the page?
Show answer
The @container rule never matches, because an inline-size container cannot answer a height query and no other ancestor is eligible. The 4cqb padding falls back to 4svb, 4% of the small viewport's block size, because no ancestor has block-axis containment.
Exercise
.region sets --tone: warning. .mid is a child of .region and sets --tone: info. .alert is a child of .mid. What does the unnamed query @container style(--tone: warning) do for .alert, and how can you make the alert react to .region's value anyway?
Show answer
The query reads the parent of .alert, which is .mid, whose computed --tone is info, so the rule does not match. To target .region, give it a name, for example container-name: tone-region, and write @container tone-region style(--tone: warning). The named query skips .mid because it lacks that name.