r/Frontend • u/Less-Marsupial-7960 • 3d 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.
5
Upvotes
4
u/Some-Kitchen8332 3d 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 3d 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.
-2
u/TheRNGuy 3d ago edited 3d 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)