Sub topic 4
Naming Containers and the container Shorthand
What this chapter assumes
You should already know how to declare a container with container-type: inline-size and write a basic @container (min-width: ...) rule. You should also be comfortable with the cascade, grid basics, and how a shorthand property resets its longhand properties. This chapter covers the part that decides which container a query talks to: names.
Why containers need names
An unnamed @container rule asks, "Starting from the element I style, which ancestor is the nearest container that can answer this query?" On a small page that is usually the wrapper you meant. As soon as containers nest, the nearest one is often the wrong one.
Suppose a card sits inside a layout region, and both are containers. You want the card's heading to get larger when the whole page area is wide. An unnamed query written inside the card's rules would measure the card's wrapper, not the page area. A name lets you aim the query at a specific container, however many others sit between it and the styled element.
Declaring a name with container-name
container-name takes one or more names. A name is a custom identifier, like a class name without the dot:
.card-wrapper {
container-type: inline-size;
container-name: card;
}
.card {
display: block;
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}The wrapper is a named inline-size container. The rule applies only when the nearest ancestor named card is at least 30rem wide. The viewport is irrelevant.
The markup this applies to:
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>The name goes on the container, not on the element you style. The .card rule sits inside the @container block, and .card-wrapper is the element being measured.
Rules for names
- Names are case-sensitive.
Cardandcardare different names. - Do not quote them.
container-name: "card"is invalid. - A name cannot be
none,and,or, ornot, because those words have meaning in the container syntax. CSS-wide keywords such asinheritandinitialare also not allowed as names. container-name: noneis the initial value and means the element has no name.- Names are not inherited. A container's name applies only to that element, not to its descendants.
Pick names that describe the container's role (page, sidebar, card, toolbar), not its current appearance (wide-box). A role name stays accurate when the design changes.
One container, several names
container-name accepts a space-separated list, so one element can answer to several names:
.main-column {
container-type: inline-size;
container-name: main content;
}A rule can then target @container main (...) or @container content (...), and both resolve to this element. This is useful when a container plays two roles, or when you are renaming a container and want old and new names to work during the migration.
A query itself holds at most one name. You cannot write @container main content (min-width: 40rem). The container is chosen by one name, and the conditions follow it.
The container shorthand
The container shorthand sets container-name and container-type in one declaration. The name comes first, then a slash, then the type:
.card-wrapper {
container: card / inline-size;
}
.card {
padding: 2cqi;
}
.card h2 {
font-size: clamp(1.125rem, 4cqi, 2rem);
}This is equivalent to the two longhand declarations in the earlier example. The cqi unit in the second and third rules resolves against this container's inline size, which the next chapter on container units covers in detail.
The grammar is:
container: <container-name> [ / <container-type> ]You can put multiple names before the slash:
.main-column {
container: main content / inline-size;
}Spaces around the slash are optional (card/inline-size is valid), but the spaced form is easier to read.
Equivalent forms
| Shorthand | Longhands | Result |
|---|---|---|
| `container: card / inline-size` | `container-name: card; container-type: inline-size` | Named inline-size container |
| `container: card / size` | `container-name: card; container-type: size` | Named container measured in both axes |
| `container: main content / inline-size` | `container-name: main content; container-type: inline-size` | Two names, one container |
| `container: card` | `container-name: card; container-type: normal` | Named, but not a size container |
| `container: none` | `container-name: none; container-type: normal` | Resets both to their initial values |
The shorthand resets the type
The shorthand always sets both longhands. If you leave out the slash and the type, container-type is reset to its initial value, normal. An element with container-type: normal has no size containment, so size queries cannot target it.
.card-wrapper {
container-type: inline-size;
}
/* Later in the stylesheet, or in another file: */
.card-wrapper {
container: card;
}The second rule sets the name, but it also silently resets container-type to normal. The @container card (min-width: 30rem) rule now never applies, and the browser reports no error.
The same thing happens with the usual shorthand and longhand ordering rules in the cascade. A longhand written after the shorthand wins for that one property. The shorthand written after a longhand overwrites it. When a container stops working, check every rule that matches the element and look for a container shorthand that omits the type.
Which container does a query pick?
For each styled element, the browser walks up the ancestor chain, nearest first, and takes the first ancestor that qualifies. What qualifies depends on the query:
- An unnamed query qualifies any ancestor container that can answer the query's feature. A size query such as
min-widthneeds an ancestor withinline-sizeorsizecontainment. - A named query qualifies only ancestors whose
container-namelist includes that name, and that can answer the query.
The element itself is never considered. A container cannot query itself, which is why the .card-wrapper and .card pair exists.
If the walk reaches the root without finding a match, the @container block simply does not apply. The element keeps its default styles, which is why those defaults should work as a single-column or compact layout.
A nested example
Here three containers are nested. A page shell, a layout region, and a card wrapper each have a role name:
<div class="page-shell">
<div class="layout">
<main class="content">
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text.</p>
</article>
</div>
</main>
</div>
</div>.page-shell {
container: page / inline-size;
}
.content {
container: content / inline-size;
}
.card-wrapper {
container: card / inline-size;
}
.layout {
display: block;
}
.card {
display: block;
}
@container page (min-width: 60rem) {
.layout {
display: grid;
grid-template-columns: 1fr 18rem;
gap: 2rem;
}
.card h2 {
font-size: 2rem;
}
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}Trace the three rules:
.layoutis inside.page-shell, so@container pagefinds it directly..card h2is also styled from thepagerule. The browser skips.card-wrapperand.content, because neither is namedpage, and measures.page-shell. The heading grows with the page width even though two nearer containers exist..cardis styled from@container card. It measures.card-wrapper, the only ancestor with that name.
If you wrote an unnamed @container (min-width: 30rem) around the .card h2 rule, the nearest qualifying ancestor would be .card-wrapper. The name page is what sends the query further up.
Names that match nothing
A query with a name that no ancestor has does not match. The rule never applies, and nothing in DevTools warns you in most browsers. Typical causes:
- A typo, such as
@container cardsagainstcontainer-name: card. - A case mismatch,
Cardversuscard. - The name is on a sibling or on a descendant of the styled element rather than an ancestor.
- The named element has
container-type: normal, so it cannot answer a size query.
When a rule seems dead, work through these in order. Temporarily changing the condition to something always true, like (min-width: 0), helps separate "no container found" from "container found but condition false".
Names and style queries
Names also work for style queries, which a later chapter covers. An element with container-type: normal can still be named, and a style query can target it by name:
.alert-region {
container-name: region;
--tone: warning;
}
@container region style(--tone: warning) {
.alert {
border-color: orange;
}
}This is the main legitimate use of a named element with no size type: it gives you a name to aim at without adding size containment.
Summary of decisions
- Use an unnamed query when the nearest suitable ancestor is the intended one, and the component will always sit directly inside its own wrapper.
- Use a name when containers nest, when a component should respond to a distant ancestor, or when other authors may add containers around your component.
- Use the
containershorthand for brevity, always with both name and type. - Use the longhands when the name and the type are set in different places, for example when a base stylesheet sets the type and a theme sets the name.
Exercise
A stylesheet has .panel { container-type: inline-size; } early on and .panel { container: sidebar; } later. A rule @container sidebar (min-width: 20rem) { .panel-body { padding: 2rem; } } never applies, even when .panel is 40rem wide. Why, and what is the fix?
Show answer
The container shorthand sets both longhands. Because it has no / type part, it resets container-type to normal, overriding the earlier inline-size. A normal container cannot answer a size query. Fix it by writing container: sidebar / inline-size.
Exercise
Given .a { container: outer / inline-size; } containing .b { container: inner / inline-size; } containing .c, which element does @container outer (min-width: 40rem) { .c { color: red; } } measure, and which element does the same rule measure if you remove the name and write @container (min-width: 40rem)?
Show answer
With the name, the rule measures .a, because the browser skips .b (named inner). Without the name, it measures .b, the nearest ancestor that is a suitable inline-size container.
Exercise
Write one declaration that makes .toolbar an inline-size container reachable by both @container actions (...) and @container header-tools (...).
Show answer
.toolbar { container: actions header-tools / inline-size; }. The names are separated by spaces before the slash, and each query uses a single name.