Sub topic 4
Naming containers and the container shorthand
This chapter assumes you can write a basic @container size query and that you know container-type: inline-size makes an element a size container. It builds on that by adding names, so a query can target a specific container.
Why names matter
An unnamed @container rule applies to the nearest ancestor that is a container. That works while you have one container. When a layout nests containers, such as a card inside a sidebar, you often want a rule to follow the sidebar and not the card's own wrapper. A name lets you say which one.
Setting a name with container-name
container-name takes a custom identifier. You then put the same identifier after @container.
.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 now a named inline-size container. The rule applies when the wrapper is at least 30rem wide, whatever the viewport width is. The markup it styles:
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>Names are case-sensitive, so Card and card are different. A name cannot be none, and, not or or, because those words have meaning in the query syntax.
An element can carry several names, separated by spaces. A query for any one of them will find it.
.layout-main {
container-type: inline-size;
container-name: main page-body;
}The container shorthand
The container shorthand sets the name and the type in one declaration. The name comes first, then a slash, then the type.
.card-wrapper {
container: card / inline-size;
}This is the same as the two longhand declarations above. For several names, separate them with spaces before the slash: container: main page-body / inline-size.
The shorthand resets what you leave out
A shorthand always sets every property it covers. If you write only a name, container-type is reset to its initial value, normal.
.card-wrapper {
container-type: inline-size;
container: card; /* container-type is now normal */
}After this, .card-wrapper has a name but is no longer a size container. A size query such as @container card (min-width: 30rem) will not match it. The same happens if the shorthand comes later in the cascade than a longhand you meant to keep.
Choosing between names and no names
Named queries and unnamed queries resolve differently.
- An unnamed
@container (min-width: 30rem)uses the nearest ancestor container that can answer that query. - A named
@container card (min-width: 30rem)uses the nearest ancestor that has the namecard. Containers without that name are skipped.
Consider a card inside a sidebar. Both are containers.
.sidebar {
container: sidebar / inline-size;
}
.card-wrapper {
container: card / inline-size;
}
@container sidebar (max-width: 20rem) {
.card {
font-size: 0.875rem;
}
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}The sidebar rule reads the sidebar's width, even though the card wrapper is closer to .card. The card rule reads the wrapper. Without names, both rules would use the wrapper, because it is the nearest container.
Naming conventions
Name containers after the component, such as card, sidebar or toolbar. Do not name them after the breakpoint or layout they produce. The name stays true when the layout changes.
Give a name to any container that another container may one day wrap. Adding names later is safe, but unnamed rules that used to find one container may then find a different one.
Exercise
A stylesheet has .panel { container-type: inline-size; container: panel; } and a rule @container panel (min-width: 40rem) { .panel .item { columns: 2; } }. The rule never applies, even when .panel is 60rem wide. Why, and what is the fix?
Show answer
The container shorthand comes after container-type and resets it to normal, because the shorthand omits the type. .panel has the name but is not a size container, so the size query cannot match. Fix it by writing container: panel / inline-size; and dropping the separate container-type line.