Sub topic 6
Naming containers
This chapter assumes you can already declare a query container with container-type and write a basic @container rule. It also assumes you know what a CSS custom identifier is: an unquoted name such as sidebar or card-list.
An unnamed @container rule is simple, but it is also blunt. Once components nest inside each other, you often need to say which ancestor a rule should measure. Names let you do that.
Why names exist
An unnamed rule such as @container (min-width: 400px) matches the nearest ancestor that is a query container able to answer the question. This works well when each component sits inside one wrapper. It gets ambiguous in a real page, where containers nest:
- A page shell is a container.
- A sidebar inside it is a container.
- A widget inside the sidebar wants to react to the page shell, not the narrow sidebar.
Without names, the widget always measures the sidebar, because the sidebar is nearer. A name lets the rule skip the sidebar and measure the page shell.
The container-name property
container-name gives a query container one or more names. It does nothing on its own, because an element without a container-type is not a size container. Set both.
.sidebar {
container-type: inline-size;
container-name: sidebar;
}
@container sidebar (width > 30rem) {
.widget {
flex-direction: row;
}
}A few facts about the property:
- The value is
none(the default) or a space-separated list of custom identifiers. - Names are case-sensitive.
Sidebarandsidebarare different names. - Names are not required to be unique. Many elements can share a name, and the rule picks the nearest matching ancestor.
- The names
none,and,or, andnotare not valid, because they would be confused with query syntax. - The property is not inherited. A child of a named container is not itself named.
The container shorthand
Most code uses the container shorthand to set the name and type in one declaration. The name comes first, then a slash, then the type.
.sidebar {
container: sidebar / inline-size;
}This is exactly equal to setting container-name: sidebar and container-type: inline-size.
| Declaration | Resulting name | Resulting type |
|---|---|---|
| `container: sidebar / inline-size;` | `sidebar` | `inline-size` |
| `container: sidebar / size;` | `sidebar` | `size` |
| `container: sidebar;` | `sidebar` | `normal` |
| `container: a b / inline-size;` | `a` and `b` | `inline-size` |
| `container: none;` | none | `normal` |
The third row is a common source of confusion. When you leave out the / type part, the shorthand resets container-type to normal. A container of type normal is not a size container, so size queries such as width > 30rem will not match it. If your named query never fires, check that the shorthand includes a type.
The shorthand also overrides earlier longhand declarations on the same element. Look at this code:
.sidebar {
container-type: inline-size;
container: sidebar;
}The second declaration wins and resets the type to normal. The sidebar stops being a size container. Avoid mixing the shorthand with longhands on the same rule, or put the shorthand first.
Targeting a named container
The container name goes between @container and the condition. The condition itself is unchanged.
@container sidebar (min-width: 20rem) {
/* rules */
}
@container sidebar (400px <= width <= 700px) {
/* rules */
}The rule applies only when the nearest ancestor named sidebar satisfies the condition. Unnamed ancestors are ignored, and so are ancestors with other names.
Here is a full example with nested containers. The markup has a page shell, a sidebar, and a widget in the sidebar:
<div class="page">
<main>Main content</main>
<aside class="sidebar">
<div class="widget">Widget</div>
</aside>
</div>The CSS declares both ancestors as containers with different names, then writes a rule against each one:
.page {
container: page / inline-size;
}
.sidebar {
container: sidebar / inline-size;
}
.widget {
padding: 0.5rem;
}
@container page (width > 60rem) {
.widget {
padding: 1.5rem;
}
}
@container sidebar (width > 20rem) {
.widget {
border: 1px solid currentColor;
}
}The widget now has two independent responsive behaviors. Its padding depends on the page shell width, and its border depends on the sidebar width. Without names, both rules would measure the sidebar.
The diagram below shows how each rule resolves.
Containers never query themselves. A rule can style only the descendants of a container, so a rule inside @container sidebar cannot change .sidebar itself. If you need to restyle the container, put a wrapper element between the container and the content.
One element, several names
An element can carry more than one name. Each name is a separate handle that rules can use.
.main-column {
container: main layout / inline-size;
}
@container main (width > 40rem) {
.teaser {
display: grid;
}
}
@container layout (width > 40rem) {
.nav {
display: flex;
}
}Both rules measure the same element. Multiple names are useful when a component library and an application each want their own vocabulary. The library may call a container card-slot, while the application calls the same element feature-row. Neither side needs to know the other's name.
You can also reuse one name on many elements. Suppose every card wrapper is called card. Then @container card (width > 400px) works inside every wrapper, and each card measures its own wrapper. This is the usual pattern for reusable components, and it is why a name is not an ID. The name labels a role, not one particular element.
How resolution works with names
The browser decides which container a rule uses by walking up from each styled element. It picks the nearest ancestor that passes both of these checks:
- If the rule has a name, the ancestor has that name in its
container-namelist. - The ancestor's
container-typecan answer the conditions in the query.
The second check matters for size conditions. An inline-size container can answer questions about width (the inline axis) but not height. Consider this CSS:
.outer {
container: outer / size;
}
.inner {
container: inner / inline-size;
}
@container (height > 300px) {
.item {
min-height: 300px;
}
}The unnamed rule asks about height. The nearest container is .inner, but it is an inline-size container and cannot answer height questions. The browser continues upward and matches .outer, which is a size container. Adding a name narrows the search further. @container inner (height > 300px) would match nothing, because the only inner container cannot answer height queries.
When no ancestor passes both checks, the query does not match and its rules do not apply. The browser does not report an error. The rule is silently skipped, which makes name typos hard to spot.
Naming conventions and trade-offs
Names are global in the sense that any rule in the stylesheet can use any name, so a few habits keep them manageable:
- Name by role, not by location.
card-slotstays accurate when the card moves.homepage-leftdoes not. - Prefer a name only when you need one. If every component has exactly one wrapper and no nesting, unnamed rules are shorter and need no coordination.
- Use a shared prefix for library code, such as
ui-card, to avoid collisions with application names. - Avoid reusing one name at different nesting levels for different meanings. It works, since the nearest ancestor wins, but readers will have to reconstruct the DOM to understand a rule.
A named query is also a stronger dependency than an unnamed one. If you rename the container in one file but not in the rule, the rule breaks silently. Keep the name next to the declaration it depends on, or define the name once in a comment near the component styles.
Quick checklist
When a named container query does not apply, check these items in order:
- The container has a size-capable type (
inline-sizeorsize), notnormal. - The shorthand includes
/ type, and no later declaration resets it. - The name in the rule exactly matches the name in the container, including case.
- The styled element is a descendant of the container, not the container itself.
- The queried feature is supported by the container's type (no height queries on
inline-size).
Exercise
A developer writes .panel { container: panel; } and then @container panel (width > 30rem) { .panel .title { font-size: 2rem; } }. The title never changes size. Give two reasons the rule fails and fix the CSS.
Show answer
First, the shorthand has no / type, so container-type is reset to normal and .panel is not a size container, so the width condition cannot match. Second, even with a type, the rule would only work for descendants, and .title is a descendant, so that part is fine; the type is the real cause. The fix is .panel { container: panel / inline-size; }. Make sure no later declaration on .panel resets container-type.
Exercise
A page has .page { container: page / inline-size; } containing .sidebar { container: sidebar / inline-size; }, which contains .widget. Which element does @container (width > 50rem) { .widget { ... } } measure, and how do you make the widget measure the page instead?
Show answer
The unnamed rule measures .sidebar, because it is the nearest ancestor that is a size-capable container. To measure the page, name the container in the rule: @container page (width > 50rem) { .widget { ... } }. The browser skips .sidebar because it does not have the name page and continues up to .page.