Sub topic 3
Writing @container size queries
This chapter assumes you know CSS selectors and the cascade, have written media queries, and are comfortable with flexbox or grid. It also assumes you know what container-type does. You declared containers in the previous chapter. Here you write the rules that query them.
From media queries to container queries
A media query asks about the viewport. A container query asks about the size of an ancestor element. The syntax is close, but the question is different, so the same component can sit in a wide main column and a narrow sidebar and pick the right layout in each place.
A size query has three parts:
- A container, which is an ancestor with
container-typeset. - An
@containerrule with a condition, such as(min-width: 30rem). - Style rules inside that block, which apply to descendants of the container.
Here is the markup used throughout this chapter.
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>The wrapper is the container. The article is the styled descendant.
A first size query
.card-wrapper {
container-type: inline-size;
container-name: card;
}
.card {
display: block;
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}container-type: inline-size makes the wrapper a query container for its inline axis. In horizontal writing modes, that is the width. container-name: card gives it a name. The @container card (min-width: 30rem) rule applies only when the nearest ancestor container named card is at least 30rem wide.
The default .card rule is the narrow layout. The query adds the wide layout on top, which is the same mobile-first pattern you use with min-width media queries.
Which features you can query
Size conditions use the same range of features as media queries, but they measure the container's content box.
| Feature | Measures |
|---|---|
| `width`, `height` | Physical width and height of the container |
| `inline-size`, `block-size` | Size along the inline or block axis, which follows the writing mode |
| `aspect-ratio`, `orientation` | Shape of the container |
The logical features inline-size and block-size are the safer choice if your content may be displayed in a vertical writing mode. For ordinary horizontal text, inline-size and width give the same result.
You can also use the range syntax and combine conditions.
@container (inline-size > 30rem) {
.card { gap: 1.5rem; }
}
@container (20rem <= inline-size <= 40rem) {
.card { padding: 1rem; }
}
@container card (min-width: 30rem) and (max-width: 60rem) {
.card { grid-template-columns: 6rem 1fr; }
}
@container (min-width: 50rem) or (orientation: landscape) {
.card { font-size: 1.125rem; }
}
@container not (min-width: 30rem) {
.card { border-width: 1px; }
}Use and, or, and not as you would in media queries. You cannot mix and and or at the same level without parentheses.
What the container type allows you to query
The container-type value decides which axes you can query.
| Value | Queryable axes | Notes |
|---|---|---|
| `inline-size` | Inline only | The usual choice. The block size still depends on content. |
| `size` | Inline and block | You must give the container a size in both directions yourself. |
| `normal` | None for size queries | Default. The element can still be a style query container. |
If you query height on an inline-size container, the condition does not match. It does not warn you. Use container-type: size only when you really need height queries, and give the container an explicit height.
Resolving the target container
When the browser evaluates an @container rule for an element, it searches up the ancestor chain, starting at the parent. It uses the first container that can answer the query. Three details matter here.
- The element is never its own container. An element cannot query itself. A rule like
.card-wrapper { container-type: inline-size; }followed by@container (min-width: 30rem) { .card-wrapper { ... } }never styles the wrapper. Style a child, or add another wrapper. - Unnamed queries use the nearest container that supports the features. A query on
(min-width: 30rem)skips ancestors that cannot answer it. For example, it skips an ancestor withcontainer-type: normal. - Named queries only consider containers with that name.
@container card (...)skips every ancestor not namedcard, even if a closer one exists.
An element can have several names, separated by spaces, such as container-name: card panel;. A query for either name matches it.
.layout {
container: layout / inline-size;
}
.sidebar {
container: sidebar / inline-size;
}
.card {
display: block;
}
/* Uses the nearest container of any name. This is .sidebar if the card is inside it. */
@container (min-width: 30rem) {
.card { display: grid; grid-template-columns: 8rem 1fr; }
}
/* Always uses .layout, even when .sidebar is closer. */
@container layout (min-width: 60rem) {
.card { padding: 2rem; }
}Names make the intent explicit and protect you when someone later adds a new container between the component and the one you meant.
Writing the rules well
Style descendants, not the container
Rules inside @container can target any descendant of the container. They can also use selectors that match descendants through the container, but the subject of the selector must be a descendant. A common pattern is to give the component a thin wrapper whose only job is to be the container.
Keep the base case outside the query
Put the default layout in normal rules. Use the query only for changes. If you put the base layout inside a query, any browser or situation where the query does not match gets no layout at all.
Use relative units for breakpoints
Write breakpoints in rem or em rather than px. They scale with the user's font size setting. Note that inside a container query, em and rem do not refer to the container. rem refers to the root font size. em refers to the font size of the element that the query is evaluated against, which is the container's computed font size for the condition.
Nested containers
You can nest queries. Each @container block can contain another, and a rule applies only when all enclosing conditions match.
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
@container card (min-width: 50rem) {
.card {
grid-template-columns: 12rem 1fr;
}
}
}Both conditions are checked against the container named card. Nesting is useful when the inner condition only makes sense in the context of the outer one, but a flat set of rules is easier to read in most cases.
A complete, runnable example
This page shows the same card in a wide main area and a narrow aside. Resize the window and you will see that each card decides on its own.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Container query demo</title>
<style>
body {
font-family: system-ui, sans-serif;
display: grid;
grid-template-columns: 2fr 1fr;
gap: 1rem;
margin: 1rem;
}
.card-wrapper {
container: card / inline-size;
}
.card {
display: block;
border: 1px solid #888;
padding: 1rem;
}
.card .thumb {
background: #cde;
height: 4rem;
margin-block-end: 0.5rem;
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
.card .thumb {
height: auto;
margin-block-end: 0;
}
}
</style>
</head>
<body>
<main>
<div class="card-wrapper">
<article class="card">
<div class="thumb"></div>
<div>
<h2>Main column</h2>
<p>This card has room, so it uses two columns.</p>
</div>
</article>
</div>
</main>
<aside>
<div class="card-wrapper">
<article class="card">
<div class="thumb"></div>
<div>
<h2>Aside</h2>
<p>This card is narrow, so it stacks.</p>
</div>
</article>
</div>
</aside>
</body>
</html>Both cards use identical CSS. The viewport is the same for both, so a media query could not tell them apart.
Common errors with size queries
- The query never matches. Check that an ancestor has
container-typeset. Without it, no container exists and the block is ignored. - Querying the element itself. The container's own styles cannot depend on its own size. Move the styles to a child.
- Height queries do nothing. The container is
inline-size. Switch tosizeand give it an explicit height, or redesign around width. - The container collapses to zero width. Inline-size containment means the container's width does not come from its content. In a flex row or a shrink-to-fit context, it can shrink to nothing. Give it a width,
flex: 1, or put it in a grid track. - A named query misses. Compare the name in the query with the name on the container. Names are case-sensitive.
- Style appears in one place and not another. A different ancestor may be the nearest container. Use a name to pin the target.
Exercise
A page has .page { container: page / inline-size; } (1200px wide) and, inside it, .sidebar { container: sidebar / inline-size; } (260px wide). A .card inside the sidebar is styled with @container (min-width: 30rem) { .card { display: grid; } }, where 1rem is 16px. Does the card become a grid? What single change makes the card respond to the page width instead?
Show answer
No. The unnamed query uses the nearest container that can answer it, which is .sidebar, and 260px is less than 480px (30rem), so the condition fails. Naming the query, as in @container page (min-width: 30rem), makes it skip the sidebar and evaluate against .page, which is 1200px wide, so the card becomes a grid.
Exercise
You write .box { container-type: inline-size; } @container (min-width: 20rem) { .box { background: gold; } }. The box is 40rem wide, but it never turns gold. Why, and how do you fix it?
Show answer
An element cannot query itself, so the rule never matches .box. Wrap the box in a parent that is the container, or put the background on a child of .box.