Grid Basics: Grid Containers, Tracks, Lines, Cells and the fr Unit
What display: grid does, the vocabulary of grid layout (lines, tracks, cells, areas and gaps), defining columns and rows, how the fr unit shares space and why 1fr isn't always equal, and the explicit and implicit grid.
kvi5hnuCode Sprout team member8 min readOn this page
Flexbox (Part 9) lays items out along one line. Grid lays them out in two dimensions at once: you define columns and rows, and items line up in both directions. That makes it the tool for page layouts, card grids where columns must line up, forms with aligned labels, dashboards, image galleries, and any design drawn on a grid.
The big difference in approach: in flexbox, the items' content decides their size, and the container shares out what's left. In grid, the container defines the tracks, and items are placed into them.
A first grid
.cards {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* three equal columns */
gap: 1rem;
}
That's a complete layout: every child of .cards goes into the next cell, three per row, with 1rem between them. New rows are created as needed.
display: grid makes a block-level grid container; display: inline-grid makes an inline-level one. Like flex items, its in-flow children become grid items and are blockified (a <span> child computes to display: block, tested in Chrome); text directly inside becomes an anonymous item; float has no effect on items; and margins between items don't collapse.
The vocabulary

| Term | Meaning |
|---|---|
| grid line | a dividing line between tracks, numbered from 1 at the start; also from −1 at the end |
| track | a column or a row: the space between two adjacent lines |
| cell | the intersection of one row and one column, the smallest unit |
| area | a rectangle of one or more cells, bounded by four lines |
| gap | space between tracks, also called a gutter |
Three columns have four column lines. Items are placed by line numbers (article 68): grid-column: 1 / 3 means from line 1 to line 3, two columns wide. Negative numbers count from the end of the explicit grid, so grid-column: 1 / -1 always spans the full width, however many columns there are.
Defining tracks
grid-template-columns and grid-template-rows list the size of each track:
.layout {
display: grid;
grid-template-columns: 15rem 1fr; /* a fixed sidebar and a flexible main column */
grid-template-rows: auto 1fr auto; /* header, content, footer */
min-block-size: 100dvh;
}
| Track size | Meaning |
|---|---|
a length: 200px, 15rem | fixed |
a percentage: 25% | of the container's size |
fr | a share of the leftover space |
auto | sized to fit the content, and stretches to fill any space the fr tracks leave |
min-content, max-content, fit-content() | based on the content (article 67) |
minmax(min, max) | between two sizes (article 67) |
The initial value, none, means no explicit tracks: without grid-template-columns, a grid has one column, and items stack in rows. repeat() shortens repeated sizes: repeat(3, 1fr) is 1fr 1fr 1fr (article 67).
The fr unit
fr is a fraction of the free space: what's left after fixed tracks, content-sized tracks and gaps have been subtracted. The free space is split in proportion to the fr values.
Tested in Chrome, in a 600px grid:
grid-template-columns | Columns |
|---|---|
200px 1fr 2fr | 200px, 133.3px, 266.7px: the 400px left is split 1 : 2 |
repeat(3, 1fr) with gap: 30px | 180px each: 600 − 60px of gaps, divided by 3 |
0.25fr 0.25fr | 150px each: when the fr values add up to less than 1, they take only that fraction of the space, leaving the rest empty |
Unlike percentages, fr accounts for gaps automatically: repeat(4, 25%) with a gap overflows the container, while repeat(4, 1fr) fits exactly.

