Sub topic 15
When to use container queries instead
This chapter assumes you can write @media rules with min-width, build a mobile-first grid, and use the box model. A media query asks about the viewport. A container query asks about the space a component actually has. This chapter shows when that difference matters.
The problem with viewport-only queries
Suppose a .card appears in a wide main column and also in a narrow sidebar. A media query at min-width: 40em looks at the whole window. On a large screen it fires for both places, so the card in the 15em sidebar gets the wide layout and breaks.
The workaround is extra selectors such as .sidebar .card. That ties the component to its location and multiplies rules. A container query removes the need for it.
Declaring a container
A container query has two parts. First, mark an ancestor as a container. Then write a rule that tests that ancestor's size.
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Summary text.</p>
</article>
</div>.card-wrapper {
container-type: inline-size;
}
.card {
display: block;
}
@container (min-width: 30em) {
.card {
display: flex;
gap: 1rem;
}
}container-type: inline-size lets descendants query the wrapper's inline size, which is its width in horizontal writing modes. The @container rule applies to .card whenever the nearest container ancestor is at least 30em wide. The same card now adapts in the main column, the sidebar, or a modal.
Choosing between the two
| Question | Use |
|---|---|
| Does the change affect the overall page structure, such as showing a sidebar? | Media query |
| Does a reusable component need to adapt to where it is placed? | Container query |
| Does it depend on user settings such as dark mode or reduced motion? | Media query |
| Does it depend on hover, pointer, or print? | Media query |
Container queries only measure element size. Preferences, input capabilities, and media types are properties of the device or user, so they stay in @media.
In practice, the two work together. Use media queries for the page shell, such as the number of columns in the main layout. Use container queries inside components so they respond to the space the shell gives them.
Common errors
- Forgetting
container-type. Without it,@containerrules never match. - Styling the container itself inside its own
@containerrule. Move the styles to a child element. - Using viewport units inside a container query and expecting them to follow the container.
vwstill refers to the viewport. - Assuming support is universal. Container size queries work in current major browsers. For older ones, keep a sensible default layout outside the
@containerrule so the component still works.
Exercise
A .promo component sits in a full-width section and in a 20em sidebar. You want it to switch to a two-column layout only when its own space is at least 30em. Which two CSS pieces do you need, and why does a @media (min-width: 30em) rule fail here?
Show answer
Give the component's wrapper container-type: inline-size, then write @container (min-width: 30em) { .promo { ... } }. The @media rule fails because it measures the viewport. On a wide screen it matches even when the sidebar is only 20em wide, so the sidebar copy gets the two-column layout too.
Exercise
You want to disable animations for users who prefer reduced motion. Should you use a media query or a container query?
Show answer
A media query. prefers-reduced-motion is a user setting, and container queries only test the size of an element.