CSS Grid Generator

Set the number of columns and rows, the gaps and the track sizes (fr, px, auto, minmax) and watch the grid redraw. You get the CSS and matching HTML to paste.

CSS Grid Layout Guide: fr Units, minmax(), auto-fit and Grid vs Flexbox

Before CSS Grid, web layouts were built with floats, tables, inline-blocks and framework class names, all workarounds for the fact that CSS had no real layout system. Grid arrived in all major browsers in 2017 and changed that. It lets you define rows and columns directly in CSS and place content into them, which makes complex layouts shorter and more predictable.

CSS Grid page layout with header, sidebar, main content and footer areas

The minimum you need

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

That’s a three-column grid with 16px gaps. Every direct child becomes a grid item and flows into the next free cell, left to right, top to bottom. Rows are created automatically as needed.

The fr unit

fr means “fraction of the remaining free space”. 1fr 2fr gives the second column twice as much space as the first. Fixed sizes are taken out first: in 200px 1fr 1fr, the first column is 200px and the other two split what is left.

One subtlety: 1fr actually means minmax(auto, 1fr), so a column won’t shrink below the width of its content. A long URL or a wide code block can push a column wider than you expect. Writing minmax(0, 1fr) lets the column shrink fully and fixes many “why is my grid overflowing” problems.

Track sizing functions

ValueMeaningTypical use
200px, 12remFixed sizeSidebars, icons
1frShare of free spaceFlexible columns
autoSize to contentLabels, buttons
minmax(200px, 1fr)At least 200px, at most one shareResponsive columns
fit-content(300px)Size to content, up to 300pxText columns with a cap
repeat(4, 1fr)Repeat a patternEqual columns

Responsive grids without media queries

The most useful Grid pattern of all:

grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));

This creates as many columns as fit, each at least 240px wide, and stretches them to fill the row. On a phone you get one column; on a laptop, four. The tool cards on our homepage use this exact pattern.

auto-fit versus auto-fill

KeywordBehaviour when there are fewer items than columns
auto-fitEmpty tracks collapse, and existing items stretch to fill the row
auto-fillEmpty tracks stay, so items keep their minimum size and leave space at the end

With plenty of items, they behave the same. The difference shows with only two or three items on a wide screen: auto-fit makes them huge, auto-fill keeps them card-sized.

gap

gap (formerly grid-gap) sets the spacing between rows and columns. Unlike margins, it never adds space on the outer edges, and it doesn’t collapse or double up. You can set rows and columns separately with row-gap and column-gap. Use values from your spacing scale for consistency.

Placing items

Items can span multiple tracks:

.featured { grid-column: span 2; }
.sidebar { grid-column: 1 / 2; grid-row: 1 / 4; }

Line numbers start at 1 at the left edge; -1 refers to the last line, so grid-column: 1 / -1 spans the full width.

Named areas

For page layouts, grid-template-areas is wonderfully readable:

.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}
header { grid-area: header; }

Rearranging the layout for mobile is just a matter of redefining the areas in a media query.

Subgrid

Nested grids used to be independent of their parent, so cards in a row couldn’t align their titles and buttons with each other. grid-template-rows: subgrid lets a child grid reuse the parent’s tracks. Subgrid has been supported in all major browsers since 2023 and is ideal for card layouts where headings, text and buttons should line up across cards.

Grid versus Flexbox

QuestionGridFlexbox
DimensionsTwo (rows and columns together)One (a row or a column)
Layout driven byThe container’s tracksThe items’ content sizes
Best forPage layouts, card galleries, forms, dashboardsNavigation bars, button groups, centring, toolbars
Alignment across rowsYes, items line up in columnsNo, each line is independent
Overlapping itemsEasy (place in the same cell)Needs positioning

They aren’t competitors. A typical page uses Grid for the overall layout and card galleries, and Flexbox inside components. If you want the details of Flexbox alignment, read our Flexbox guide.

Where Grid layouts usually break

  • Forgetting that only direct children are grid items. Wrappers in between break the layout (or use display: contents with care, or subgrid).
  • Using 1fr with long unbreakable content. Switch to minmax(0, 1fr).
  • Recreating 12-column frameworks unnecessarily. Grid can express the layout directly. If you do want a classic column grid, our layout grid calculator gives exact column widths.
  • Setting fixed heights on rows, which cause overflow when content grows. Prefer auto or minmax(100px, auto).

Browser DevTools

Chrome, Firefox and Edge include Grid inspectors that overlay line numbers, track sizes and area names on the page. Firefox’s was the first and is still excellent. When a grid behaves unexpectedly, turn the overlay on before changing any code.