Lesson 5 of 9 · 22m
Container Queries and Fluid Design
Responsive to the component's home, not the viewport — @container, container query units, clamp() typography, and why this replaces most breakpoint thinking.
Media queries answer the wrong question. They ask "how wide is the viewport?" when what a component actually needs to know is "how much space do I have?" A card in a wide main column and the same card in a narrow sidebar need different layouts — and with media queries that's impossible to express without brittle context-specific overrides. Container queries fix the question.
The setup that trips everyone
.sidebar, .main {
container-type: inline-size; /* this element is now a query container */
}
.card {
display: grid;
gap: 0.75rem;
}
@container (min-width: 30rem) {
.card {
grid-template-columns: 8rem 1fr; /* thumbnail + content side by side */
}
}Two hard-won details. First: the containment must be on an ancestor, not the element you're styling — .card queries the size of its container, and the container can't be the card itself. In practice you either add container-type to layout regions (.sidebar, .main) or wrap each component in a thin wrapper div. Second: container-type: inline-size applies layout containment — the element's size can no longer depend on its contents on that axis. If your sidebar suddenly collapses to zero width, that's what happened; it needs its width from the layout (grid track, flex basis), not from its children.
Named containers when nesting gets real
.widget { container: widget / inline-size; }
@container widget (min-width: 24rem) {
.widget-body { columns: 2; }
}container is shorthand for container-name + container-type. Naming matters once containers nest: an unqualified @container query resolves against the nearest ancestor container, which is sometimes not the one you meant.
Fluid type and spacing without any queries at all
h1 { font-size: clamp(1.75rem, 1rem + 2.5vw, 3rem); }
.section { padding: clamp(1rem, 0.5rem + 3vw, 4rem); }clamp(min, preferred, max) interpolates linearly between the extremes. The vw term makes it scale with the viewport; the rem terms in the mix keep it responsive to user font-size preferences (a11y requirement — pure vw/px fluid type ignores zoomed browser font settings). For type scales, generate the clamp values once with a tool like utopia.fyi and commit them as tokens.
Container query units
cqi/cqw are percentages of the container's inline size / width — the container-aware version of vw. font-size: 5cqi inside a card means the type scales with the card, not the viewport. Use sparingly: it's powerful for tightly-coupled component internals and confusing when overused.
Field notes
- Container queries shipped in all modern browsers in 2023 — safe for production now.
- You can't style the container itself based on its own size — that's a circular dependency, and the spec forbids it. Style children.
- Media queries aren't dead: viewport-level concerns (nav collapse, overall page grid) are still media-query territory. Container queries are for components.
Was this lesson helpful?