CSSApr 5, 2026 ยท Updated Oct 13, 2026 ยท 4 min read

How to Use CSS Grid: A Practical Layout Guide

CSS Grid with measured examples: why 1fr columns came out 48, 886 and 48 px wide in our test, how auto-fit and auto-fill differ, the min() fix for grids that overflow on small screens, and subgrid.

๐Ÿ”ฒ
Try the Grid Generator
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01Your First Grid02Why 1fr Columns Are Not Always Equal03Named Template Areas04Responsive Grids Without Media Queries05Subgrid: Lining Up Nested Content06Alignment

Your First Grid

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 16px;
}

This makes three columns with 16 px between them. The fr unit shares out the space left after gaps and fixed-size tracks, so 1fr 2fr makes the second column twice as wide as the first. Grid works in two dimensions at once, rows and columns, which is what separates it from Flexbox; our Flexbox vs Grid guide covers when to use which.

Why 1fr Columns Are Not Always Equal

A 1fr track never shrinks below its content's minimum width, because it means minmax(auto, 1fr). One long unbreakable string, such as a URL, a code sample or a wide image, is enough to skew the whole grid. We put a long URL in the middle cell of a 600 px grid in Chromium (October 13, 2026):

ColumnsMeasured track widthsGrid content width
repeat(3, 1fr)48 px / 886 px / 48 px1015 px
repeat(3, minmax(0, 1fr))189 px / 189 px / 189 pxtracks fit; the URL overflows its own cell

With plain 1fr the side columns collapsed to 48 px and the grid spilled 415 px past its container. minmax(0, 1fr) removes the content minimum and gives three equal columns, but the URL then overflows inside its cell, so pair it with overflow-wrap: anywhere on the items to let long strings wrap.

Named Template Areas

.layout {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-areas:
    'header header'
    'sidebar main'
    'footer footer';
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

@media (max-width: 700px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-areas: 'header' 'main' 'sidebar' 'footer';
  }
}

Each string is a row and each word a cell, so the CSS reads like a sketch of the page. Reassigning the areas in a media query rearranges the layout without touching the HTML. Two rules to remember: every named area must form a rectangle, or the whole declaration is invalid and ignored, and a . marks an empty cell. Moving content visually does not change the reading or tab order, which still follows the HTML, so keep the source order sensible.

Responsive Grids Without Media Queries

repeat(auto-fit, minmax(200px, 1fr)) creates as many columns of at least 200 px as fit, then stretches them. The difference between auto-fit and auto-fill only shows when there are fewer items than columns. With two items in a 1,000 px container:

PatternTracks createdEach item
auto-fit492px 492px 0px 0px492 px
auto-fill238px 238px 238px 238px238 px

auto-fit collapses the empty tracks so the items grow to fill the row; auto-fill keeps the empty tracks, so the items stay column-sized. Use auto-fill when a lone item should look like part of a grid rather than a banner.

The minimum also has a trap: in a container narrower than it, the track keeps its minimum and overflows. In a 240 px container, minmax(280px, 1fr) produced 280 px items that stuck out 40 px. Capping the minimum with min() fixes it, and the items measured 240 px:

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr));
  gap: 20px;
}

Subgrid: Lining Up Nested Content

Cards in a row often have titles and text of different lengths, so their buttons end up at different heights. With subgrid, each card adopts the row tracks of the parent grid instead of creating its own, and the matching parts line up across cards:

.cards { display: grid; grid-template-columns: repeat(3, 1fr); }
.card {
  display: grid;
  grid-row: span 3;              /* title, text, button */
  grid-template-rows: subgrid;
}

MDN's compatibility data lists subgrid in Chrome and Edge 117, Firefox 71 and Safari 16, so it works in every current major browser.

Alignment

justify-items and align-items position items inside their cells, along the inline axis (horizontal in English) and the block axis. justify-content and align-content position the whole set of tracks when they are smaller than the container. place-items: center centers items both ways in one line. To lay out a grid visually and copy the CSS, use the Grid Generator.

Frequently Asked Questions

What is CSS Grid?+
CSS Grid is a two-dimensional layout system that lets you define both rows and columns. Unlike Flexbox (one-dimensional), Grid controls layout in both directions simultaneously.
What does 1fr mean in CSS Grid?+
One share of the space left after gaps and fixed tracks: 1fr 2fr makes the second column twice as wide as the first. A 1fr track will not shrink below its content's minimum width, though; with a long URL in one cell, our 600px three-column grid came out 48 px / 886 px / 48 px. Use minmax(0, 1fr) when columns must stay equal.
How do I make a responsive grid without media queries?+
Use grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr)). Items are at least 280px wide and reflow as the container changes; the min() keeps a single item from overflowing containers narrower than 280px, which plain minmax(280px, 1fr) did in our test.
Try it yourself

Use the Grid Generator โ€” free, no signup required.

โšก Open Grid Generator
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.