Skip to content
en

CSS Grid generator

Builds a ready display: grid block – columns, rows and gaps. Shows how many cells you get and how many columns the container fits with auto-fit.

Input

Result

Fill in the fields – the result will appear here.

Report a problem
This will be sent with your message:
Advertisement

How CSS Grid works

Three lines where it all starts

display: grid turns on the grid, grid-template-columns describes the columns, grid-template-rows the rows. Then the items inside fill the cells on their own, in turn, left to right. Nothing extra needs to be set on the children.

1fr is a share, not a width

1fr means "one share of the free space". Three columns of 1fr split the container into three equal parts, and the gaps are taken out of that space first – that's why the grid doesn't overflow the edge, unlike a layout in percent.

repeat() only shortens the line

repeat(3, 1fr) is the same as 1fr 1fr 1fr. It becomes useful when there are twelve columns or when a keyword stands in place of the number.

auto-fit and auto-fill

Both tell the browser: "count the columns yourself, to fit the container width". Together with minmax(240px, 1fr) this gives a responsive grid with no media query at all: a column is never narrower than 240 pixels, and the width decides how many fit.

There is one difference. When there aren't enough columns to fill a row, auto-fit stretches the existing ones over all the space, while auto-fill leaves empty tracks and the columns stay narrow. For cards people usually pick auto-fit.

gap is read from the rows

gap: 16px 24px means 16 between rows and 24 between columns, in that order. If the numbers are equal, write one. There is never a gap at the grid's outer edges.

Common questions about CSS Grid

Grid or Flexbox?

Grid – when you describe the layout in two directions at once: rows and columns. Flexbox – when items sit in one row or one column and share the space themselves. A card gallery is grid, a button bar is flex.

How many rows do I need to describe?

Usually none. If there are more items than cells, the browser adds rows itself. grid-template-rows is needed when rows must have a set height.

Why do columns overflow the container?

The most common cause is long text or an image that won't shrink. 1fr means "at least the content's size", so wide content pushes the column out. The fix is minmax(0, 1fr) instead of 1fr.

Do I need media queries?

For a card grid – no. repeat(auto-fit, minmax(240px, 1fr)) already rearranges the columns for any width. A media query is still useful when mobile needs a different layout, not just fewer columns.

How do I make one card span two columns?

Add grid-column: span 2 to it. The grid expects this, and the other cards rearrange themselves. This page builds the container; rules for single cells are added by hand.

Advertisement

Report a problem

This will be sent with your message: