CSS Grid Generator

Set the tracks of a grid, draw the areas of your layout directly on the preview and copy the CSS with grid-template-areas or line numbers, plus the HTML.

Loading tool…

About the CSS Grid Generator

A grid is defined by its tracks: grid-template-columns and grid-template-rows each take a list of sizes. A length or a percentage is fixed, auto fits the content, and the fr unit shares the space that is left after the other tracks are sized. minmax(min, max) sets a range, and minmax(0, 1fr) is the usual fix for a column that is pushed wider than its share by long content, because a plain 1fr is really minmax(auto, 1fr). Repeated tracks are written with repeat(4, 1fr).

Items are placed on the numbered lines between the tracks. grid-area: 2 / 1 / 3 / 4 reads row-start / column-start / row-end / column-end, so that item sits in the second row and spans columns 1 to 3: the end lines are exclusive. The same layout can be written as a picture with grid-template-areas, where each string is a row, each word is a cell, and a dot leaves a cell empty. Named areas must be rectangles and cannot overlap, and every row must have the same number of cells.

justify-items and align-items position each item inside its area, horizontally and vertically; the default stretch makes the item fill it. justify-content and align-content move the tracks inside the container when they do not fill it, which only happens with fixed or content-sized tracks. gap is written as row gap followed by column gap. For responsive card grids without media queries, use repeat(auto-fit, minmax(220px, 1fr)) as the column template.

How to use it

  1. Set the number of columns and rows, then edit the size of each track.
  2. Adjust the gaps and the alignment of items and tracks.
  3. Drag over empty cells in the preview to draw an area; on a touch screen, tap the first and the last cell.
  4. Name the areas to get grid-template-areas, or leave them unnamed for line-based placement.
  5. Copy the CSS and the HTML.

Frequently asked questions

What does 1fr mean?
One fraction of the free space of the grid. After fixed and content-sized tracks and the gaps are subtracted, the rest is divided between the fr tracks in proportion to their numbers: with 1fr 2fr the second column gets twice the share of the first.
grid-template-areas or line numbers?
Both produce the same layout. Named areas are easier to read and to rearrange in a media query, since only the container changes. Line numbers are needed for overlapping items and are shorter for simple spans such as grid-column: 1 / -1.
Why does my grid overflow its container?
A 1fr track cannot become smaller than the minimum content of its items, such as a long word, a wide image or a pre block. Write minmax(0, 1fr) instead of 1fr, or set min-width: 0 on the item, so that the track can shrink.
Do I need to place every item?
No. Items without a placement flow into the free cells in source order, row by row. It is common to place only the few items that span several tracks and leave the others to auto-placement.

Related tools