Sub topic 7
Which container does a query match?
This chapter assumes you can already declare a query container with container-type, write a basic @container rule, and name a container with container-name or the container shorthand. It covers how the browser picks the one container that a given @container rule evaluates against. Most "my container query does nothing" bugs come from a wrong guess here.
The rule starts at the styled element
A container query is not evaluated once for the whole page. It is evaluated separately for each element that the rules inside it select.
For every element matched by a selector inside @container, the browser walks up that element's ancestors and looks for a query container. It uses the first one that qualifies. It then tests the size condition against that container. The comparison is never made against the viewport or the document.
Three consequences follow:
- The search starts at the element's parent. An element is never its own query container, so a container cannot restyle itself with its own query.
- The search runs from the element the rule styles, the last compound selector in the rule. In
.sidebar .widget { ... }, the container is looked up from.widget. Ancestors named earlier in the selector have no special role. - Two elements matched by the same rule can resolve to different containers. This is what makes a component reusable.
Unnamed queries: nearest eligible ancestor
An @container rule without a name has no name to filter on. It picks the nearest ancestor that is eligible for the features the query uses.
Eligibility depends on the query:
- Size features such as
widthandinline-sizeneed an ancestor whosecontainer-typeisinline-sizeorsize. - The
heightandblock-sizefeatures needcontainer-type: size. Aninline-sizecontainer does not qualify for them. - An ancestor with
container-type: normalis never a size container, even if it has acontainer-name. The search skips it.
The search takes the nearest eligible ancestor and stops there. It does not keep going until it finds a container where the condition is true. If the nearest eligible container is 300px wide and a more distant one is 900px wide, then (min-width: 500px) is false. The outer container is never consulted.
If no ancestor is eligible, the condition is unknown and the rule does not apply. The browser reports no error.
Named queries: nearest ancestor with that name
When a rule gives a name, as in @container sidebar (width > 30rem), the browser looks for the nearest ancestor that satisfies both of these:
- Its
container-namelist includessidebar. - It is eligible for the features in the condition.
Containers in between that have other names, or no name, are skipped. Name matching is case-sensitive.
An element can have several names. container-name: card layout makes it a match for @container card (...) and for @container layout (...). A container can therefore serve as a general-purpose "nearest" container and also be addressed by name from a deeper level.
A worked example with nested containers
This markup nests a widget in a sidebar, and the sidebar in a page. Both ancestors are containers.
<main class="page">
<aside class="sidebar">
<div class="widget">Widget</div>
</aside>
</main>.page {
container: page / inline-size;
}
.sidebar {
container: sidebar / inline-size;
width: 18rem;
}
.widget {
padding: 1rem;
background: lightgray;
}
/* Unnamed: matches the nearest eligible ancestor, .sidebar (18rem). */
@container (min-width: 40rem) {
.widget {
background: gold;
}
}
/* Named: skips .sidebar and matches .page. */
@container page (min-width: 40rem) {
.widget {
border: 2px solid black;
}
}
/* Height needs container-type: size. Neither ancestor has it. */
@container (height > 10rem) {
.widget {
background: tomato;
}
}On a 70rem-wide page, the result is:
- The widget gets a black border. The
pagequery resolves to.page, which is 70rem wide, so the condition is true. - The widget stays light gray. The unnamed query resolves to
.sidebar, which is 18rem wide. The 70rem page is irrelevant to it. - The
heightrule never applies. Both ancestors areinline-sizecontainers, so neither is eligible and the condition is unknown.
The resolution flow
The diagram shows what the browser does for each element that a rule selects.
Summary of cases
| Situation | Container used |
|---|---|
| Unnamed width query, parent is `inline-size` | The parent |
| Unnamed width query, parent has only a name (`container-type: normal`) | The parent is skipped. The next eligible ancestor is used. |
| Unnamed height query, nearest container is `inline-size` | The nearest `size` container. If none exists, the condition is unknown. |
| Named query, nearest container has a different name | That container is skipped. The nearest container with the matching name is used. |
| Named query, no ancestor has the name | None. The condition is unknown. |
| Query inside the container's own rules | Never the element itself. The search starts at its parent. |
Using names to pick a level
Unnamed queries are the right default for a component that responds to the space it is placed in. If you want a rule to respond to a specific layer, you need a name. Typical cases are a card that should respond to the page layout rather than to a small wrapper nested inside it, or a layout rule that depends on a particular region.
Two practices keep this predictable:
- Give a container a name when other containers may be placed between it and the elements that query it. Unnamed queries then cannot accidentally bind to a new in-between wrapper.
- Use specific names such as
product-gridinstead of generic ones likewrapper. Duplicated names are legal, and the nearest matching one wins. A name reused in a nested subtree can silently redirect a query.
Style queries use a different search
@container style(--theme: dark) has no size features, so it has no size-eligibility test. Every element can answer a style query about its computed values, even with container-type: normal. An unnamed style query therefore resolves to the parent element. With a name, it resolves to the nearest ancestor with that name. Size and style conditions can be combined in one query, and in that case the container must be eligible for both.
Quick checks when a query seems to ignore your container
- Look at the nearest ancestor with a
container-type. If it is not the one you intended, that ancestor is the one being measured. - Check that the container has
inline-sizeorsize, not just a name. A name alone gives no size containment. - If the query tests height, confirm that the container uses
container-type: size. - If the query is named, compare the name with the container's
container-nameexactly, including case. - Confirm the rule styles a descendant. A rule meant to style the container itself will never match.
Exercise
A page has .layout { container: layout / inline-size; } (900px wide) containing .panel { container-type: inline-size; width: 300px; }, which contains .title. The rule @container (min-width: 500px) { .title { color: red; } } is written. Does .title turn red, and why? What change would make .title respond to the 900px container?
Show answer
It does not turn red. The query is unnamed, so it resolves to the nearest eligible ancestor, .panel, which is 300px wide. The condition is false, and the search does not continue to .layout. Naming the target, as in @container layout (min-width: 500px), makes the query skip .panel and match .layout, so .title turns red.
Exercise
You write @container (height > 400px) { .card { padding: 2rem; } }, and the wrapper above .card has container-type: inline-size with a height of 600px. The padding never changes. Give the reason and the fix.
Show answer
An inline-size container is not eligible for height queries, so the wrapper is skipped. No other eligible ancestor exists, so the condition is unknown and the rule does not apply. Change the wrapper to container-type: size (and give it a definite height, since size containment ignores its children when computing size) so that the height query has an eligible container.