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.
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):
| Columns | Measured track widths | Grid content width |
|---|---|---|
repeat(3, 1fr) | 48 px / 886 px / 48 px | 1015 px |
repeat(3, minmax(0, 1fr)) | 189 px / 189 px / 189 px | tracks 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:
| Pattern | Tracks created | Each item |
|---|---|---|
auto-fit | 492px 492px 0px 0px | 492 px |
auto-fill | 238px 238px 238px 238px | 238 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?
What does 1fr mean in CSS Grid?
How do I make a responsive grid without media queries?
- [1]W3C, CSS Grid Layout Module Level 2 (fr, auto-fit, auto-fill, subgrid) โ
- [2]MDN browser-compat-data, subgrid, checked Oct 13, 2026 โ
- [3]MDN, minmax() โ
- [4]MDN, repeat() with auto-fill and auto-fit โ
- [5]W3C, CSS Values and Units Level 4: min() and other comparison functions โ
- [6]W3C, CSS Box Alignment Module Level 3 โ