CSS container-name Property

The container-name property assigns one or more names to an element so descendant elements can target that ancestor in CSS container queries.

Container names help identify specific query containers in complex layouts. They are particularly useful when a component has multiple container ancestors or needs to respond to a particular layout region.

The property does not enable size queries by itself. To query a container's dimensions, use container-type or the container shorthand to establish an appropriate size query container.

container-name Syntax

The property accepts none or one or more space-separated custom identifiers.

selector {
  container-name: card;
}

General Syntax

selector {
  container-name: none | <custom-ident>+;
}

Single Container Name

.card-wrapper {
  container-name: card;
}

Multiple Container Names

.card-wrapper {
  container-name: card layout;
}

No Container Name

.card-wrapper {
  container-name: none;
}

CSS-Wide Keywords

.card-wrapper {
  container-name: inherit;
}
.card-wrapper {
  container-name: initial;
}
.card-wrapper {
  container-name: unset;
}
.card-wrapper {
  container-name: revert;
}
.card-wrapper {
  container-name: revert-layer;
}

Container names are CSS identifiers, not quoted strings. Multiple names are separated by spaces rather than commas.

Property Values

Value Description
none Assigns no container names. This is the initial value.
card Assigns the container name card.
sidebar Assigns the container name sidebar.
main-content Assigns the container name main-content.
card layout Assigns two names to the same container.
panel dashboard widget Assigns three names to the same container.

Property Characteristics

Characteristic Value
Initial value none
Inherited No
Applies to All elements
Computed value Specified list of identifiers or none
Animation type Discrete
Percentages Not applicable
Shorthand container

The property identifies containers but does not determine which dimensions are available for size queries.

How It Works

Container names allow descendant styles to reference a specific ancestor container.

Step 1: Create a Component

<div class="card-wrapper">
  <div class="card">
    <h3>CSS Reference</h3>
    <p>Explore CSS properties and examples.</p>
  </div>
</div>

Step 2: Assign a Container Name

.card-wrapper {
  container-name: card;
}

The wrapper is now identified by the name card.

Step 3: Enable Inline-Size Queries

.card-wrapper {
  container-name: card;
  container-type: inline-size;
}

The container now supports size queries along its logical inline dimension.

Step 4: Define Default Styles

.card {
  padding: 15px;
  background-color: #f1f5f9;
}

Step 5: Query the Named Container

@container card (min-width: 400px) {
  .card {
    padding: 25px;
  }
}

When the selected card container is at least 400 pixels wide, the descendant card receives the larger padding.

Important Distinction

The name does not establish size containment. Both the name and an appropriate query container type are required for a named size query.

Container Naming Rules

Container names follow CSS custom identifier rules.

Valid Names

.component {
  container-name: card;
}
.component {
  container-name: sidebar;
}
.component {
  container-name: main-content;
}
.component {
  container-name: _panel;
}
.component {
  container-name: card2;
}

Names Are Case-Sensitive

.component {
  container-name: Card;
}
@container Card (min-width: 400px) {
  .card-content {
    padding: 25px;
  }
}

The names Card and card are different identifiers.

Reserved Names

The following identifiers cannot be used as custom container names:

  • none
  • initial
  • inherit
  • unset
  • revert
  • revert-layer
  • default

Invalid Quoted Names

.component {
  container-name: "card";
}

Quoted strings are not valid container names.

Invalid Comma-Separated Names

.component {
  container-name: card, layout;
}

Use spaces to separate multiple container names.

Recommended Naming Style

Use short, descriptive identifiers with consistent capitalization, such as sidebar, article, or dashboard-card.

Multiple Container Names

A single element can have more than one container name.

Assign Two Names

.card-wrapper {
  container-name: card layout;
  container-type: inline-size;
}

Query the First Name

@container card (min-width: 400px) {
  .card-content {
    padding: 25px;
  }
}

Query the Second Name

