Sub topic 6
Naming Containers
This chapter assumes you know core CSS (selectors, the cascade, the box model), basic flexbox or grid, and that you have already declared a container with container-type and written a basic @container rule. Here you will learn how to give containers names so a rule targets the ancestor you intend.
Why names help
An unnamed @container rule matches the nearest ancestor that is a query container. That works for simple pages. In real layouts you often nest containers: a page wrapper, a sidebar inside it, and a card inside the sidebar. A rule meant for the sidebar can end up matching the card's wrapper instead. A name lets the rule say which container it means.
container-name
The container-name property gives a container one or more names. You set it on the container, not on the element being styled.
.sidebar {
container-type: inline-size;
container-name: sidebar;
}
@container sidebar (width > 30rem) {
.widget {
flex-direction: row;
}
}The name goes between @container and the condition. The rule now only applies when an ancestor named sidebar is wider than 30rem. Other containers are ignored, even if they are closer to .widget.
A name is a custom identifier, like a keyframes name. It is case-sensitive. Avoid the words none, and, or, and not, which the syntax reserves.
The container shorthand
The container shorthand sets container-name and container-type in one declaration. Write the name first, then a slash, then the type.
.sidebar {
container: sidebar / inline-size;
}This is the same as the two longhand declarations above. If you write only a name with no slash, the type resets to normal, so the element is not a size container. That is a common way to break a working query.
/* Wrong: no slash, so container-type becomes normal */
.sidebar {
container: sidebar;
}Several names on one container
A container can carry more than one name, separated by spaces. A query can use any one of them.
.panel {
container: panel layout-item / inline-size;
}
@container layout-item (min-width: 400px) {
.panel .title {
font-size: 1.25rem;
}
}Use this when one element plays several roles, for example a general role (layout-item) and a specific one (panel).
Named and unnamed queries together
An unnamed @container rule still works when containers have names. It matches the nearest ancestor with a suitable container type, named or not. A named rule skips ancestors that lack that name.
In this layout, .card can respond to two different sizes at once.
main.page {
container: page / inline-size;
}
aside.sidebar {
container: sidebar / inline-size;
}
/* Matches the sidebar, the nearest container */
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}
/* Matches the page, skipping the sidebar */
@container page (min-width: 900px) {
.card {
box-shadow: 0 2px 8px rgb(0 0 0 / 0.2);
}
}Common naming errors
- A typo in the name. If no ancestor has that name, the rule never applies, and the browser reports no error. Check spelling and case.
- A name on the wrong element. The name must be on an ancestor of the styled element, not on the element itself. A container cannot query itself.
- A named container with no type. A name alone does not create a size container. You also need
container-type, or the shorthand with a slash. - Querying the wrong axis. If you need
height, the container needscontainer-type: size, notinline-size.
Exercise
A stylesheet has .layout { container: layout; } and @container layout (min-width: 600px) { .item { gap: 2rem; } }. The rule never applies, even when .layout is 900px wide. What is wrong, and how do you fix it?
Show answer
The shorthand has a name but no slash and type, so container-type resets to normal and .layout is not a size container. Write .layout { container: layout / inline-size; }.