r/Frontend • • 8d ago

Do you use CSS Container Queries for reusable components, or still rely mostly on media queries?

I've been experimenting with CSS Container Queries, especially for components that appear in different layouts.

One thing I like is that a card can adapt to the space available to it rather than the entire viewport.

For example:

.card-wrapper {
  container-type: inline-size;
}

.card {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

u/container (min-width: 500px) {
  .card {
    grid-template-columns: 140px 1fr;
  }
}

This makes the component more independent of the page layout.

But I'm curious about real-world usage:

  • Have you started using container queries in production?
  • Do you still use media queries for most responsive layouts?
  • Have you encountered any browser support or debugging issues?

I'd be interested in hearing how people are using them in larger component-based projects.

9 Upvotes

9 comments sorted by

5

u/Some-Kitchen8332 8d ago

i use them in production for cards and side panels now. media queries still handle the big page stuff like type scale and when the nav collapses. the fiddly bit is two wrappers both set as containers, the inner one starts querying the wrong width and it takes a minute to notice

1

u/Colby-Jenkins 7d ago

That issue with nested containers is definitely a headache. ive found it helps to avoid conflicting container settings whenever possible. For example, setting a specific width on the inner wrapper can keep it from collapsing incorrectly. I also keep media queries in the mix for broader layout shifts, especially with navigation transitions.

1

u/Some-Kitchen8332 7d ago

yeah locking a width on the inner wrapper is the bit i was missing. the inner one querying the wrong width is so easy to miss

1

u/Lopsided_Love_1611 1d ago

Naming the containers sorts most of that out: container: sidebar / inline-size on the outer one, then @container sidebar (min-width: 500px). A named query only looks at the nearest ancestor container with that name, so the inner wrapper can't steal it. Style queries are handy for the same kind of thing too, @container style(--theme: dark) lets a card react to a custom property its parent sets instead of a width, and the parent doesn't even need container-type for that.

2

u/cuelark 8d ago

one thing that bit me is container-type: inline-size on the wrapper means it can't size itself off its own content anymore, so if the wrapper is shrink-to-fit it collapses to zero width. give it an explicit width or make it a grid/flex child that stretches. also devtools shows a little container badge on the element now which helps when debugging which one is actually being queried.

1

u/GeekyantsReactNative 4d ago

I have started exploring container queries too. They make reusable components much more flexible, especially when the same component appears in different layouts. I still rely on media queries for overall page responsiveness, but combining both seems like the best approach.

1

u/noahfromphilly 3d ago

My team has moved over to using container queries quite a bit. We don't use them for everything, but they can be very useful for individual components. Being able to influence the layout and design of a component based on its container instead of the viewport is very useful.

1

u/Tobi007-del 2d ago

I found them most useful in multi-pane layouts say a chat app with a sidebar even when the main page still has a sidebar, in that unique case, media queries couldn't help properly so in places where nesting can make a component scoped more to it's parent than the window, they shine. I use media queries still for basic whole-page layout. Though container queries can collapse with fit-content so js and css variables (`--unit`) can come in handy in tighter spots.

-2

u/TheRNGuy 8d ago edited 8d ago

Seems like over-engineering, but maybe it could be most useful in React component libraries, when you use same component in different places, but want to look it different, without refactoring it.

I probably wouldn't use it if those were components made specifically for one site.

Another use I see if you could drag-n-drop components in different places.

Also, it seems for looks only, but not behaviour or stuff like useEffect or events, though it would still simplify React code.

In most cases, media query would still be more useful. For many sites or apps, you won't even need container query.

(I didn't even know it's a thing, but googled after it)