Sub topic 7
Which Container Does a Query Match?
This chapter assumes you know how to declare a container with container-type, write a basic @container rule, and name a container with container-name. Here you will learn how the browser picks the container for each rule, and why that choice sometimes surprises you.
The resolution rule
A container query never looks at siblings, descendants, or the viewport. For each element the rule styles, the browser walks up the ancestor chain, starting at the element's parent, and checks each ancestor in turn. It stops at the first ancestor that qualifies.
An ancestor qualifies when both conditions hold:
- It is a query container, meaning it has a
container-typeother thannormal. - If the rule names a container, the ancestor's
container-namelist includes that name.
The element itself is never its own container. A rule styles the element, and the container it queries is always an ancestor.
Unnamed queries use the nearest container
An unnamed @container rule matches the nearest ancestor container, whatever its name. Consider nested containers:
.page {
container-type: inline-size;
}
.sidebar {
container-type: inline-size;
}
@container (min-width: 600px) {
.widget {
flex-direction: row;
}
}If .widget sits inside .sidebar, which sits inside .page, the rule measures .sidebar. A wide .page does not help if the sidebar is narrow. This is the most common source of "my query never fires" reports. The query is working, but it is measuring a closer container than you intended.
Named queries skip non-matching ancestors
Naming lets a rule jump past nearer containers:
.page {
container: page / inline-size;
}
.sidebar {
container: sidebar / inline-size;
}
@container page (min-width: 600px) {
.widget {
flex-direction: row;
}
}Now .widget is styled according to the width of .page, even though .sidebar is closer. The browser checks .sidebar, sees that its name does not match, and continues upward.
An element can also carry several names, as in container-name: card panel. A rule naming either card or panel matches it.
Type must also support the condition
Finding the container is only the first step. The browser then evaluates the condition against it, and the container's type limits what it can answer.
| Container type | Width condition | Height condition |
|---|---|---|
| `inline-size` | Works in horizontal writing modes | Does not match |
| `size` | Works | Works |
| `normal` | Not a size container | Not a size container |
If the nearest qualifying container lacks the dimension you query, the rule does not match. It does not keep searching further up. Likewise, a named rule that finds a name match with the wrong type fails instead of moving on to a better candidate.
Common mistakes
- Querying the element you marked. Setting
container-typeon.cardand then styling.cardinside@containerhas no effect. Put the container on a wrapper and style its descendants. - A closer container captures the rule. Add names to the containers and name the query.
- Querying height on an
inline-sizecontainer. Switch tocontainer-type: size, and give the container an explicit height, sincesizecontainment stops it from taking its height from its content. - A name typo. A mismatched name means no ancestor qualifies, so the rule silently does nothing.
Exercise
A .widget is inside .sidebar (container: sidebar / inline-size), which is inside .page (container: page / inline-size). The sidebar is 300px wide and the page is 900px wide. Does @container (min-width: 600px) { .widget { color: red; } } apply? What if the rule is @container page (min-width: 600px)?
Show answer
The unnamed rule does not apply, because it matches the nearest container, .sidebar, which is only 300px wide. The rule named page skips the sidebar because the name does not match, measures .page at 900px, and applies.