- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- Container Queries
CSS Responsive Design
Container Queries
Adapt a component to the space provided by its container instead of the size of the viewport. You will establish named query containers, write size and style queries, use container-relative units, and decide when media queries remain appropriate.
Media queries ask how wide the screen is. Container queries ask how wide the box is that the component sits in. That is the question you usually meant: the same card needs a different layout in a narrow sidebar than in a wide main column, even on the same screen.
Container queries do not replace CSS Media Queries. Use media queries for the page as a whole, and container queries for components that can appear in different places. Both are part of making things adapt, alongside Feature Queries.
Two steps: mark the container, then query it
Example
css
.card-region {
container: cards / inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container cards (width >= 34rem) {
.card {
grid-template-columns: 10rem 1fr;
}
}First, container: cards / inline-size turns .card-region into a container named cards that can be measured by its width. Then @container cards (width >= 34rem) says: when that container is at least 34rem wide, give the card two columns, a 10rem one and one for the rest. Drop the same card in a narrow sidebar, and it stays stacked.
Two common mistakes. The query looks at an ancestor, never at the element itself, so you cannot query .card from inside .card's own rule. And without container-type or container on an ancestor, @container silently matches nothing. Naming the container, as above, also stops a nested component from accidentally measuring the wrong box.
Units that follow the container
Example
css
.card__title {
font-size: clamp(1.2rem, 4cqi, 2rem);
}cqi is 1% of the container's inline size (its width, in English text). Here the title grows with the card's space instead of the window's, within a sensible minimum and maximum. Related units are cqw, cqh, cqmin and cqmax.
Style queries
Example
css
@container style(--featured: true) {
.card { border-color: gold; }
}Instead of a size, this checks a custom property on the container. When an ancestor sets --featured: true, the cards inside get a gold border, without an extra class on every card.
Query a container's scroll state
Scroll-state queries can respond when a sticky element is stuck, an item is snapped, or a container can still scroll in a direction. Declare a scroll-state query container, then test its state from an @container rule.
Example
css
.section-heading {
position: sticky;
inset-block-start: 0;
container: section-heading / scroll-state;
}
@container section-heading scroll-state(stuck: top) {
.section-heading__content {
box-shadow: 0 0.35rem 0.8rem rgb(15 23 42 / 0.18);
}
}Container Query Reference
| Syntax | Purpose | Example |
|---|---|---|
| container-type | Establishes a size query container. | container-type: inline-size |
| container-name | Names a query container. | container-name: card |
| @container | Applies rules according to container conditions. | @container card (width > 30rem) |
| cqw | Represents one percent of the query container's width. | font-size: 4cqw |
| scroll-state() | Tests whether a container is stuck, snapped, scrolled, or scrollable. | scroll-state(stuck: top) |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Container Queries example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
.card-region {
container: cards / inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container cards (width >= 34rem) {
.card {
grid-template-columns: 10rem 1fr;
}
}
.card__title {
font-size: clamp(1.2rem, 4cqi, 2rem);
}
@container style(--featured: true) {
.card { border-color: gold; }
}
.section-heading {
position: sticky;
inset-block-start: 0;
container: section-heading / scroll-state;
}
@container section-heading scroll-state(stuck: top) {
.section-heading__content {
box-shadow: 0 0.35rem 0.8rem rgb(15 23 42 / 0.18);
}
}
</style>
</head>
<body>
<main class="card-region"><article class="card"><div aria-hidden="true">◼</div><section><h1 class="card__title">Container-aware card</h1><p>Resize the preview. The card responds to its container.</p></section></article></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| @container size queries | 105 | 105 | 110 | 16 |
| Scroll-state queries | 133 | 133 | No | No |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Size queries work in every current browser. Scroll-state queries are Chrome and Edge only for now, so use them only to add a small extra touch, such as a shadow on a stuck header.
Container Queries exercises
A container
Make the wrap a container for queries on its inline size.
The HTML your code works on
<style>
.wrap { width: 400px; }
</style>
<div class="wrap">
<div class="card">
<h2 class="name">Tea</h2>
<p class="text">Hot and fresh.</p>
</div>
</div>Result
