All tools

CSS grid generator

Build CSS grid layouts visually and copy the CSS.

Grid

Columns
Rows

Item 1

Click an item in the preview to select it, then make it span more cells.

Common layouts

Result

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

About the CSS grid generator

CSS Grid lays out content in rows and columns at the same time. It's the simplest way to build page layouts, dashboards, image galleries and responsive card grids without extra wrapper elements or media queries.

This generator builds the grid visually: choose the columns and rows, the gaps and alignment, make a responsive auto-fit grid, and click any item to make it span several columns or rows. The CSS updates as you go.

How to use the CSS grid generator

  1. Pick a common layout, or choose a fixed number of columns or a responsive auto-fit/auto-fill grid.
  2. Set the rows, the column and row gaps, and how items align inside their cells.
  3. Click an item in the preview, then increase its column or row span.
  4. Click Copy CSS and add the .grid class to the parent element.

Examples

Responsive card grid with no media queries

Input

auto-fit · min 240px · gap 24px

Output

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

Three columns with a featured item

Input

3 columns of 1fr · gap 16px · item 1 spans 2 columns

Output

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

.grid > :nth-child(1) {
  grid-column: span 2;
}

Common errors and how to fix them

auto-fit and auto-fill look the same

They only differ when there is room for more columns than items. auto-fit stretches the existing items to fill the row; auto-fill keeps empty columns.

A 1fr column overflows with long content

1fr has a minimum of auto, so long words or wide code blocks push it wider. Use minmax(0, 1fr) instead.

grid-area names don't place items

Every row in grid-template-areas must have the same number of cells, and an area must form a rectangle. Use . for empty cells.

Good to know

  • 1fr means one share of the free space, so repeat(3, 1fr) makes three equal columns. minmax(0, 1fr) also stops long words or wide content from stretching a column.
  • repeat(auto-fit, minmax(160px, 1fr)) fits as many columns of at least 160px as the container allows, so the grid is responsive with no media queries. auto-fill keeps empty columns when there are few items; auto-fit stretches the items to fill the row.
  • With rows set to As many as needed, new rows are created automatically as items are added.
  • grid-column: span 2 makes an item two columns wide. A span larger than the number of columns is reduced to fit.
  • gap sets the space between cells only, not around the outside of the grid.

Frequently asked questions

What's the difference between auto-fit and auto-fill?

Both create as many columns as fit. When there aren't enough items to fill a row, auto-fill keeps the empty columns so items stay their minimum width, while auto-fit collapses them so the items stretch across the row.

Is CSS Grid supported in all browsers?

Yes. CSS Grid, gap for grid and repeat() work in all current browsers, including Safari, Chrome, Firefox and Edge.

Should I use grid or flexbox?

Grid for two-dimensional layouts where rows and columns line up, flexbox for a single row or column of items. They work well together: a grid for the page, flexbox inside each card.