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.
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
| Value | Meaning | Typical use |
|---|---|---|
200px, 12rem | Fixed size | Sidebars, icons |
1fr | Share of free space | Flexible columns |
auto | Size to content | Labels, buttons |
minmax(200px, 1fr) | At least 200px, at most one share | Responsive columns |
fit-content(300px) | Size to content, up to 300px | Text columns with a cap |
repeat(4, 1fr) | Repeat a pattern | Equal 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
| Keyword | Behaviour when there are fewer items than columns |
|---|---|
auto-fit | Empty tracks collapse, and existing items stretch to fill the row |
auto-fill | Empty 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
| Question | Grid | Flexbox |
|---|---|---|
| Dimensions | Two (rows and columns together) | One (a row or a column) |
| Layout driven by | The container’s tracks | The items’ content sizes |
| Best for | Page layouts, card galleries, forms, dashboards | Navigation bars, button groups, centring, toolbars |
| Alignment across rows | Yes, items line up in columns | No, each line is independent |
| Overlapping items | Easy (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: contentswith care, or subgrid). - Using
1frwith long unbreakable content. Switch tominmax(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
autoorminmax(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.