Skip to main content

@container

The @container CSS at-rule is a conditional group rule that applies styles to a containment context. Style declarations are filtered by a condition and applied to the container if the condition is true. The condition is evaluated when the container size or <style-feature> value changes.

The {{cssxref("container-name")}} property specifies a list of query container names. These names can be used by @container rules to filter which query containers are targeted. The optional, case-sensitive <container-name> filters the query containers that are targeted by the query.

Once an eligible query container has been selected for an element, each container feature in the <container-condition> is evaluated against that query container.

Syntax

The @container at-rule has the following syntax:

@container <container-condition># {
<stylesheet>
}

For example:

@container (width > 400px) {
h2 {
font-size: 1.5em;
}
}

/* with an optional <container-name> */
@container tall (height > 30rem) {
h2 {
line-height: 1.6;
}
}

/* multiple queries in a single condition */
@container (width > 400px) and style(--responsive: true) {
h2 {
font-size: 1.5em;
}
}

/* condition list */
@container card (width > 400px), style(--responsive: true) {
h2 {
font-size: 1.5em;
}
}

Values

  • <container-condition>

    • : An optional <container-name> and a <container-query>. Styles defined in the <stylesheet> are applied if the condition is true.

      • <container-name>
        • : Optional. The name of the container that the styles will be applied to when the query evaluates to true, specified as an {{cssxref("ident")}}.
      • <container-query>
        • : A set of features that are evaluated against the query container when the size of the container changes.
  • <stylesheet>

    • : A set of CSS declarations.

Logical keywords in container queries

Logical keywords can be used to define the container condition:

  • and combines two or more conditions.
  • or combines two or more conditions.
  • not negates the condition. Only one 'not' condition is allowed per container query and cannot be used with the and or or keywords.
@container (width > 400px) and (height > 400px) {
/* <stylesheet> */
}

@container (width > 400px) or (height > 400px) {
/* <stylesheet> */
}

@container not (width < 400px) {
/* <stylesheet> */
}

Named containment contexts

A containment context can be named using the {{cssxref("container-name")}} property.

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

The shorthand syntax for this is to use {{cssxref("container")}} in the form container: <name> / <type>, for example:

.post {
container: sidebar / inline-size;
}

In container queries, the {{cssxref("container-name")}} property is used to filter the set of containers to those with a matching query container name:

@container sidebar (width > 400px) {
/* <stylesheet> */
}

Details about usage and naming restrictions are described in the {{cssxref("container-name")}} page.

Descriptors

The following descriptors can be used within the container condition:

  • aspect-ratio

    • : The {{cssxref("aspect-ratio")}} of the container calculated as the width to the height of the container expressed as a {{cssxref("ratio")}} value.
  • block-size

    • : The {{cssxref("block-size")}} of the container expressed as a {{cssxref("length")}} value.
  • height

    • : The height of the container expressed as a {{cssxref("length")}} value.
  • inline-size

    • : The {{cssxref("inline-size")}} of the container expressed as a {{cssxref("length")}} value.
  • orientation

    • : The orientation of the container, either landscape or portrait.
  • width

    • : The width of the container expressed as a {{cssxref("length")}} value.

Examples

Setting styles based on a container's size

Consider the following example of a card component with a title and some text:

<div class="post">
<div class="card">
<h2>Card title</h2>
<p>Card content</p>
</div>
</div>

A container context can be created using the container-type property, in this case using the inline-size value on the .post class. You can then use the @container at-rule to apply styles to the element with the .card class in a container that's narrower than 650px.

const post = document.querySelector(".post");
const span = document.createElement("span");
span.textContent = ".post width: " + post.clientWidth + "px";
post.parentNode.insertBefore(span, post.nextSibling);
// update on resize
window.addEventListener("resize", () => {
span.textContent = ".post width: " + post.clientWidth + "px";
});
span {
display: block;
text-align: center;
}
.card {
margin: 10px;
border: 2px dotted;
font-size: 1.5em;
}
.post {
border: 2px solid;
}
/* A container context based on inline size */
.post {
container-type: inline-size;
}

/* Apply styles if the container is narrower than 650px */
@container (width < 650px) {
.card {
width: 50%;
background-color: gray;
font-size: 1em;
}
}

{{EmbedLiveSample("Setting_styles_based_on_a_container's_size", "100%", 230)}}

Creating named container contexts

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

<div class="post">
<div class="card">
<h2>Card title</h2>
<p>Card content</p>
</div>
</div>

First, create a container context using the container-type and container-name properties. The shorthand syntax for this declaration is described in the {{cssxref("container")}} page.

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

Next, target that container by adding the name to the container query:

@container summary (min-width: 400px) {
.card {
font-size: 1.5em;
}
}

Nested container queries

It's not possible to target multiple containers in a single container query. It is possible to nest container queries which has the same effect.

The following query evaluates to true and applies the declared style if the container named summary is wider than 400px and has an ancestor container wider than 800px:

@container summary (min-width: 400px) {
@container (min-width: 800px) {
/* <stylesheet> */
}
}

Container style queries

Container queries can also evaluate the computed style of the container element. A container style query is a @container query that uses one or more style() functional notations. The boolean syntax and logic combining style features into a style query is the same as for CSS feature queries.

@container style(<style-feature>),
not style(<style-feature>),
style(<style-feature>) and style(<style-feature>),
style(<style-feature>) or style(<style-feature>) {
/* <stylesheet> */
}

The parameter of each style() is a single <style-feature>. A <style-feature> is a valid CSS declaration, a CSS property, or a <custom-property-name>.

@container style(--themeBackground),
not style(background-color: red),
style(color: green) and style(background-color: transparent),
style(--themeColor: blue) or style(--themeColor: purple) {
/* <stylesheet> */
}

A style feature without a value evaluates to true if the computed value is different from the initial value for the given property.

If the <style-feature> passed as the style() function's argument is a declaration, the style query evaluates to true if the declaration's value is the same as the computed value of that property for the container being queried. Otherwise, it resolves to false.

The following container query checks if the {{cssxref("computed_value")}} of the container element's --accent-color is blue:

@container style(--accent-color: blue) {
/* <stylesheet> */
}

[!NOTE] If a custom property has a value of blue, the equivalent hexidecimal code #0000ff will not match unless the property has been defined as a color with {{cssxref("@property")}} so the browser can properly compare computed values.

Style features that query a shorthand property are true if the computed values match for each of its longhand properties, and false otherwise. For example, @container style(border: 2px solid red) will resolve to true if all 12 longhand properties (border-bottom-style, etc.) that make up that shorthand are true.

The global revert and revert-layer are invalid as values in a <style-feature> and cause the container style query to be false.

Specifications

Browser compatibility

See also