html69// developer tools

How to Build CSS Grid Layouts Visually (and How grid-template Works)

CSS Grid Generator

CSS Grid is one of the best layout tools ever invented, right up until you are staring at grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) at 1 AM wondering why the third card is stretching. Two-dimensional layouts give you rows and columns at the same time. Guessing track sizes in a stylesheet still gets old fast.

The CSS Grid Generator lets you shape columns, rows, gap, and span areas in this tab, preview the layout, and copy CSS or Tailwind. We never see the CSS.

Build CSS Grid layouts visually

Dial columns, rows, and gaps. Drag across the preview to place a box that spans tracks. Copy a .grid rule, Tailwind grid-cols-* / gap-* utilities, or the matching HTML. Paste an existing display: grid block back onto the controls. Sample, Holy, Cards, Sidebar, and Feature are starting points. Caps are 12 columns, 12 rows, and 12 items.

Need one axis only? Use the CSS Flexbox Generator. Mixing utilities and custom CSS? Jump to the CSS to Tailwind Converter or the Tailwind to CSS Converter. Those tools also run on your computer.

How grid-template works

display: grid turns a container into rows and columns with gutters between them. Two properties do most of the work:

PropertyWhat it sets
grid-template-columnsVertical tracks, left to right.
grid-template-rowsHorizontal tracks, top to bottom.

A few building blocks make the templates click faster:

  • fr. A share of leftover space after fixed tracks and gaps. 1fr 2fr means the second column gets about twice the remaining space, not twice the content width.
  • repeat(). Saves you from typing 1fr 1fr 1fr 1fr. The generator writes repeat(n, minmax(0, 1fr)) so tracks can shrink below content size.
  • minmax(min, max). A track can shrink to min and grow to max. Pair it with repeat(auto-fit, minmax(240px, 1fr)) or auto-fill by hand when cards should wrap as the viewport changes. That auto-fit pattern is a manual recipe. The generator uses a fixed column count.
  • gap. Row and column gutters without negative margins on the outer edges. Split it with row-gap and column-gap when the two axes differ.

How to build a CSS Grid layout by hand

The generator is the fast path for equal tracks and spans. Hand-written grid-template still helps when you need auto-fit, named areas, or a DevTools overlay.

1. Classic responsive card grid

A card grid that adapts without a pile of media queries:

CSS

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.25rem;
}

On a narrow screen the cards stay at least 240px (or stack). On a wide screen, 1fr shares leftover space across the columns in each row.

2. Page shell with grid-template-areas

Named areas read like a mini map of the page: header, sidebar, main, footer.

CSS

.app-shell {
  display: grid;
  min-height: 100dvh;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 1rem;
}

.site-header { grid-area: header; }
.site-sidebar { grid-area: sidebar; }
.site-main { grid-area: main; }
.site-footer { grid-area: footer; }

@media (max-width: 768px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "main"
      "sidebar"
      "footer";
  }
}

On mobile you rearrange the whole structure in one place. You do not have to retarget every child's line numbers.

3. Span across columns or rows

A hero card or chart that should cover two columns:

CSS

.featured-card {
  grid-column: span 2;
}

Explicit lines work too. grid-column: 1 / -1 spans from the first line to the last. If a child asks for span 2 on a 2-column grid and only one column is left on the row, Grid usually kicks the item to the next row instead of squashing it. That is the classic empty gap on the right.

4. Turn on the grid overlay in DevTools

When a grid behaves oddly, inspect it:

  • Open DevTools (F12, or right-click and Inspect) and select the element with display: grid.
  • Click the grid badge next to display: grid in the Styles or Layout panel.
  • Track lines, line numbers, and gap areas light up over the page.

Grid vs Flexbox

Reach forWhen
CSS GridRows and columns together: pages, card grids, dashboards.
CSS FlexboxOne axis: navbars, button groups, tags, toolbars.

Sketch tracks in the CSS Grid Generator, or write grid-template yourself. The conversion stays on your computer. We never see the files.

All blog posts