CSSeasyCSS

Responsive Auto-Fit Grid

Create a responsive grid that automatically adjusts columns based on available space.

01

The problem

You need a grid layout that automatically adjusts the number of columns based on screen size without media queries.

02

The solution

CSS
.auto-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
  padding: 20px;
}

/* Alternative with auto-fill for empty tracks */
.auto-grid-fill {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

03

See it live

Isolated previewSandboxed
04

Parameters

auto-fitkeyword

Stretches items to fill available space

auto-fillkeyword

Creates empty tracks if space allows

minmax()function

Sets minimum and maximum column width

05

Put it to work

Example
<div class="auto-grid">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
  <div class="card">Card 3</div>
  <div class="card">Card 4</div>
</div>

Worth knowing

auto-fit collapses empty tracks while auto-fill keeps them. Use auto-fit for most cases.