@container layout (min-width: 600px) {
  .card-content {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Three Container Names

.widget-wrapper {
  container-name: widget dashboard panel;
  container-type: inline-size;
}

Changing the Name List

.widget-wrapper {
  container-name: widget dashboard;
  container-name: panel;
}

The second declaration replaces the first name list. The resulting container name is only panel.

Practical Consideration

Multiple names are useful when one container participates in several groups of named queries without requiring additional wrapper elements.

The none Value

The none value removes all container names from an element.

.component-wrapper {
  container-name: none;
}

Initial Value

The initial value of container-name is none.

Removing an Existing Name

.component-wrapper {
  container-name: card;
}
.component-wrapper.unnamed {
  container-name: none;
}

Retaining the Container Type

.component-wrapper {
  container-name: none;
  container-type: inline-size;
}

The element remains eligible for unnamed inline-size queries even though it has no container name.

Difference from container: none

.component-wrapper {
  container: none;
}

The shorthand resets both container-name and container-type, while the container-name longhand changes only the name list.

Working with container-type

The container-type property determines which size dimensions can be queried.

Named Container Without Size Queries

.card-wrapper {
  container-name: card;
  container-type: normal;
}

This creates a named container, but it does not enable size queries.

Named Inline-Size Container

.card-wrapper {
  container-name: card;
  container-type: inline-size;
}

This enables queries along the logical inline dimension.

Named Size Container

.panel-wrapper {
  container-name: panel;
  container-type: size;
}

This enables queries along both logical inline and block dimensions.

Comparison

Container Type Inline Size Queries Block Size Queries
normal No No
inline-size Yes No
size Yes Yes

Practical Consideration

Use container-type: inline-size for most components that need to respond to available inline space. Use size when queries must evaluate both dimensions.

Working with the container Shorthand

The container shorthand combines container-name and container-type into one declaration.

Longhand Properties

.card-wrapper {
  container-name: card;
  container-type: inline-size;
}

Equivalent Shorthand

.card-wrapper {
  container: card / inline-size;
}

Multiple Names

.card-wrapper {
  container: card layout / inline-size;
}

Name Only

.card-wrapper {
  container: card;
}

This sets container-name: card and resets container-type to normal.

Overriding the Name

.card-wrapper {
  container: card / inline-size;
  container-name: panel;
}

The resulting container name is panel, while the type remains inline-size.

Overriding the Type

.card-wrapper {
  container: card / inline-size;
  container-type: size;
}

The name remains card, while the type becomes size.

Practical Consideration

Use the shorthand when assigning both properties together. Use the longhands when changing one property without resetting the other.

Named Container Queries

Named container queries select an ancestor with the specified name that supports the requested query features.

Define the Container

.dashboard-wrapper {
  container-name: dashboard;
  container-type: inline-size;
}

Query by Name

@container dashboard (min-width: 600px) {
  .dashboard-card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Minimum Width

@container dashboard (min-width: 500px) {
  .dashboard-card {
    padding: 25px;
  }
}

Maximum Width

@container dashboard (max-width: 350px) {
  .dashboard-card {
    padding: 10px;
  }
}

Range Syntax

@container dashboard (400px <= width <= 700px) {
  .dashboard-card {
    gap: 20px;
  }
}

Combined Conditions

@container dashboard (min-width: 400px) and (max-width: 700px) {
  .dashboard-card {
    grid-template-columns: 1fr 2fr;
  }
}

Important Distinction

The named container must support the query's requested size features. A matching name alone does not make a size query valid for that container.

Named vs. Unnamed Queries

Named and unnamed container queries differ in how they select an ancestor.

Named Query

.card-wrapper {
  container-name: card;
  container-type: inline-size;
}
@container card (min-width: 400px) {
  .card {
    padding: 25px;
  }
}

The query selects an appropriate ancestor named card.

Unnamed Query

.card-wrapper {
  container-type: inline-size;
}
@container (min-width: 400px) {
  .card {
    padding: 25px;
  }
}

The query selects the nearest eligible ancestor supporting the requested size feature.

Comparison

Feature Named Query Unnamed Query
Requires a name Yes No
Ancestor selection Nearest eligible ancestor with the matching name Nearest eligible ancestor supporting the query
Typical use Complex or nested components Simple components
Size query support Requires an appropriate container type Requires an appropriate container type

Practical Consideration

Use named queries when a component must respond to a specific ancestor. Use unnamed queries when the nearest eligible container is the intended target.

Nested Containers

Nested containers allow components to respond to different ancestor dimensions.

HTML Structure

<div class="page-layout">
  <div class="card-wrapper">
    <div class="card">
      <p>CSS Reference</p>
    </div>
  </div>
</div>

Assign Container Names

.page-layout {
  container-name: page;
  container-type: inline-size;
}
.card-wrapper {
  container-name: card;
  container-type: inline-size;
}

Query the Outer Container

@container page (min-width: 800px) {
  .card {
    font-size: 1.125rem;
  }
}

Query the Inner Container

@container card (min-width: 350px) {
  .card {
    padding: 25px;
  }
}

Unnamed Query

@container (min-width: 350px) {
  .card {
    border-width: 2px;
  }
}

For the card in this example, the unnamed query selects the nearest eligible size query container, .card-wrapper.

Practical Consideration

Named containers make it easier to distinguish between page-level and component-level query conditions.

Container Selection

Container selection determines which ancestor is evaluated by an @container rule.

Nearest Matching Named Ancestor

.outer-wrapper {
  container: card / inline-size;
}
.inner-wrapper {
  container: card / inline-size;
}
@container card (min-width: 400px) {
  .card-content {
    padding: 25px;
  }
}

For a descendant inside both wrappers, the query selects the nearest eligible ancestor named card.

Different Names

.outer-wrapper {
  container: page / inline-size;
}
.inner-wrapper {
  container: card / inline-size;
}

A query for page can select the outer wrapper, while a query for card can select the inner wrapper.

No Matching Name

@container missing (min-width: 400px) {
  .card-content {
    padding: 25px;
  }
}

If no eligible ancestor has the name missing, the query does not match.

Matching Name Without the Required Type

.outer-wrapper {
  container: card / inline-size;
}
.inner-wrapper {
  container: card / normal;
}
@container card (min-width: 400px) {
  .card-content {
    padding: 25px;
  }
}

For a descendant inside both wrappers, the size query can select the outer wrapper because the inner wrapper does not support width queries.

Important Distinction

Container selection considers both the requested name and the query features supported by an ancestor. It does not simply select the nearest element with a matching name.

Container Names and Style Queries

Container names can also identify ancestors for supported container style queries.

Named Style Query Container

.theme-wrapper {
  container-name: theme;
  --theme-color: #336699;
}

Query a Custom Property

@container theme style(--theme-color: #336699) {
  .theme-content {
    border-color: #336699;
  }
}

No Size Container Required

Custom property style queries do not require container-type: inline-size or container-type: size. A normally typed ancestor can be eligible for supported style queries.

Named Style Query with Shorthand

.theme-wrapper {
  container: theme / normal;
  --theme-color: #336699;
}

Browser Support Consideration

Support for style queries and the properties they can test may vary. Custom property style queries are more broadly implemented than some newer style query capabilities.

Use feature-specific compatibility checks before relying on advanced style queries in production.

Responsive Components

Named containers help reusable components adapt to specific layout regions.

Sidebar Cards

.sidebar {
  container-name: sidebar;
  container-type: inline-size;
}
@container sidebar (min-width: 300px) {
  .sidebar-card {
    padding: 25px;
  }
}

Main Content Cards

.main-content {
  container-name: content;
  container-type: inline-size;
}
@container content (min-width: 600px) {
  .content-card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Navigation Components

.navigation-wrapper {
  container-name: navigation;
  container-type: inline-size;
}
@container navigation (min-width: 500px) {
  .navigation-links {
    flex-direction: row;
  }
}

Dashboard Widgets

.widget-wrapper {
  container-name: widget;
  container-type: inline-size;
}
@container widget (min-width: 350px) {
  .widget-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

Practical Consideration

Named queries are especially useful when components are reused in sidebars, dashboards, and content areas with different available widths.

Practical Uses

The container-name property helps organize responsive component styles.

Documentation Layouts

.documentation-wrapper {
  container: documentation / inline-size;
}
@container documentation (min-width: 600px) {
  .documentation-content {
    grid-template-columns: 1fr 2fr;
  }
}

Search Forms

.search-wrapper {
  container: search / inline-size;
}
@container search (min-width: 450px) {
  .search-form {
    flex-direction: row;
  }
}

Reusable Panels

.panel-wrapper {
  container: panel / inline-size;
}
@container panel (min-width: 500px) {
  .panel-content {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
  }
}

Component Libraries

.component-region {
  container-name: component;
  container-type: inline-size;
}
@container component (min-width: 400px) {
  .component-content {
    padding: 25px;
  }
}

When Not to Use

A name is unnecessary when unnamed queries already select the intended ancestor. Avoid adding names that do not improve clarity or control over container selection.

container-name Example

This example creates two named containers. Each contains a card, but the cards respond to different named container queries.

The sidebar card uses the sidebar container, while the content card uses the content container.

<div class="container-name-demo">
  <div class="demo-group">
    <h3>Sidebar Container</h3>
    <div class="sidebar-wrapper">
      <div class="demo-card sidebar-card">
        <h4>CSS Sidebar</h4>
        <p>A compact reference card.</p>
      </div>
    </div>
  </div>
  <div class="demo-group">
    <h3>Content Container</h3>
    <div class="content-wrapper">
      <div class="demo-card content-card">
        <h4>CSS Reference</h4>
        <p>A wider reference card.</p>
      </div>
    </div>
  </div>
</div>
.container-name-demo {
  display: grid;
  gap: 20px;
}
.container-name-demo .demo-group {
  padding: 15px;
  border: 1px solid #cccccc;
  background-color: #f8f9fa;
}
.container-name-demo h3 {
  margin: 0 0 12px;
  font-size: 1.125rem;
}
.container-name-demo .sidebar-wrapper {
  container-name: sidebar;
  container-type: inline-size;
  width: 280px;
  max-width: 100%;
}
.container-name-demo .content-wrapper {
  container-name: content;
  container-type: inline-size;
  width: 550px;
  max-width: 100%;
}
.container-name-demo .demo-card {
  padding: 15px;
  border: 2px solid #336699;
  background-color: #ffffff;
}
.container-name-demo .demo-card h4 {
  margin: 0 0 8px;
  font-size: 1.125rem;
}
.container-name-demo .demo-card p {
  margin: 0;
}
@container sidebar (min-width: 250px) {
  .container-name-demo .sidebar-card {
    background-color: #e8f2ff;
    border-color: #336699;
  }
}
@container content (min-width: 400px) {
  .container-name-demo .content-card {
    background-color: #e8f7ed;
    border-color: #2f855a;
    padding: 25px;
  }
}
Browser Result

Sidebar Container

Content Container

CSS Reference

A wider reference card.

The sidebar container is 280 pixels wide, satisfying the named query for sidebar. Its card receives a light blue background.

The content container is 550 pixels wide, satisfying the named query for content. Its card receives a light green background and additional padding.

Both cards use the same basic structure, but their styles are controlled by different named containers.

Accessibility

The container-name property does not directly affect accessibility, but the responsive layouts controlled by named container queries should remain usable.

Preserve Semantic HTML

Use appropriate headings, landmarks, lists, and form controls. Container names do not replace semantic HTML structure.

Maintain Reading Order

Ensure layout changes preserve a logical reading and keyboard navigation order.

Support Text Resizing

Test named container queries with enlarged text and browser zoom to ensure components remain readable.

Avoid Horizontal Overflow

Use flexible sizing and appropriate breakpoints so components remain usable in narrow containers.

Keep Interactive Controls Available

Do not hide essential navigation or form controls solely because a named container becomes smaller.

Practical Consideration

Test responsive components in their actual layout regions rather than relying only on viewport resizing.

Important Considerations

Keep the following points in mind when using container-name:

  • Purpose: Assigns names to elements for use in container queries.
  • Initial value: none.
  • Inheritance: The property is not inherited.
  • Applies to: All elements.
  • Identifiers: Names use CSS custom identifiers.
  • Case sensitivity: Container names are case-sensitive.
  • Multiple names: Use spaces to separate names.
  • Quoted names: Strings are not valid container names.
  • Reserved identifiers: CSS-wide keywords, none, and default cannot be used as names.
  • none: Removes all assigned container names.
  • Size queries: Require an appropriate container-type.
  • Named queries: Select an eligible ancestor with a matching name.
  • Unnamed queries: Do not require container names.
  • Nested containers: Different names allow queries to target different ancestors.
  • Multiple matching ancestors: The nearest eligible matching ancestor is selected.
  • Style queries: Supported style queries can use named containers without size containment.
  • Shorthand: The container property sets both container-name and container-type.
  • Responsive design: Named containers support reusable components in different layout regions.
  • Accessibility: Responsive layouts should preserve content usability and logical navigation.

Use descriptive container names when components need to respond to specific ancestor containers. For simple components, unnamed queries may provide the same functionality with less CSS.

Browser Support

The CSS container-name property and named size container queries are widely supported in modern browsers, including Chrome, Edge, Firefox, and Safari.

Support for advanced container query features, such as some style queries and scroll-state queries, may vary between browser versions.

Basic Example

.card-wrapper {
  container-name: card;
  container-type: inline-size;
}
@container card (min-width: 400px) {
  .card {
    padding: 25px;
  }
}

Feature Detection

@supports (container-name: card) and (container-type: inline-size) {
  .card-wrapper {
    container-name: card;
    container-type: inline-size;
  }
}

Feature queries can detect support for these declarations, but they do not guarantee support for every newer container query feature.

For current compatibility information, visit Can I Use? .

Specifications

The container-name property is defined in CSS Containment Module Level 3.

CSS Containment Module Level 3

The specification defines container names, naming restrictions, query container selection, and the relationship between container names and container queries.