Sub topic 3
Writing @container size queries
This chapter assumes you know CSS selectors and the cascade, media queries with viewport units, and flexbox or grid basics. You do not need to know container query units or style queries yet. Later chapters cover those.
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 same card component can then be two columns in a wide main column and one column in a narrow sidebar, with no change to the page layout rules.
Writing one takes two steps:
- Mark an ancestor as a query container with
container-type. - Write an
@containerrule whose conditions are checked against that ancestor.
Step 1: Declare the container
Here is the markup used in 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 element we will restyle.
.card-wrapper {
container-type: inline-size;
container-name: card;
}container-type: inline-size lets descendants query the wrapper's inline size, which is its width in horizontal writing modes. It also applies containment, so the wrapper's width can no longer depend on its children. The other useful value is size, which allows queries on both axes. It is stricter, because the element's height must then also come from outside its content.
container-name gives the container a name so a query can target it. Naming is covered in the next chapter. We use it here only so the example reads clearly.
Step 2: Write the @container rule
.card {
display: block;
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}The card is a single block by default. When the card container is at least 30rem wide, the card becomes a two-column grid. The viewport width does not matter. Put the container's width in a narrow sidebar and the card stays stacked, even on a large monitor.
What you can test
Size conditions go in parentheses after the optional container name. These features are available:
| Feature | Meaning |
|---|---|
| `width`, `height` | Physical dimensions of the container |
| `inline-size`, `block-size` | Dimensions along the writing direction |
| `aspect-ratio` | Ratio of width to height |
| `orientation` | `portrait` or `landscape` |
height, block-size, aspect-ratio and orientation need a container whose axes are all size-contained, so use container-type: size for those. Width and inline-size work with inline-size.
You can use the same range syntax as media queries, and you can combine conditions:
@container card (20rem <= width < 40rem) {
.card h2 {
font-size: 1.25rem;
}
}
@container card (min-width: 30rem) and (max-width: 50rem) {
.card {
gap: 0.5rem;
}
}The keywords and, or and not work as they do in media queries. Mixing and with or requires extra parentheses.
Which container is queried?
A query looks up the tree from the styled element and uses the nearest ancestor that is a query container and matches the optional name. Without a name, the nearest container that can answer the question is used. The query never matches the element itself. How nesting and names interact gets its own chapter. For now, remember that the container must be an ancestor.
Common errors
- Styling the container itself. If
.card-wrapperis both the container and the target ofdisplay: grid, the rule never applies. An element cannot respond to its own size. Put the styles on a descendant, or add a wrapper. - A container with no width of its own. Inline-size containment ignores the children when computing width. A container that is shrink-to-fit, such as a floated or
display: inline-blockelement, can collapse to zero width. Give it a width from its parent, for example a block-level element, a grid track or a flex item with a basis. - Querying height on an
inline-sizecontainer. The condition will not match. Switch tocontainer-type: sizeand give the container a definite height. - Putting the container-type on the wrong element. If nothing happens, check that the rule's selector target is inside the element with
container-type.
Exercise
A .sidebar element has container-type: inline-size. You want .sidebar itself to get a 2rem padding when it is wider than 20rem, using @container (min-width: 20rem) { .sidebar { padding: 2rem; } }. Why does this not work, and how do you fix it?
Show answer
A container query only matches descendants of the container, never the container itself, so .sidebar cannot be styled by a query on its own width. Wrap it in a new element that has container-type: inline-size, or move the container declaration to a parent, so that .sidebar becomes a descendant of the queried container.