Learn how container queries finally solve the ‘card inside a grid’ problem, see real-world code examples, and discover performance and browser-support tips for 2025.
Published July 2025 • 10 min read
For two decades, media queries have ruled responsive design. They work great at page level, but collapse when a component moves from a sidebar into a hero or vice-versa. Enter CSS Container Queries—now fully supported in every evergreen browser as of Chrome 113, Safari 17 and Firefox 110. Instead of asking “How wide is the viewport?” we can finally ask “How big is this card right now?” That shift unlocks fluid, design-system-driven layouts—and slashes the number of breakpoints you maintain.
.card {
container-type: inline-size; /* establishes a query container */
}
@container (min-width: 400px) {
.card__title { font-size: 1.5rem; }
.card__grid { grid-template-columns: 1fr 1fr; }
}
Think of container-type
as turning on a local ResizeObserver
. The query fires whenever the inline size (usually width) of .card
crosses a threshold—in this case 400 px.
container-type: inline-size
(or size
to include height).container-name: card;
so you can target multiple containers with @container card (…) {…}
.Gotcha: Containers can’t cycle. A container item can’t also query its descendants that are themselves containers. Keep hierarchies clean.
A common breakpoint mess is a three-column grid that collapses to one column on mobile. With container queries you simply let the grid auto-fit and scale the card internals instead:
.cards {
display: grid;
gap: var(--space-2);
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
}
.card { container-type: inline-size; }
@container (max-width: 320px) {
.card__meta { display:none; }
.card__title { font-size: 1.125rem; }
}
No viewport breakpoints needed. Whether cards sit in a 300 px sidebar or a full-width hero, they adapt autonomously.
IE is long dead, but some enterprise Chrome 90 stragglers remain. Use one of these:
@supports (container-type: inline-size) {
/* container-query overrides here */
}
npm i css-containerquery
)—adds ResizeObserver-based fallbacks. Only load in unsupported browsers via <script nomodule>
.If you’re building with Web Components, React or Webflow’s new Components panel, container queries map 1:1 with the concept of an encapsulated module. Ship each component with its own size-based styles; drop it anywhere and it behaves.
Global typography scales, off-canvas nav toggles and OS-level color-scheme checks remain viewport responsibilities. Use @media
for macro layout; container queries for micro.
/* 1. Define your scale */
:root {
--space-1: .5rem;
--space-2: 1rem;
--space-3: 2rem;
}
/* 2. Cards parent */
.cards {
display:grid;
gap:var(--space-2);
grid-template-columns:repeat(auto-fit, minmax(280px,1fr));
}
/* 3. Card container */
.card { container-type:inline-size; }
@container (min-width:480px){
.card__grid{grid-template-columns:1fr 1fr;}
}
Container queries free us from the tyranny of viewport-centric thinking and bring truly modular, reusable components into everyday CSS. Start by refactoring a single card or pricing table—you’ll wonder how you ever shipped responsive sites without them.
Lorem ipsum dolor sit amet consectetur mi urna tellus dignissim duis at in tempor mauris morbi fermentum dolor lobortis aliquam maecenas.