container-name CSS property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since February 2023.

The container-name CSS property specifies a list of query container names used by the @container at-rule in a container query. A container query will apply styles to elements based on the size or scroll-state of the nearest ancestor with a containment context. When a containment context is given a name, it can be specifically targeted using the @container at-rule instead of the nearest ancestor with containment.

Syntax

css
container-name: none;

/* A single name */
container-name: my-layout;

/* Multiple names */
container-name: my-page-layout my-component-library;

/* Global Values */
container-name: inherit;
container-name: initial;
container-name: revert;
container-name: revert-layer;
container-name: unset;

Values

none

Default value. The query container has no name.

<custom-ident>

A case-sensitive string that is used to identify the container. The following conditions apply:

  • The name must not equal or, and, not, or default.
  • The name value must not be in quotes.
  • The dashed ident intended to denote author-defined identifiers (e.g., --container-name) is permitted.
  • A list of multiple names separated by a space is allowed.

Formal definition

Initial valuenone
Applies toall elements
Inheritedno
Computed valuenone or an ordered list of identifiers
Animation typeNot animatable

Formal syntax

container-name = 
none |
<custom-ident>+

Examples

Using a container name

Given the following HTML example which is a card component with a title and some text:

html
<div class="card">
  <div class="post-meta">
    <h2>Card title</h2>
    <p>My post details.</p>
  </div>
  <div class="post-excerpt">
    <p>
      A preview of my <a href="https://example.com">blog post</a> about cats.
    </p>
  </div>
</div>

To create a containment context, add the container-type property to an element in CSS. The following example creates two containment contexts, one for the card meta information and one for the post excerpt:

Note: A shorthand syntax for these declarations are described in the container page.

css
.post-meta {
  container-type: inline-size;
}

.post-excerpt {
  container-type: inline-size;
  container-name: excerpt;
}

Writing a container query via the @container at-rule will apply styles to the elements of the container when the query evaluates to true. The following example has two container queries, one that will apply only to the contents of the .post-excerpt element and one that will apply to both the .post-meta and .post-excerpt contents:

css
@container excerpt (width >= 400px) {
  p {
    visibility: hidden;
  }
}

@container (width >= 400px) {
  p {
    font-size: 2rem;
  }
}

For more information on writing container queries, see the CSS Container Queries page.

Using multiple container names

You can also provide multiple names to a container context separated by a space:

css
.post-meta {
  container-type: inline-size;
  container-name: meta card;
}

This will allow you to target the container using either name in the @container at-rule. This is useful if you want to target the same container with multiple container queries where either condition could be true:

css
@container meta (width <= 500px) {
  p {
    visibility: hidden;
  }
}

@container card (width <= 200px) {
  h2 {
    font-size: 1.5em;
  }
}

Specifications

Specification
CSS Conditional Rules Module Level 5
# container-name

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
container-name
Chrome – Full support
Chrome 105 (Release date: 2022-09-02)
footnote Full support
Edge – Full support
Edge 105 (Release date: 2022-09-01)
footnote Full support
Firefox – Full support
Firefox 110 (Release date: 2023-02-14)
footnote Full support
Opera – Full support
Opera 91 (Release date: 2022-09-14)
footnote Full support
Safari – Full support
Safari 16 (Release date: 2022-09-12)
footnote Full support
Chrome Android – Full support
Chrome Android 105 (Release date: 2022-09-02)
footnote Full support
Firefox for Android – Full support
Firefox for Android 110 (Release date: 2023-02-14)
footnote Full support
Opera Android – Full support
Opera Android 72 (Release date: 2022-10-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16 (Release date: 2022-09-12)
footnote Full support
Samsung Internet – Full support
Samsung Internet 20 (Release date: 2023-02-10)
footnote Full support
WebView Android – Full support
WebView Android 105 (Release date: 2022-09-02)
footnote Full support
WebView on iOS – Full support
WebView on iOS 16 (Release date: 2022-09-12)
footnote Full support
none
Chrome – Full support
Chrome 105 (Release date: 2022-09-02)
footnote Full support
Edge – Full support
Edge 105 (Release date: 2022-09-01)
footnote Full support
Firefox – Full support
Firefox 110 (Release date: 2023-02-14)
footnote Full support
Opera – Full support
Opera 91 (Release date: 2022-09-14)
footnote Full support
Safari – Full support
Safari 16 (Release date: 2022-09-12)
footnote Full support
Chrome Android – Full support
Chrome Android 105 (Release date: 2022-09-02)
footnote Full support
Firefox for Android – Full support
Firefox for Android 110 (Release date: 2023-02-14)
footnote Full support
Opera Android – Full support
Opera Android 72 (Release date: 2022-10-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16 (Release date: 2022-09-12)
footnote Full support
Samsung Internet – Full support
Samsung Internet 20 (Release date: 2023-02-10)
footnote Full support
WebView Android – Full support
WebView Android 105 (Release date: 2022-09-02)
footnote Full support
WebView on iOS – Full support
WebView on iOS 16 (Release date: 2022-09-12)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also