Sub topic 13
Container queries for component-level breakpoints
This chapter assumes you can write mobile-first media queries, use CSS Grid, and know what a design frame is. Media queries ask how wide the viewport is. Sometimes that is the wrong question.
The problem with viewport-based breakpoints
Suppose a card component appears in two places: a wide main column and a narrow sidebar. At a 1200px viewport, the sidebar can still be only 280px wide. A media query for min-width: 48em fires on the viewport, so the card gets its two-column layout inside a space that is too small for it.
You could write extra selectors such as .sidebar .card to override the layout. That couples the component to every place it is used. A container query avoids this by asking about the width of the component's own container.
Declaring a container
Two steps are needed. First, mark an ancestor as a container. Then write an @container rule that targets descendants of that ancestor.
.card-list {
container-type: inline-size;
}
@container (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}container-type: inline-size tells the browser to track the container's inline size, which is its width in horizontal writing modes. The .card base styles stay as the narrow, stacked layout. This is the same mobile-first pattern you used with min-width media queries. The only change is what the width is measured against.
A container query never styles the container itself. It styles elements inside it. That is why the rule targets .card and not .card-list.
How it behaves in two places
The viewport is identical for both cards. Only the container width differs, and that is what the rule checks. With a threshold of 30rem (480px at the default font size), the 700px container qualifies and the 280px one does not.
Container queries versus media queries
| Question | Use |
|---|---|
| Is the whole page layout changing (columns, navigation)? | Media query |
| Does a reusable component need to adapt to its slot? | Container query |
| Is the input device touch or mouse? | Media query (`hover`, `pointer`) |
| Should the user's preferences apply (reduced motion, color scheme)? | Media query |
Container queries only measure size. Input capabilities and user preferences remain media features. Most real projects use both: media queries shape the page, and container queries shape components inside it.
Common errors
- No container declared. If no ancestor has
container-type, the@containerrule never matches and nothing errors. Check the browser's dev tools for the container badge on the element. - Styling the container itself. The query can only change descendants. If you need to change the container's own layout, wrap it in another element and make the wrapper the container.
- Circular sizing.
inline-sizecontainment means the container's width cannot depend on its children's content. A container that relies on its content to set its width, such as a shrink-to-fit inline-block, may collapse to zero. Give it a width from its parent, for example by making it a block or grid item. - Choosing thresholds from devices. Pick the container width where the component's content starts to look cramped or wasteful, as you did for page breakpoints. Do not use phone or tablet sizes.
Where this fits in your frames
Your mobile, tablet, and desktop frames still decide the page-level layout. Container queries add a second layer. In the design tool, treat a component such as the card as having its own small set of widths, for example "narrow" and "wide" variants. Then record the container threshold next to the component, not in the global breakpoint tokens. Developers can then see that the card changes at 30rem of container width, whatever the viewport is doing.
Exercise
A product card has a two-column layout when @media (min-width: 48em) is true. It breaks when placed in a 300px sidebar on a desktop screen. What change fixes this, and what must you add to the markup or CSS for it to work?
Show answer
Replace the media query with @container (min-width: 30rem) (or another threshold based on where the card looks cramped). Then declare an ancestor of the card, such as the list or sidebar wrapper, as a container with container-type: inline-size. Without that declaration the container rule never matches. The card then stays stacked in the 300px sidebar and switches to two columns in a wide main area.
Exercise
Why does a container query rule that targets the container element itself have no effect?
Show answer
A container query can only style descendants of the container, not the container itself. To change the container's own layout, put it inside a wrapper element and make the wrapper the container.