@container queries

The @container media query is a new CSS feature that allows you to apply styles to an element based on the size of its container¹. This is useful for creating responsive components that can adapt to different layouts and contexts.
To use @container media queries, you need to do two things:
  • First, you need to create a containment context on the element that will act as the container for your query. You can do this by using the container-type property with a value of size, inline-size, or normal¹. For example:
.post { container-type: inline-size; }
  • Second, you need to write a @container at-rule with a condition that specifies the size or aspect ratio of the container. You can use descriptors like width, height, inline-size, block-size, aspect-ratio, or orientation². For example:
/* If the container is larger than 700px */ @container (min-width: 700px) { .card h2 { font-size: 2em; } }
This will apply a larger font size for the card title if the container is wider than 700px.
You can also use logical keywords like and, or, and not to combine multiple conditions². For example:
/* If the container is larger than 700px and has a landscape orientation */ @container (min-width: 700px) and (orientation: landscape) { .card h2 { font-size: 2em; } }
You can also name your containment contexts using the container-name property and target them using their name in the @container at-rule². For example:
.post { container-name: sidebar; container-type: inline-size; } @container sidebar (min-width: 700px) { .card h2 { font-size: 2em; } }
This will apply styles only to containers with the name "sidebar".
I hope this helps you understand how to use @container media queries in CSS. If you want to learn more, you can check out these resources:
Source: Conversation with Bing, 3/19/2023(1) CSS Container Queries - CSS: Cascading Style Sheets | MDN - Mozilla. https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries Accessed 3/19/2023. (2) @container - CSS: Cascading Style Sheets | MDN. https://developer.mozilla.org/en-US/docs/Web/CSS/@container Accessed 3/19/2023. (3) CSS Container Queries: Use-Cases And Migration Strategies. https://www.smashingmagazine.com/2021/05/css-container-queries-use-cases-migration-strategies/ Accessed 3/19/2023. (4) Next Gen CSS: @container | CSS-Tricks - CSS-Tricks. https://css-tricks.com/next-gen-css-container/ Accessed 3/19/2023.

Working example

Let's say you have a simple card component with an image, a title, some text, and a button. You want to change the layout and appearance of the card depending on how much space it has in its container.
Here is the HTML markup for the card:
<div class="card"> <img src="image.jpg" alt="Image"> <div class="content"> <h3>Title</h3> <p>Some text</p> <button>Click me</button> </div> </div>
And here is the basic CSS for the card:
.card { display: flex; align-items: center; border: 1px solid gray; padding: 1rem; } .card img { width: 150px; height: 150px; } .card .content { margin-left: 1rem; }
Now, let's say you want to make the following changes based on the size of the card's container:
  • If the container is narrower than 400px, stack the image and content vertically instead of horizontally.
  • If the container is narrower than 300px, hide the button and reduce the font size of the title and text.
  • If the container is wider than 600px, increase the font size of the title and text.
To do this with @container media queries, you need to do two things:
  • First, you need to create a containment context on an element that will act as the container for your query. You can use any element that wraps around your card component. For example, you can use a div with a class of container:
<div class="container"> <div class="card"> <!-- ... --> </div> </div>
Then, you need to use the container-type property with a value of size on this element:
.container { container-type: size; }
This will make your element a query container for both inline-size and block-size dimensions.
  • Second, you need to write @container at-rules with conditions that specify the size or aspect ratio of your query container. You can use descriptors like width, height, inline-size, block-size, or aspect-ratio. For example:
/* If the container is narrower than 400px */ @container (max-width: 400px) { }
Inside this at-rule, you can write any CSS declarations that you want to apply when this condition is true. For example:
/* If the container is narrower than 400px */ @container (max-width: 400px) { /* Stack image and content vertically */ .card { flex-direction: column; } .card img { width: auto; height: auto; } .card .content { margin-left:0; margin-top:1rem; } }
You can repeat this process for other conditions using different descriptors or values. For example:
/* If the container is narrower than 300px */ @container (max-width:300px) { /* Hide button and reduce font size */ .card button { display:none; } .card h3, .card p { font-size:.8rem; } } /* If container is wider than than600 px */ @container (min-width :600 px) { /* Increase font size */ .card h3, .card p { font-size :1.2 rem; } }
This way, you can create responsive components that can adapt to different layouts and contexts using @container media queries.
I hope this example helps you understand how to use @container media queries in CSS. If you want to see it in action, you can check out this CodePen demo⁵:
If you want to learn more about @container media queries syntax and features², use cases³⁴, or polyfills⁶, you can check out these resources:
  • [Inside The Container Query Polyfill -
Source: Conversation with Bing, 3/19/2023(1) Container Query Proposal & Explainer - OddBird. https://css.oddbird.net/rwd/query/explainer/ Accessed 3/19/2023. (2) CSS Container Queries - CSS: Cascading Style Sheets | MDN - Mozilla. https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries Accessed 3/19/2023. (3) Next Gen CSS: @container | CSS-Tricks - CSS-Tricks. https://css-tricks.com/next-gen-css-container/ Accessed 3/19/2023. (4) @container - CSS: Cascading Style Sheets | MDN. https://developer.mozilla.org/en-US/docs/Web/CSS/@container Accessed 3/19/2023. (5) Inside the container query polyfill - Chrome Developers. https://developer.chrome.com/blog/inside-the-container-query-polyfill/ Accessed 3/19/2023. (6) Container Query Proposal & Explainer - OddBird. https://css.oddbird.net/rwd/query/explainer/ Accessed 3/19/2023.

Old Version

@container queries are a special type of CSS rule that allows web developers to apply styles to different elements based on their container size. It's like having different sets of clothes for different sizes, where the size of the container is the deciding factor.

How it works

When a web page is loaded, the browser calculates the size of each container element, such as a <div> or <section>. With @container queries, developers can apply styles to these elements based on their calculated size.
For example, if a developer wants to change the font size of a text element when its container is smaller than 500 pixels, they can use the following code:
.container { font-size: 16px; /* default font size */ } @media (max-width: 500px) { .container { font-size: 14px; /* font size for smaller containers */ } }
In this example, the default font size for the .container element is 16 pixels. However, when the container is smaller than 500 pixels, the font size changes to 14 pixels.

Benefits of @container queries

@container queries offer several benefits for web developers:
  • They allow for more efficient and flexible responsive design, as developers can tailor styles to specific container sizes.
  • They simplify code by allowing developers to avoid unnecessary duplication of styles for different screen sizes.
  • They provide a more intuitive way of thinking about responsive design, as developers can think in terms of container sizes rather than device sizes.

Browser support

Currently, @container queries are not officially supported in any major browser. However, there are several polyfills and experimental implementations available that allow developers to use @container queries today. Here are a few resources for further exploration: