CSS Grid Generator

Build a CSS grid visually: type grid-template-columns and rows (fr, px, %, minmax(), repeat(), auto-fill), set gaps and alignment, make items span several columns or rows on a live preview, then copy the generated CSS. Includes a quick reference of real column widths for common templates.

Start from a pattern
Live previewClick an item to set its span
Items · editing item 1
Generated CSS
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

Usage examples

A classic 12-column page grid

Pick the 12-column preset, set a gap, then give items a column span such as 8 and 4 for a main area and a sidebar, or 4 + 4 + 4 for three equal cards. The CSS uses grid-column: span N on each item.

A responsive card gallery without media queries

The 'Responsive cards' preset writes repeat(auto-fill, minmax(250px, 1fr)). The browser fits as many 250px-minimum columns as the width allows and stretches them to fill the row, so you get 1 column on a phone and 4 on a 1200px desktop.

Holy grail layout with header and footer

Use columns 200px 1fr 200px and rows auto 1fr auto, then give the first and last item a column span of 3. The header and footer stretch across the page while the middle row holds two sidebars and the content.

A dashboard with one featured tile

On a 4-column grid, select the first item and set both column span and row span to 2. It becomes a large tile and the remaining items flow around it automatically.

Fixed sidebar beside fluid content

The 'Sidebar + content' preset writes 250px 1fr: the first column stays 250px wide and the second takes whatever is left, which is simpler than calculating widths with calc().

Quick reference

Column widths in a 1200px container with a 24px gap (empty grid)

grid-template-columnsColumn widthsTotal incl. gaps
repeat(2, 1fr)588px ; 588px1200px
repeat(3, 1fr)384px ; 384px ; 384px1200px
repeat(4, 1fr)282px ; 282px ; 282px ; 282px1200px
repeat(12, 1fr)78px ; 78px ; 78px ; 78px ; 78px ; 78px ; 78px ; 78px ; 78px ; 78px ; 78px ; 78px1200px
1fr 2fr 1fr288px ; 576px ; 288px1200px
2fr 1fr784px ; 392px1200px
250px 1fr250px ; 926px1200px
200px 1fr 200px200px ; 752px ; 200px1200px
minmax(300px, 1fr) 3fr300px ; 876px1200px
0.5fr 0.25fr588px ; 294px906px
25% 75%300px ; 900px1224px
repeat(auto-fill, minmax(250px, 1fr))282px ; 282px ; 282px ; 282px1200px

How many columns repeat(auto-fill, minmax(250px, 1fr)) creates at common widths (24px gap)

Container widthColumnsEach column
320px1320px
375px1375px
768px2372px
1024px3325.33px
1280px4302px
1440px5268.8px
1920px7253.71px

Frequently asked questions

What does the fr unit mean?

fr is a fraction of the free space left after fixed-size tracks and gaps are subtracted. In a 1200px container with 24px gaps, 1fr 2fr 1fr leaves 1152px to share, giving columns of 288, 576 and 288px. If the fr values add up to less than 1, only that fraction of the free space is used, which is why 0.5fr 0.25fr does not fill the row.

Why does 25% 75% overflow when I add a gap?

Percentage tracks are measured against the container, and gaps are added on top. 25% + 75% of 1200px is already 1200px, so a 24px gap makes the grid 1224px wide and it overflows. Use 1fr 3fr instead: fr tracks are calculated after gaps are subtracted, so they always fit.

What is the difference between auto-fill and auto-fit?

Both create as many columns as fit. auto-fill keeps the empty columns when there are fewer items than columns, so items keep their column width. auto-fit collapses empty columns to zero, so the existing items stretch across the whole row. With enough items to fill the row, the two behave the same.

How many columns will repeat(auto-fill, minmax(250px, 1fr)) make?

The browser repeats the track as many times as fit without overflowing, using the 250px minimum: columns = floor((container width + gap) ÷ (250 + gap)), and at least 1. The 1fr maximum then shares the leftover space. At 1200px with a 24px gap that is 4 columns of 282px; at 768px it is 2 columns of 372px.

Why use minmax(0, 1fr) instead of 1fr?

1fr is really minmax(auto, 1fr), and the auto minimum means a column can't get narrower than its content's minimum width. A long URL or a wide image can therefore make one '1fr' column wider than the others. minmax(0, 1fr) removes that minimum so the columns stay truly equal.

What happens if a span is larger than the number of columns?

The grid adds implicit columns to fit the item, sized by grid-auto-columns (auto by default), which usually breaks the layout. Keep spans at or below your column count, or use grid-column: 1 / -1 to span the full explicit grid whatever its size.

Should I use CSS Grid or Flexbox?

Grid controls rows and columns at the same time, so it suits page layouts, galleries and anything aligned in two dimensions. Flexbox lays items out along one axis and suits toolbars, navigation and centering. They combine well, and both are supported in all current browsers; CSS Grid has been available since 2017 (Chrome 57, Firefox 52, Safari 10.1).