Sub topic 1
Why Viewport Media Queries Fall Short for Components
Prerequisites
This chapter assumes you know the following:
- Core CSS: selectors, the cascade, and the box model.
- Basic layout with flexbox or grid.
- How
@mediaqueries and responsive design work.
You don't need any container query knowledge yet. This chapter explains the problem that container queries solve.
The working model of a media query
A media query tests the state of the viewport (the visible area of the browser window) or the device. The most common test is the viewport width. Every element on the page sees the same answer.
.card {
display: block;
}
@media (min-width: 700px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}This rule says: when the viewport is at least 700px wide, lay out every .card as two columns. The rule never asks how wide the card itself is.
That works when a card always takes the full page width. It breaks once the same card appears in different places.
Where the model breaks
A component is a reusable piece of UI, such as a card, a menu, or a form field. A good component should look right wherever you put it. Viewport queries tie a component's layout to the page instead of to its own space.
Suppose a page has a main column and a narrow sidebar, and both contain a .card. On a 1200px viewport, the media query above matches. Both cards switch to two columns. The sidebar is only 300px wide, so its card gets squeezed. The main-column card has plenty of room and looks fine.
The query measures the viewport, which is 1200px for both cards. The card in the sidebar needs the single-column layout, but the media query can't tell it that.
Common workarounds and their costs
Developers usually respond in one of three ways.
| Workaround | Cost |
|---|---|
| Add context selectors such as `.sidebar .card` inside the media query | The component now knows about its surroundings. Every new location needs new rules. |
| Create modifier classes such as `.card--narrow` and apply them by hand | The markup must be updated whenever the layout changes. The class can drift out of sync with the real width. |
| Measure the element with JavaScript (`ResizeObserver`) and toggle classes | Styling logic moves out of CSS. It adds code to maintain and can cause extra layout work. |
All three work, but each one gives the component knowledge about the page. That is the opposite of reuse.
What you actually want
The real requirement is that the card's layout should depend on the space the card has. The page then decides where the card goes, and the card adapts on its own. In CSS terms, the query should look at an ancestor element's size instead of the viewport's size.
Container queries provide exactly that. The pattern is the same as the media query above, but the condition is evaluated against a container:
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}With this version, the main-column card gets two columns and the sidebar card stays in one column, at the same viewport width. The following chapters cover each piece of this syntax. For now, note the shift: the component asks about its own container, not about the page.
When media queries are still the right tool
Container queries don't replace media queries. Keep using @media for things that really belong to the viewport or the user's environment:
- Overall page layout, such as when to show or hide a main navigation.
- User preferences, such as
prefers-color-schemeandprefers-reduced-motion. - Print styles and device features like
hoverorpointer.
A useful rule is that page-level decisions use media queries and component-level decisions use container queries.
Exercise
A .card appears once in a 800px main column and once in a 300px sidebar, on a 1200px viewport. A rule inside @media (min-width: 700px) switches .card to two columns. What happens to each card, and why is this a problem?
Show answer
Both cards switch to two columns, because the media query tests the 1200px viewport and both cards see the same result. The sidebar card is only 300px wide, so the two-column layout is cramped. The query can't account for the space each card actually has.
Exercise
Name two decisions that should still use @media queries even after you adopt container queries.
Show answer
Any two of these: whole-page layout changes such as showing or hiding the main navigation, user preference queries such as prefers-color-scheme or prefers-reduced-motion, and print or device-feature queries such as hover and pointer.