Chase McCoy wrote a nifty post about the “gap problem” when making a grid of items. His argument might be summarized like this: how should we space elements with margins in CSS? He notes that the gap
property isn’t quite ready for prime time when it comes to using it with flexbox, like this:
.grid {
display: flex;
gap: 10px;
}
Right now, using gap
with flexbox is only supported in Firefox and I’ve already caught myself forgetting about that in a few projects. So watch out for that.
Anyway, the part about Chase’s blog post that I love is where he mentions Andy Bell’s technique for creating a responsive layout with no media queries, like this:
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
This CSS is doing the following:
- Make a grid with a 10px gap between each column and row.
- Each column should have a minimum width (150px).
- Each column should also be equal width (1fr).
- The grid should auto-fill as many columns that can fit.
The nifty thing about all this is that our grid is now effectively responsive because of minmax
— if you resize the browser, then the grid will snap down into fewer columns, just like this:
No media queries at all! Although sure, there’s a few other ways that you could get this to work but I think this is neat not just because we’re avoiding media queries — instead, it’s because it teaches us to think in a new way when designing and building components.
Chase continues:
With this technique, instead of using breakpoints to specify the screen size where your items should stack, you specify the minimum size an element should be before it stacks. I like this because it encourages developers to think about responsive design in terms of behaviors instead of screen sizes.
“Behaviors instead of screen sizes” is such a great way to think about component design! A lot of the problems I’ve encountered when making components for a design system is when I’ve been thinking about screen sizes — mobile, tablet, desktop, etc. — and trying to make those components fit within those constraints.

Thinking in behaviors is always more effective because there are so many things that can impact a component beyond what screen or device width we’re working with. Perhaps we want that component to fit inside another component. Or we want to align some helper text to the side of it for comparison.
Either way, thinking about behaviors instead of screen sizes isn’t really going to be fully possible until we have container queries, as Chris writes:
Container queries are always on the top of the list of requested improvements to CSS. The general sentiment is that if we had container queries, we wouldn’t write as many global media queries based on page size. That’s because we’re actually trying to control a more scoped container, and the only reason we use media queries for that now is because it’s the best tool we have in CSS. I absolutely believe that.
It works well for small cells like here – 150px.
If I need bigger cells ~ 500px, for example, then you still need media queries. Otherwise, it will overflow on mobile (assuming < 500px)
See: https://css-tricks.com/intrinsically-responsive-css-grid-with-minmax-and-min/
But “minmax” is already used here – just change 150px to 500px in the same Pen, shrink the window and horizontal scroll will appear.
…yes, components should be fluid. That’s a decade-plus old best practice. Container queries are needed for encapsulating responsive design, but currently it should take place at the application composition layer. Except for page layout components. Even then, though, a media query should be used only when absolutely needed. Which it’s never been for an layout transformation like this. Floats with min width can do this, too. The need for media queries for something like this is usually because a designer wants the comfort of a more static and less fluid layout with a very specific number of columns at very specific screen sizes.
Nice one, I loved it. How would you modify the code to get a resizable grid of 150×150 pixel squares? Using repeat(auto-fill, minmax(150px, 1fr)); slightly resizes the grid elements to let them “fit” the windows’ width. We should add a grid-template-rows option too, I guess, right? Any idea? I couldn’t make it work in any way
You can have a look at this simple Pen, let me know what you think:
There might be potential for building flexible layout components using math functions and grid: https://blog.logrocket.com/flexible-layouts-without-media-queries
This is cool, but can create problems if only a few grid items. Let’s say your min-width is ~33vw, but there’s only two items: this causes the second item to float weirdly off to the right.
Demo: https://codepen.io/crstauf/pen/bGVwLZO
Well this is embarrassing. Turns out, using
auto-fill
instead ofauto-fit
resolves the problem.