HTML & CSS
One Container Query Beats Five Breakpoints
A breakpoint is a guess about where a layout breaks. A container query is the layout telling you.
Most responsive CSS is written against the viewport, which is a proxy for the only thing that actually matters: how much room the component has. A card in a narrow sidebar and a card in a wide article need different layouts at the same screen width, and no media query can tell them apart.
Container queries fix that. One component, one rule, and it responds to its own box.
The pattern
.card {
container-type: inline-size;
display: grid;
gap: 1rem;
padding: 1.25rem;
}
@container (min-width: 34rem) {
.card {
grid-template-columns: 14rem 1fr;
align-items: start;
}
}Two declarations and a rule. No component prop, no `useMediaQuery`, no JavaScript at all. The same card stacks in a 300px rail and goes two-up in a 720px column, and the browser recalculates on resize, on sidebar collapse, and on a font-size change.
Where a media query still wins
- Page-level chrome — a nav that becomes a drawer is a decision about the page, not about a component's box.
- Input modality — `hover: hover` and `pointer: coarse` describe the device, and no container knows about your finger.
- Print — a different medium entirely.
- User preferences — reduced motion, contrast, colour scheme.
The useful mental model: media queries ask what is the environment, container queries ask how much room do I have. Most component layout is the second question, which is why most component layout is better served by a container query.
Grid already answered half of this
If the thing you are building is a row of equal cards, you do not need a breakpoint at all. `repeat(auto-fit, minmax(18rem, 1fr))` fits as many columns as the width allows and collapses to one on its own. That single line replaces three media queries and never produces the awkward two-and-a-half-column state.
.grid {
display: grid;
gap: 1.5rem;
grid-template-columns: repeat(auto-fit, minmax(min(18rem, 100%), 1fr));
}The `min(18rem, 100%)` is the detail that matters. Without it, a 320px screen gets a 288px minimum inside a 320px container with padding and overflows by a few pixels — the classic mobile horizontal scrollbar that nobody can reproduce on a desktop.
Fluid type without breakpoints
h1 { font-size: clamp(1.75rem, 1.2rem + 2.4vw, 2.875rem); }
p { font-size: clamp(1rem, 0.96rem + 0.2vw, 1.125rem); }Two lines replace the small/medium/large triplet, and the type scales continuously instead of jumping at 768px. Keep a hard floor — that first number — so a 280px screen does not get a 14px headline.
A breakpoint is a guess about where a layout breaks. A container query is the layout telling you.