In the last grid only lines 1 to 3 exist: the empty half isn't a track, just space the fr values didn't claim.
Why 1fr columns aren't always equal
1fr actually means minmax(auto, 1fr): a track can't get narrower than its content's minimum size. Tested in Chrome: three 1fr columns in 600px, with an unbreakable word 290.6px wide in the first, are 290.6px, 154.7px and 154.7px: the first column is as wide as the word, and the other two share the rest. It's the same issue as flex items' automatic minimum (article 62).
When columns must be equal whatever their content, set the minimum to 0, and let the content wrap or scroll inside:
.cols { grid-template-columns: repeat(3, minmax(0, 1fr)); } /* 200px each, tested in Chrome */
.cols pre { overflow-x: auto; }
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
select { font: inherit; margin-inline-start: 0.25rem; }
.grid {
display: grid;
grid-template-columns: var(--cols, 1fr 1fr 1fr);
grid-auto-rows: 3rem;
gap: 0.4rem;
margin-top: 0.75rem;
padding: 0.4rem;
border: 2px dashed #8a3b12; border-radius: 8px;
}
.grid > div {
padding: 0.4rem; border-radius: 6px;
background: #f4efe6; border: 1px solid #8a3b12;
}
.grid > div:first-child { background: #fde2e1; border-color: #b3261e; }
With 1fr 1fr 1fr, the first column is wider than the others, because of the long word. With minmax(0, 1fr), the columns are equal and the word spills out of its cell; add overflow-wrap: anywhere or overflow: hidden to handle it. (overflow: hidden on an item also removes its automatic minimum, like in flexbox.) auto 1fr makes two columns, the first as wide as its content. 0.25fr 0.25fr leaves half the width empty.

The first grid is the surprise: three 1fr columns, three different widths, because the word sets the first column's minimum.
Gaps
gap sets the space between rows and columns, row-gap and column-gap separately:
.grid { gap: 1rem 2rem; } /* 1rem between rows, 2rem between columns */
Gaps only go between tracks, never at the container's edges, and items can't be placed in them. Percentages refer to the container's size. The older names grid-gap, grid-row-gap and grid-column-gap still work as aliases.
The explicit and implicit grid
The tracks you define with grid-template-columns and grid-template-rows are the explicit grid. When there are more items than cells, or an item is placed outside the defined tracks, the grid adds implicit tracks to hold them.
Tested in Chrome: a grid with grid-template-columns: 100px 100px and three items puts the third in a new, implicit row. Implicit rows are sized auto by default; grid-auto-rows sets their size, and article 69 covers auto-placement in detail.
.gallery {
display: grid;
grid-template-columns: repeat(4, 1fr); /* explicit columns */
grid-auto-rows: 10rem; /* every row created for the items is 10rem */
gap: 0.5rem;
}
Most grids define their columns and let the rows be implicit: the number of items decides the number of rows.
Inspecting grids
All browsers' DevTools have a grid overlay: in the Elements or Inspector panel, a grid badge next to a grid container shows its lines, numbers, track sizes, gaps and area names on the page. It's the fastest way to understand why an item ended up where it did. In getComputedStyle, grid-template-columns returns the resolved sizes in pixels, such as 200px 133.328px 266.672px.
Grid or flexbox?
- Use grid when items should line up in both directions, or when the layout, not the content, should decide the sizes.
- Use flexbox for a line of items sized by their content (article 61).
They combine well: a grid for the page and the card list, flexbox inside each card.
What's current
- Grid works in all browsers, and has since 2017; the old
-ms-gridsyntax is obsolete. gapreplacedgrid-gap, which still works as an alias.- Subgrid (article 71) is in all browsers since 2023.
- Masonry-style layouts with grid lanes (article 72) are arriving.
Common questions
What's the difference between fr and %?
Percentages are of the container's whole size and ignore gaps, so they can overflow. fr shares only the space left after gaps and fixed tracks.
Why is one of my 1fr columns wider than the others?
Its content has a minimum width larger than the share: 1fr is minmax(auto, 1fr). Use minmax(0, 1fr).
How do I make an item span all columns?
grid-column: 1 / -1.
Do I need to define rows?
Usually not. Rows are created automatically as items fill the columns; use grid-auto-rows to size them.
Can grid items overlap? Yes: place two items in the same cells (article 70). Unlike flexbox, grid allows it.
Summary
display: gridmakes a grid container; its children become blockified grid items.- Lines divide tracks; tracks are columns and rows; cells are single intersections; areas are rectangles of cells.
grid-template-columnsandgrid-template-rowsdefine the explicit grid; extra items create implicit tracks.frshares the free space after gaps and fixed tracks;1frhas a content-based minimum, so useminmax(0, 1fr)for strictly equal columns.gapspaces tracks, never the edges.
Practice questions
In an 800px grid with gap: 20px, what are the column widths?
.g { grid-template-columns: 180px 1fr 3fr; }
Answer
The free space is 800 − 180 − 40 (two gaps) = 580px, split 1 : 3. The columns are 180px, 145px and 435px.
A grid has the rule below and holds 7 items. How many rows are there, and are they explicit or implicit?
.g { display: grid; grid-template-columns: repeat(3, 1fr); }
Answer
Three rows, all implicit: no rows are defined, so the grid creates them as items fill the three columns (3 + 3 + 1).
Why does grid-template-columns: repeat(4, 25%) with gap: 1rem overflow, and what should you use instead?
Answer
Four 25% columns already fill the container, and the three gaps are added on top. repeat(4, 1fr) subtracts the gaps first, so it fits.
A two-column layout uses grid-template-columns: 1fr 1fr, but a wide <pre> makes the left column much wider. Fix it.
Answer
.layout { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.layout pre { overflow-x: auto; }
Previous: Flexbox in Practice: Navigation Bars, Media Objects, Sticky Footers and Forms
Next: Grid Track Sizing: repeat(), minmax(), auto-fill, auto-fit and Intrinsic Sizes

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.