Creating Responsive Layouts with CSS Grid Without Media Queries

With CSS Grid, I can build resilient layouts without giving elements fixed sizes. Instead, I let them stretch within set limits and fill the remaining space.

CSS Grid changed how I think about layouts and how I build them. I no longer need separate code for specific resolutions, and I can sometimes skip Media Queries altogether (just for responsive grids, of course).

Like many others, I use this approach whenever I can to build CSS Grid-based layouts without Media Queries. Here’s a live example:

  • Grid item 1 in a responsive CSS Grid without media queries
  • Grid item 2 in a responsive CSS Grid without media queries
  • Grid item 3 in a responsive CSS Grid without media queries
  • Grid item 4 in a responsive CSS Grid without media queries

Try resizing the browser window and watch the columns rearrange. On a narrow phone screen, the items stack into a single column.

This is a responsive grid without any media queries and it works in every viewport thanks to CSS Grid. Cool, isn’t it?

How does it work?

CSS Grid is a good fit for two-dimensional layouts. For a one-dimensional layout like the one above, I’d usually use Flexbox with flex-wrap so the items can wrap onto a new line. I chose CSS Grid here for its minmax function.

With that function, I can build a grid that adapts to different resolutions without writing Media Queries for each one. Here’s the CSS I used for the example above:

.container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(192px, 100%), 1fr));
  gap: 15px;
}

And this is the HTML for the same example:

<ul class="container">
    <li><img src="image.jpg" /></li>
    <li><img src="image.jpg" /></li>
    <li><img src="image.jpg" /></li>
</ul>

The CSS above lays out the elements in a grid. Each column has a minimum width of 192px, and its maximum width uses Fractional Units (1fr). That gives each column an equal share of the space left in the container.

When the element’s width reaches 192px, it won’t shrink beyond that. Instead, the row will break, and the element will stretch to the maximum width available (considering 1fr).

So what does the keyword auto-fill do? Here’s the short version.

Using the auto-fill Keyword

When I create a grid inside an element, I can use the CSS repeat() function to say how many times to repeat a track definition. For example, the code below creates a grid with 10 elements, each 100px wide:

.container {
   grid-template-columns: repeat(10, 100px);
}

I can also use fr units to create 10 flexible elements of equal width:

.container {
   grid-template-columns: repeat(10, 1fr);
}

But this code always gives me 10 elements. It doesn’t let me fit as many fixed-width elements as a viewport or container has room for.

What I want is a grid that fits as many fixed-width elements as the container can hold. That’s what auto-fill lets me do.

To fit as many elements as possible in the container, each with a width of 100px, I use auto-fill instead of a specific number. Here’s what that looks like:

.container {
   grid-template-columns: repeat(auto-fill, 100px);
}

You might also see auto-fit. It works much like auto-fill, but auto-fit collapses empty tracks, so the existing items stretch to fill the whole row. Use auto-fill if you want to keep the space for empty tracks, and auto-fit if you want the items to fill the available width.

Preventing Overflow on Small Screens

There’s a reason I used the min(192px, 100%) wrapper in the first code example. The classic version of this pattern uses a plain minmax(192px, 1fr), but it has one weak spot.

If the container is narrower than 192px, the column can’t shrink below its minimum. The item spills out of the container and causes horizontal scrolling.

On a full-width phone layout, a 192px minimum rarely causes trouble. Overflow does show up, though, when the grid sits in a narrow sidebar or card, or when you choose a larger minimum. A 350px minimum, for example, is already too wide for many phones.

/* Overflows when the container is narrower than 192px */
grid-template-columns: repeat(auto-fill, minmax(192px, 1fr));

/* Never wider than the container */
grid-template-columns: repeat(auto-fill, minmax(min(192px, 100%), 1fr));

The min() function returns the smaller of the two values. In a wide container, that’s 192px, so nothing changes. In a container narrower than that, it returns 100% and the single column fits exactly.

I use this version by default now. It costs nothing on wide screens and fixes the one case where the “no media queries” grid breaks. The min() function has around 97% global browser support, according to Can I use.

Browser Support

All major browsers have supported CSS Grid since 2017. Global support is about 97%, according to Can I use. You can use it in production without a fallback.

If a newer CSS feature doesn’t have full support yet, you can test for it with an @supports feature query and serve a simpler layout to older browsers.

FAQs

Here are some common questions about responsive CSS Grid layouts:

What is the difference between auto-fill and auto-fit?
auto-fill creates as many tracks as will fit and keeps empty tracks, preserving their space. auto-fit does the same but collapses empty tracks to zero, so existing items stretch to fill the row. The difference is only visible when there are fewer items than available tracks.
Why does my auto-fill grid overflow on mobile?
With minmax(350px, 1fr), a column can never be narrower than 350px, so it overflows any container smaller than that, including many phone screens. Wrap the minimum in min(): minmax(min(350px, 100%), 1fr). The column then shrinks to the container's width when there's no room for 350px.
Can I control the number of columns at different breakpoints without Media Queries?
Yes, that is exactly what repeat(auto-fill, minmax(min(MIN, 100%), 1fr)) does. By changing the minimum value in minmax(), you control when columns break to a new row. A smaller minimum means more columns at narrow widths, while a larger minimum results in fewer columns.
Does this technique work for both images and text content?
Yes. The repeat(auto-fill, minmax()) pattern works with any type of content - images, cards, text blocks, or any HTML element. The grid items will adapt their size based on the minimum and maximum values you set.
Should I use grid-gap or gap?
Both work. The grid-gap property was the original name, but it has been replaced by the shorthand gap, which also works with Flexbox. Modern browsers support both, but gap is the recommended property going forward.
Do I still need Media Queries at all if I use this technique?
For the grid layout itself, no. The repeat(auto-fill, minmax()) pattern handles responsiveness automatically. However, you may still need Media Queries for other things like font sizes, spacing, or hiding/showing elements at certain breakpoints.

In Conclusion

In short, one line of CSS Grid gives you a layout that adapts to any width. I’m sure you’re seeing CSS Grid on more and more websites. As Frontend developers, you need to know how it works.

I hope you picked up something new. If you’d like a closer look, my full guide to the fr unit explains why that unit is at the heart of these flexible grids.

For even more control over how components respond to their available space, take a look at CSS Container Queries – they let each component adapt its layout based on its container’s width, not the viewport. And when you do need a breakpoint for fonts or spacing, the media queries range syntax makes it much easier to write.

Summarize with AI Claude ChatGPT Google AI
Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!