What Is CSS Grid Layout?

CSS Grid Layout is a two-dimensional layout system for the web. Unlike Flexbox, which is one-dimensional (either a row or a column), Grid lets you control both rows and columns simultaneously. It was designed to solve the complex layout challenges that float-based and inline-block layouts could never handle gracefully.

With CSS Grid, you define a grid container, specify rows and columns, and then place items into that grid. The result is clean, maintainable, and highly responsive layouts with far less markup than older techniques.

Grid Container & Grid Items

Every CSS Grid layout starts with a grid container. Set display: grid or display: inline-grid on a parent element, and all its direct children become grid items.

.container {
  display: grid;
  grid-template-columns: 200px 200px 200px;
  grid-template-rows: auto;
  gap: 16px;
}

This creates three fixed-width columns of 200px each with a 16px gap between cells. Simple, but not yet flexible. Let's make it better.

The fr Unit and repeat()

The fr unit distributes available space proportionally, like flex-grow in Flexbox. Combined with repeat(), you can create flexible grids with very little code.

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

This creates three equal-width columns. Change to repeat(4, 1fr) for four columns, and everything scales automatically. No media queries needed for simple responsive behavior.

auto-fit, auto-fill & minmax()

This is where CSS Grid truly shines for responsive design. The auto-fit keyword combined with minmax() creates layouts that adapt to any screen width without a single media query.

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 24px;
}
.item {
  background: #f0f4ff;
  padding: 20px;
  border-radius: 8px;
}

Each grid item will be at least 250px wide, but they stretch to fill the container when there's extra space. As the viewport shrinks, items wrap into fewer columns automatically. This single pattern replaced hundreds of lines of media queries in many codebases.

Named Grid Areas

For page-level layouts, named grid areas make your CSS incredibly readable. Define areas with grid-template-areas and assign items with grid-area.

.page {
  display: grid;
  grid-template-columns: 250px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header  header  header"
    "sidebar content aside"
    "footer  footer  footer";
  gap: 20px;
  min-height: 100vh;
}
.header  { grid-area: header;  }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.aside   { grid-area: aside;   }
.footer  { grid-area: footer;  }

This creates a classic three-column page layout with header and footer. Want a different layout on mobile? Just redefine grid-template-areas:

@media (max-width: 768px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "content"
      "sidebar"
      "aside"
      "footer";
  }
}

Alignment and Justification

CSS Grid includes powerful alignment properties. Use justify-items, align-items, justify-content, and align-content on the container. Use justify-self and align-self on individual items.

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(2, 100px);
  justify-content: center;  /* horizontally center the grid */
  align-content: center;    /* vertically center the grid */
  align-items: stretch;     /* stretch items to fill cells */
  gap: 12px;
}
.item.center-me {
  justify-self: center;
  align-self: center;
}

Practical Tips

Try It Yourself

Writing CSS Grid by hand is great, but experimenting visually is faster. Use our free visual CSS Grid generator to build and preview your grid layouts in real time. Set columns, rows, gaps, and alignment visually, then export the code.

Our visual CSS Grid generator supports all the features covered here — named areas, minmax, auto-fit, custom column counts, and more. It's completely free and requires no account.

Conclusion

CSS Grid is the most powerful layout system CSS has ever had. With just a few lines of code you can create layouts that used to require complex frameworks and brittle hacks. Learn the fundamentals, practice with real projects, and use interactive tools to speed up your workflow. Start building your next layout with our free visual CSS Grid generator today!