CSS Grid Cheatsheet: Interactive Visual Guide

October 15, 2026  |  Cheatsheet  |  CSS

By Syed · Frontend Engineer & Creator of UITweak

Grid is the layout tool for two dimensions: rows and columns at the same time. Click through the values below, watch the tiles rearrange, and copy the code when it looks right.

Prefer building over reading? Open the Grid Builder →

grid-template-columns

grid-template-columns declares the explicit column tracks, and the browser resolves fixed sizes and fr shares before placing anything: each 1fr gets one share of whatever space remains after fixed tracks and gaps are subtracted. Items then flow into the tracks row by row through auto-placement, each one claiming the next free cell. Because track sizing happens first, adding more items never changes column widths, which is what keeps grid layouts stable as content grows.

1
2
3
4
5
6
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

/* Tailwind: grid grid-cols-3 */

auto-fit: grids that fill any container

repeat(auto-fit, minmax(140px, 1fr)) asks the browser to fit as many 140px-minimum tracks as the container allows, then collapse any empty tracks so the filled ones stretch to full width. The layout recalculates on every resize, which is why the grid reflows with no media queries. This only collapses tracks that end up completely empty, so a final row with fewer items still stretches those items across the full width.

As many 140px columns as fit, no media queries needed. Resize your browser to watch it adapt.

1
2
3
4
5
6
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
}

/* Tailwind arbitrary: grid grid-cols-[repeat(auto-fit,minmax(140px,1fr))] */

gap

Grid gap sets the gutters between tracks, and the browser subtracts the total gutter width from the container before dividing the remainder among fr tracks. Gutters are fixed and never flex, so a 16px gap stays 16px whether the columns are 100px or 400px wide. row-gap and column-gap split the two axes when rows and columns need different rhythm.

1
2
3
4
5
6
.container {
  display: grid;
  gap: 16px;
}

/* Tailwind: grid gap-[16px] */

place-items

place-items is shorthand for align-items plus justify-items, aligning every item inside its own grid area on both axes at once. The default stretch grows each item to fill its cell, but only on axes where the item has no definite size. Setting place-items: center shrinks every item to its content size and centers it in its cell, which is the fastest way to center a batch of icons or thumbnails.

1
2
3
.container {
  display: grid;
  place-items: center; /* align + justify together */
}

/* Tailwind: grid place-items-center */

grid-column: span

grid-column: span 2 tells an item to cover two column tracks instead of one, and the browser widens the item across both tracks plus the gutter between them. Auto-placement keeps laying later items into the next free cells, so a wide item can leave an awkward hole in the row above it. Adding grid-auto-flow: dense lets smaller items backfill those holes, at the cost of visual order no longer matching DOM order.

Box 1 spans multiple columns:

1
2
3
4
.featured {
  grid-column: span 2; /* covers 2 columns */
}

/* Tailwind: col-span-2 (row-span-2 for rows) */

grid-template-areas

grid-template-areas draws the layout as a text map where each quoted string is a row and each word is one column cell, so the browser builds tracks directly from the map's shape. Repeating a name across adjacent cells merges them into one region, and every region must form a solid rectangle, which the browser enforces by ignoring invalid maps. Items claim regions with grid-area, so rearranging the page means editing the map text instead of renumbering spans.

header
nav
main
ads
footer
.layout {
  display: grid;
  grid-template-areas:
    "header header header"
    "nav    main   ads"
    "footer footer footer";
}
.header { grid-area: header; }
.main   { grid-area: main; }

grid-auto-rows

Rows the browser creates on its own (implicit rows) are sized by grid-auto-rows; without it they default to auto and size to their tallest content, which breaks mosaic layouts when one tile has more text. Setting grid-auto-rows: 200px forces every implicit row to the same height, while minmax(150px, auto) keeps rows uniform but lets them grow for overflowing content. Explicit rows from grid-template-rows are unaffected, so the two properties combine cleanly.

1
2
3
4
5
6
.container {
  display: grid;
  grid-auto-rows: 120px;
}

/* Tailwind arbitrary: auto-rows-[120px] */

Common Production Bugs & Fixes

Bug 1: A `1fr` column blows out because of long content

What goes wrong: a grid column set to 1fr stretches wider than its share and overflows the container when a child holds a long word, a wide image, or a white-space: nowrap label. Why: fr tracks have an automatic minimum of min-content, the same as minmax(auto, 1fr). The browser refuses to size the track below the content's minimum size, so the track grows past its fair share and breaks the layout. Fix: clamp the minimum to zero and let the item itself handle overflow:

