r/webdevelopment • Human Verified • 15d ago

Discussion Are CSS Container Queries making responsive design easier for reusable components?

I have been looking more closely at CSS container queries recently, and I think they solve a problem we often face when building reusable components.

For example, imagine creating a product card that is used in three places:

  • A full-width section on the homepage.

  • A narrow sidebar.

  • A dashboard widget.

With traditional media queries, we usually adjust the layout based on the browser window size. But the same component can have very different available space depending on where it is placed.

Container queries approach this differently. They allow the component to respond to the width of its parent container.

A simple example:

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

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

This makes the component more independent of the overall page layout.

I still think media queries are useful for things like navigation and overall page structure. But for reusable components, container queries seem like a much cleaner approach.

One thing I am interested in is how developers are using them in real projects.

Are you using container queries in production or do you still prefer traditional media queries for most layouts?

7 Upvotes

9 comments sorted by

2

u/BoBoBearDev 14d ago edited 14d ago

Yes, that's why container query is created because media query is incapable of doing the same. Media Query responds to browser size, but when you have a component that is displayed inside different layout, it all falls apart. Meaning, controls using media query is stuck with certain fixed layout, which is lacking.

Just to be clear, the UX is often designed by some designers who doesn't understand this. Back in the days, they don't even care and everything is fixed size. For example, aside from IE6, all the browser did the padding wrong (lack of box-sizing: border-box), all of them cannot use percentages based layout. Because the padding would break the calculation. But the entire industry blamed IE6 doing padding correctly and calling IE6 not following the standard.

Meaning, trying to convince your UX team to use Container Query will be a uphill battle.

2

u/eurosat7 12d ago

caniuse.com told me: in 2023 @container became baseline.

Oh, really?

1

u/lucid1014 14d ago

I’ll be honest I’m very slow to adopt new css. Everytime I read Claude output these days I learn new css features lol. That’s why ai will easily replace me. That is a cool idea… I guess I just would traditionally use scoped css for that issue. So .dashboard .card would have different props than .sidebar .card

1

u/Agreeable-Yogurt-487 14d ago

And that's how you end up with an unmaintainable mess.. a million scoped classes doing more or less the same..

1

u/tdic89 13d ago

Only if you let it run rampant. Best thing to do with any AI development is set up some rules and best practices before you even begin making it code anything.

1

u/Less-Marsupial-7960 Human Verified 8d ago

I agree with this. AI-generated code becomes much easier to maintain when the project already has clear conventions, reusable component patterns and styling rules.

I think the important part is making the AI follow the existing architecture instead of allowing it to introduce a different approach every time it creates a component.

1

u/Additional-Sky8447 10d ago

It really shines when combined with css grid. Using auto-fit and minmax inside a container q means the card just figures its own layout automatically, no extra breakpoints needed

0

u/Affectionate_Tea9218 14d ago

Geeze! Interesting. I’m still stuck at grid: that was the solution. Heh.

I love making my infographics and flow charts in css. And this is a new thing for me… yes: it’s awesome, expected results (not when I’m at the keyboard to). I’ll put css in-line sometimes because it’s just muscle memory. So me using new stuff..? I do love some good front end tho.