Resources
Jul 13, 2025

Container Queries in CSS: Responsive Design for 2025 and Beyond

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.

Container Queries in CSS: Responsive Design for 2025 and Beyond

Published July 2025 • 10 min read

Why Container Queries matter in 2025

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.

1 — Quick syntax refresher

.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.

2 — Creating your first query container

  1. Wrap any component—card, nav bar, sidebar—in a div.
  2. Add container-type: inline-size (or size to include height).
  3. Optionally name it: 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.

3 — Real-world pattern: Auto-upgrading cards

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.

4 — Fallback strategies

IE is long dead, but some enterprise Chrome 90 stragglers remain. Use one of these:

  • @supports test → load baseline CSS first:
@supports (container-type: inline-size) {
  /* container-query overrides here */
}
  • cq-polyfill (npm i css-containerquery)—adds ResizeObserver-based fallbacks. Only load in unsupported browsers via <script nomodule>.

5 — Performance and debugging tips

  • Limit containers: every container sets up a ResizeObserver. Use them on structural wrappers, not on every paragraph.
  • Chrome DevTools → Rendering → Enable Container Queries overlay shows active breakpoint lines in real time.
  • Remember: queries evaluate from the inside out; nested containers never ask about viewport.

6 — Component libraries & design systems

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.

7 — When media queries still win

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.

8 — Copy-paste starter snippet

/* 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;}
}

Conclusion

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.

Subscribe to our weekly newsletter

Lorem ipsum dolor sit amet consectetur mi urna tellus dignissim duis at in tempor mauris morbi fermentum dolor lobortis aliquam maecenas.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Subscribe Newsletter Image - Subscription X Webflow Template