Sub topic 5
Spacing tracks with gap
This chapter assumes you can already define a grid with display: grid, grid-template-columns, and grid-template-rows, and that you know the fr unit. It covers how gap adds space between tracks and how that space affects track sizes.
What gap does
A track is a single row or column in the grid. The gap property sets the empty space between adjacent tracks. It is set on the grid container, not on the items.
| Property | Sets the space between |
|---|---|
| `column-gap` | Columns (the vertical gutters) |
| `row-gap` | Rows (the horizontal gutters) |
| `gap` | Both. One value sets both, and two values are read as row then column. |
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
row-gap: 24px;
column-gap: 12px;
}
/* Same result as the two lines above */
.grid {
gap: 24px 12px;
}The older names grid-gap, grid-row-gap, and grid-column-gap still work as aliases. Prefer the unprefixed names in new code. Flexbox and multi-column layout support them too.
Gaps only exist between tracks
A gap is never added before the first track or after the last one. A grid with three columns has two column gaps. The container's outer edge stays flush with the first and last tracks. If you want space around the whole grid, add padding to the container.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
padding: 20px;
}How gaps affect track sizes
The grid subtracts the gaps from the container's width first. It then divides what remains among the flexible tracks.
Suppose the container is 700px wide, with grid-template-columns: 1fr 1fr 1fr and column-gap: 20px.
- Two gaps take 2 × 20px = 40px.
- 700px − 40px = 660px remains.
- Each
1frcolumn gets 660px ÷ 3 = 220px.
The columns shrink to make room, so the grid stays inside its container. This is why gap is better than adding margins to items. Margins sit inside a track and don't change its size, and they also add space at the outer edges.
Units and common errors
Gap accepts any length, including rem, em, and %. A percentage gap is resolved against the container's content box, which is not always the value you expect. Use rem or px unless you have a reason not to.
- Fixed tracks can overflow. Gaps are not subtracted from fixed-size tracks. Three
200pxcolumns with a20pxgap need 640px. A narrower container makes them overflow. - Gaps stay when items span. An item that spans two columns also covers the gap between them.
- Empty tracks keep their gap. A gap still appears next to a track with no items. Only collapsing the track removes it, as
auto-fitdoes with empty tracks. - Items don't stretch into gaps. Alignment applies inside the track, so gaps are never part of an item's cell.
Exercise
A grid container is 1000px wide and uses grid-template-columns: repeat(4, 1fr); column-gap: 20px;. How wide is each column, and how much gap space is there in total?
Show answer
There are three gaps between four columns, so the gaps total 60px. The remaining 940px is divided by four, so each column is 235px wide.