r/Frontend • u/Less-Marsupial-7960 • 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
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
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
useEffector 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)