Media queries respond to the viewport, but a reusable component often needs to respond to the space it has, not the whole page — container queries finally solved this after years of JavaScript workarounds.
.card-container {
container-type: inline-size;
container-name: card;
}
/* This card looks different depending on ITS container's width,
not the viewport - works the same whether it's in a wide main
area or a narrow sidebar */
@container card (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 150px 1fr;
}
}
@container card (max-width: 399px) {
.card {
display: block;
}
}
Before container queries, building a truly reusable component that adapted to its context meant JavaScript measuring the container’s size on resize — a real performance and complexity cost that container queries eliminate entirely for pure layout concerns.