CSS Grid Generator

Choose a Template

Customize Grid

Grid Columns

Examples: 1fr, 200px, 50%, 2rem, minmax(100px, 1fr), auto

Grid Rows

Examples: auto, 100px, 1fr, min-content, max-content

Grid Gap

Examples: 10px, 1rem, 2%, 0

Grid Areas

Enter area names in the grid cells. Use "." (period) for empty cells. Area names must be unique and valid CSS identifiers.

Color Scheme

Adds media queries for mobile, tablet, and desktop layouts

Grid View shows the structure of your grid with colored areas.

Grid Preview

Preview of your grid layout with named areas.

The Complete CSS Grid Guide: From Basics to Advanced Layouts

Last updated: April 19, 2025

This comprehensive guide will teach you everything you need to know about CSS Grid Layout, from basic concepts to advanced techniques. Use our interactive Grid Generator tool above to experiment with the concepts as you learn!

Table of Contents

Introduction to CSS Grid

CSS Grid Layout (also known as "Grid") is a two-dimensional layout system designed specifically for the web. It allows you to organize content into rows and columns and has many features that make building complex layouts straightforward.

Before CSS Grid, web developers relied on various layout methods like tables, floats, positioning, and inline-block, each with significant limitations. CSS Grid was created to solve these problems by providing a native CSS solution for creating grid-based layouts.

Unlike Flexbox, which is one-dimensional and deals with either rows OR columns, Grid is two-dimensional and can handle both rows AND columns simultaneously, making it perfect for page layouts and complex interface components.

Why Use CSS Grid?

  • Create complex layouts with minimal HTML
  • Control both rows and columns simultaneously
  • Design layouts that adapt to different screen sizes
  • Precisely control the placement of elements
  • Simplify responsive design implementation
  • Reduce the need for media queries in many cases

Key Grid Concepts

Before diving into the code, it's important to understand the fundamental concepts of CSS Grid:

Grid Container

The element on which display: grid is applied. It's the direct parent of all the grid items.

Grid Item

The direct children of the grid container.

Grid Line

The dividing lines that make up the structure of the grid. They can be vertical ("column grid lines") or horizontal ("row grid lines").

Grid Track

The space between two adjacent grid lines. You can think of them as the columns or rows of the grid.

Grid Cell

The space between two adjacent row and two adjacent column grid lines. It's a single "unit" of the grid.

Grid Area

The total space surrounded by four grid lines. A grid area may be composed of any number of grid cells.

Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
A 3×3 grid with 9 cells

The Grid Container

To create a grid container, you set the display property of an element to grid or inline-grid.

.container {
  display: grid;
}

Once you've established a grid container, you can set up the columns and rows using the grid-template-columns and grid-template-rows properties.

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

This creates a grid with:

  • Three columns: the first is 200px wide, and the other two share the remaining space equally (1fr each)
  • Three rows: the first and last adjust to their content (auto), and the middle one takes up the remaining space (1fr)
  • A 10px gap between all rows and columns

Grid Items

Grid items are the direct children of a grid container. By default, each item takes up exactly one grid cell, but you can make items span multiple rows and/or columns.

.item {
  grid-column: 1 / 3; /* Start at line 1, end at line 3 */
  grid-row: 2 / 4;    /* Start at line 2, end at line 4 */
}

Grid Areas

Grid areas are a powerful feature that allows you to name regions of your grid and place items in those regions. This makes your CSS more readable and maintainable.

.container {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header header"
    "sidebar content content"
    "footer footer footer";
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer { grid-area: footer; }
Header
Sidebar
Content
Footer
Visual representation of the grid areas defined above

Responsive Grid Layouts

CSS Grid makes it easy to create responsive layouts that adapt to different screen sizes. Here are some techniques:

Using minmax() for Responsive Columns

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

Redefining Grid Areas with Media Queries

.container {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "header"
    "nav"
    "content"
    "sidebar"
    "footer";
}

@media (min-width: 768px) {
  .container {
    grid-template-columns: 200px 1fr;
    grid-template-areas:
      "header header"
      "nav nav"
      "sidebar content"
      "footer footer";
  }
}

Common Grid Patterns

Here are some common layout patterns you can create with CSS Grid:

Holy Grail Layout

.holy-grail {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header header"
    "left-sidebar content right-sidebar"
    "footer footer footer";
  min-height: 100vh;
}

Card Grid

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

Additional Resources

Here are some excellent resources to learn more about CSS Grid:

Ready to Create Your Own Grid Layout?

Now that you understand CSS Grid, try our interactive Grid Generator tool at the top of this page to create your own custom layouts!

Last updated: April 19, 2025

If you found this guide helpful, please share it with others who might benefit from learning about CSS Grid!

Frequently Asked Questions

What is CSS Grid?

CSS Grid is a powerful layout system that allows you to create two-dimensional grid-based layouts. It provides a way to divide a webpage into rows and columns, making it easier to design complex responsive layouts.

When should I use CSS Grid?

Use CSS Grid when you need to create complex two-dimensional layouts. It's perfect for overall page layouts, image galleries, card layouts, and any design that requires precise control over both rows and columns.

What's the difference between Grid and Flexbox?

Grid is designed for two-dimensional layouts (rows and columns), while Flexbox is designed for one-dimensional layouts (either rows OR columns). They can be used together, with Grid handling the overall layout and Flexbox managing content within grid items.

How can I make my grid responsive?

You can make your grid responsive using CSS functions like minmax(), auto-fit, and auto-fill, combined with media queries. Our grid generator helps you create responsive layouts with visual controls for these properties.