π What is container-name?
container-name is a CSS property used to give a container acustom name so that your @container queries can explicitly target it β instead of targeting any container in the ancestor chain.
Note
π― Why Use container-name?
- To target specific containers in complex layouts
- To avoid triggering queries from unintended parent containers
- To create reusable, independent UI components
- To make container queries clearer and more maintainable
π Syntax
container-name Syntax
container-name: myContainer;A container can have **one or multiple names**:
Multiple names
container-name: layout card responsive;π§± Example 1 β Naming a Container
Define a Named Container
.card {
container-type: inline-size;
container-name: card-layout;
}Now you can target this container specifically:
Query the Named Container
@container card-layout (min-width: 480px) {
.card-title {
font-size: 1.8rem;
}
}The query triggers ONLY when .card grows to 480px wide.
π§± Example 2 β Using Multiple Named Containers
Multiple Names
.dashboard {
container-type: inline-size;
container-name: layout dashboard sidebar-area;
}You can now write queries like:
Using Any Name
@container dashboard (min-width: 900px) {
.sidebar { display: block; }
}
@container layout (min-width: 1200px) {
.main-content { max-width: 1100px; }
}Each name creates a separate βlisten pointβ for responsive behavior.
π Visual Model

With naming, components react EXACTLY to the container you specify.
π§ͺ Example 3 β Combining container-type + container-name
Named Container with Type
.profile-card {
container-type: inline-size;
container-name: profile;
}
@container profile (min-width: 400px) {
.avatar { width: 150px; }
.details { font-size: 1.2rem; }
}Even if this card is nested inside other containers,only this specific container affects the component.
π± Real-World Example β Reusable Components
Suppose you have multiple cards inside different grid layouts. Each card should react to its own space, not the page.
Reusable Card Component
.card {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 550px) {
.card-body {
display: flex;
gap: 20px;
}
}This makes the card responsive in ANY layout where itβs placed.
π§ How container-name Works Internally
| Concept | Behavior |
|---|---|
| Without container-name | Query matches the nearest container-type ancestor |
| With container-name | Query matches ONLY the named container(s) |
| Multiple names | You can match using any one of the declared names |
Note
β οΈ Common Mistakes
- Using container-name without container-type β does nothing
- Misspelling the container name in @container queries
- Naming too many containers unnecessarily
- Assuming container-name affects layout (it doesnβt)
π₯ Summary
container-name is a powerful addition to container queries, enabling developers to precisely target containers in any layout. When combined with container-type, it unlocks fully modular, reusable, component-driven responsive design.
Learn more βMDN Docs π