Course contentsShow
← Web Development
Lesson 368 of 2,9826. CSS GridFree lesson

Responsive Grid Without Media Queries

Leverage auto-fit, auto-fill, and minmax to create grids that adapt without breakpoints.

Responsive Grid Without Media Queries

You'll learn how to build grids that automatically adapt to screen size without writing a single media query.

The Core Idea

Until now, responsive layouts typically required media queries to adjust at different breakpoints. But CSS Grid has a powerful combination that makes grids automatically responsive: repeat(), auto-fit (or auto-fill), and minmax() work together like a smart packing algorithm that rearranges items based on available space.

Think of it like flexible storage bins: you tell the grid "make each item at least this wide, but let it grow if there's room, and fit as many as you can per row." The grid handles the rest automatically.

How It Works

The magic formula looks like this:

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1rem;
}

Let's break it down:

  • repeat() creates multiple columns
  • auto-fit creates as many tracks as will fit in the available space, collapsing empty ones
  • minmax(250px, 1fr) sets each column to be at least 250px wide but allows it to grow equally to fill space

As the viewport shrinks, fewer columns fit, so items automatically reflow to new rows. On wide screens, more items sit side-by-side. No breakpoints needed!

You can swap auto-fit with auto-fill if you want empty tracks to remain (less common for this pattern).

/* Alternative with auto-fill */
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));

Key Takeaway: Combine repeat(), auto-fit, and minmax() to create intrinsically responsive grids that adapt to any screen width without media queries—the grid itself becomes intelligent.