CSS Reference ยท Container Queries and Logical Properties
The container-name property
Reference for the CSS container-name property: give a query container one or more names so @container rules can target the right ancestor when containers nest.
The container-name property gives an element a name, or several, that @container rules can use to say which container they want to query. It does not make the element a container by itself: you also need a container-type, or the container shorthand.
Names matter as soon as containers are nested. Without a name, a query uses the nearest ancestor container that can answer it. With a name, a rule can skip the closest container and ask a particular one further up.
.layout { container-name: page; container-type: inline-size; } .sidebar { container-name: side; container-type: inline-size; } @container page (min-width: 60rem) { .sidebar { display: block; } }
Try it
Both badges sit inside the narrow inner container, but only the one that names outer is shown, because only that container reaches 300px.
Syntax
container-name: none | <custom-ident>+
- Initial value:
none - Applies to: all elements
- Inherited: no
- Animatable: no
- Computed value:
none, or the list of identifiers as specified
Values
| Value | Description |
|---|---|
none |
The element has no container name. It can still be a container, and unnamed queries can still match it. |
<custom-ident> |
A name of your choice, written without quotes. Separate several names with spaces. A name cannot be none, and, not, or, or one of the CSS-wide keywords such as inherit or initial. |
Usage notes
- Multiple names.
container-name: card list;lets the same element answer to either name. Use it when one container plays two roles. - A name is not a type. A named element with
container-type: normalstill cannot answer size queries. In practice you set both, usually with thecontainershorthand. - Case matters. Custom identifiers are case-sensitive, so
Cardandcardare different names. - Names are looked up through ancestors. A rule
@container sidebar (...)styles elements that have an ancestor container namedsidebar. If there is none, the rule matches nothing. - Pick meaningful names. Name the role (
page,sidebar,card) rather than the width or the color, so the CSS stays correct when the design changes. - Not a selector. The name is only used by
@container. You cannot select elements with it, and it does not create anidorclass.
Browser support
Supported in all current major browsers together with container queries; check a compatibility table for older versions.
Related
containerโ shorthand for name and typecontainer-typeโ makes the element a container@containerโ the rule that uses the name- Lesson: Container Queries