What is a CSS grid?

CSS grid layout places the children of an element in rows and columns. You define the tracks on the container with grid-template-columns and grid-template-rows, and the space between them with gap; every child then takes the next free cell. It's the usual way to build card grids, photo galleries, dashboards and page layouts with a sidebar.

How to use the generator

  1. Pick the number of columns and rows with the sliders or by typing.
  2. Set the column gap and row gap in pixels.
  3. Give each column and row a size. Every new track starts at 1fr, an equal share of the free space.
  4. Watch the preview, then copy the CSS and the HTML, or download them as files.

The preview box has a fixed height so that rows sized in fr fill it the way they would in a container with a set height.

Sizes you can use

Each field takes one track size, the same values CSS accepts:

  • fr units for shares of the free space, like 1fr or 2fr
  • lengths and percentages, like 200px, 12rem or 25%
  • auto, min-content and max-content
  • minmax(150px, 1fr) for a track that never gets narrower than 150px
  • fit-content(300px)

A single column can also hold a whole responsive pattern, such as repeat(auto-fill, minmax(150px, 1fr)), which fits as many columns as there's room for.

Your browser checks every size. A size it doesn't accept is marked red and 1fr takes its place in the code until you fix it.

The code

When all columns have the same size, the CSS uses repeat(), like repeat(3, 1fr); different sizes are listed in order, like 200px 1fr 1fr. Equal gaps become a single gap value, and different ones are written as the row gap followed by the column gap. The HTML is a container with one numbered child per cell.

Examples

  • Three equal columns: grid-template-columns: repeat(3, 1fr);
  • A sidebar and a main area: column sizes 250px and 1fr
  • A header, content and footer: row sizes auto, 1fr and auto
  • A responsive card grid: one column sized repeat(auto-fill, minmax(200px, 1fr))

Features

  • Columns, rows and gaps with sliders
  • A size for every column and row, checked by your browser
  • Live preview with numbered cells
  • CSS and HTML to copy or download
  • Runs in your browser