.layout {
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr); /* 0 minimum, not auto */
}
.layout .main {
  min-width: 0;      /* items need this too, same rule as flexbox */
  overflow: hidden;  /* or handle text with ellipsis */
}

Bug 2: `auto-fill` leaves empty ghost columns when there are few items

What goes wrong: a card grid with only two items shows them squeezed to the left with a big empty area on the right, even though the minmax() sizing looked right. Why: repeat(auto-fill, minmax(200px, 1fr)) keeps creating tracks for the whole row whether or not items fill them, so the empty tracks hold their share of space and the real items stay narrow. auto-fit instead collapses the empty tracks to zero, handing their space back to the filled tracks. Fix: use auto-fit when items should stretch to fill the row:

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

Reach for auto-fill only when you want the column rhythm preserved, for example when items will be added later and the grid should not reflow.

Bug 3: `grid-auto-flow: dense` scrambles the keyboard and screen-reader order

What goes wrong: turning on dense packing fills the holes in a mosaic, but keyboard focus and screen readers now jump around the page in a confusing sequence. Why: dense flow backfills small items into earlier holes, so visual order diverges from DOM order. Assistive technology and the tab key follow the DOM, which means a sighted keyboard user tabs to items in a different sequence than the one they see. Fix: keep the default sparse flow when order matters, or place items explicitly in DOM sequence:

.mosaic {
  display: grid;
  grid-auto-flow: row; /* default sparse: visual order matches DOM order */
}

If the mosaic must be gapless and order matters, assign grid-column and grid-row explicitly in DOM sequence rather than letting dense flow reorder items.

Real-World Recipes

Recipe 1: Classic sidebar plus fluid content

Use when a dashboard or docs page needs a fixed sidebar beside fluid content that never overflows on narrow content. The content column uses minmax(0, 1fr) so long code lines or wide tables shrink instead of blowing out the grid.

.shell {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  gap: 1.5rem;
  min-height: 100vh;
}
@media (max-width: 768px) {
  .shell { grid-template-columns: 1fr; } /* stack on mobile */
}

Recipe 2: Responsive card grid with no media queries

Use when a gallery or product list must show as many cards per row as fit, each at least 220px wide. auto-fit with minmax() handles every breakpoint automatically, and a final partial row stretches to fill the container.

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1.25rem;
}
.card-grid img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover; /* uniform tiles without distortion */
}

Recipe 3: Page skeleton with named areas

Use when a marketing page needs header, sidebar, main, and footer regions that rearrange cleanly on mobile. The text map makes the layout readable at a glance, and the mobile override just rewrites the map.

.page {
  display: grid;
  grid-template-columns: 220px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 1rem;
}
.page > header  { grid-area: header; }
.page > aside   { grid-area: sidebar; }
.page > main    { grid-area: main; }
.page > footer  { grid-area: footer; }

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

FAQ

Can I animate grid-template-columns between 1fr 3fr and 3fr 1fr?

Yes, in modern browsers, as long as both states have the same number and type of tracks. The browser interpolates the fr values smoothly, which gives a sliding sidebar effect with a plain CSS transition. If the track counts differ, the change snaps instead of animating.

What is subgrid and when would I use it?

subgrid lets a nested grid adopt its parent's tracks on one axis, so cards inside different parent cells can align their internal rows, like lining up buttons across a row of uneven cards. Set display: grid on the child with grid-template-rows: subgrid (or columns), and it inherits the parent's track lines. All modern browsers support it, so it is safe for current audiences without a fallback.

Why do my grid items overlap when I use negative line numbers?

Negative line numbers count back from the end of the explicit grid, so grid-column: 1 / -1 spans the full width. Overlap happens when two items are explicitly placed on the same lines; unlike auto-placement, explicit placement never moves an item out of the way. Give each item its own line range, or leave one of them auto-placed so the browser finds a free cell.

Does gap behave the same in grid and flexbox?

The spacing behavior is identical, but the old grid-gap name only ever applied to grid. grid-gap, grid-row-gap, and grid-column-gap still work as legacy aliases in grid layouts, yet gap, row-gap, and column-gap are the standard names and the only ones flexbox understands. Use the unprefixed names everywhere.

Share this guide