Sub topic 5
Spacing tracks with gap
This chapter covers the gap property: what it does, how it differs from margins, and where it behaves in unexpected ways. It assumes you can already define a grid with grid-template-columns and grid-template-rows, and that you know the fr unit and repeat() from the earlier chapters.
What gap does
gap sets the size of the gutters between tracks. A gutter is the empty space between two adjacent rows or two adjacent columns. Gaps are part of the grid container's layout, not of the items. The grid reserves the space first, then places items in the tracks that remain.
.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto auto;
gap: 16px;
}This grid has three columns and two rows. It therefore has two column gutters and one row gutter, each 16px.
Gaps appear only between tracks. There is no gap before the first track or after the last one. The outer edges of the grid stay flush with the container's content box.
The three properties
gap is a shorthand for two longhand properties.
| Property | Controls | Example |
|---|---|---|
| `row-gap` | Space between rows (vertical spacing) | `row-gap: 24px;` |
| `column-gap` | Space between columns (horizontal spacing) | `column-gap: 8px;` |
| `gap` | Both, row value first | `gap: 24px 8px;` |
With one value, gap applies it to both axes. With two values, the first is the row gap and the second is the column gap.
.a { gap: 20px; } /* rows 20px, columns 20px */
.b { gap: 24px 8px; } /* rows 24px, columns 8px */
.c { row-gap: 1rem; column-gap: 2rem; }The row value comes first because gap follows the same order as grid-template-rows followed by grid-template-columns. A common mistake is to assume the order is horizontal then vertical, as with margin shorthand's left and right pairs.
Older code may use grid-gap, grid-row-gap, and grid-column-gap. These are the original names from before the properties were shared with other layout modes. Browsers still accept them as aliases, but new code should use the unprefixed names.
Allowed values
A gap accepts any non-negative length or percentage. The following are all valid.
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem; /* relative to root font size */
}
.tight { gap: 4px; }
.fluid { gap: 2vw; } /* scales with viewport width */
.calc { gap: calc(1rem + 0.5vw); }Negative values are invalid and the declaration is ignored. If you want items to overlap, use line-based placement or negative margins on the items, not a negative gap.
Percentages
A percentage gap is resolved against the grid container's content box in the matching axis. A column-gap: 5% is 5% of the container's content width. A row-gap: 5% is 5% of the container's content height.
This second case is a gotcha. If the container's height depends on its content (the usual case, with height: auto), the percentage cannot be resolved against a known height. Browsers then treat it as zero for the purposes of sizing the container, and later resolve it against the final height. The result can differ from what you expected, so prefer rem, px, or viewport units for row gaps.
Gap versus margin
You can also space grid items with margins. The two approaches behave differently.
| Row | `gap` | `margin` on items |
|---|---|---|
| Set on | The container | Each item |
| Outer edges | No extra space | Adds space at the grid edges |
| Affects track sizing | Yes, reserved before `fr` is distributed | Yes, added to item size |
| Spans | One gutter per track boundary crossed | Applies to each item individually |
Consider a row of three cards with margin: 0 8px on each card. The first card has 8px of space on its left and the last card has 8px on its right, so the grid has unwanted outer padding. You would then have to remove it with :first-child and :last-child rules. With gap: 16px you get exactly 16px between cards and nothing at the edges.
Use gap for consistent spacing between tracks. Use margins when a single item needs extra space of its own, for example one card that should sit further from its neighbor.
How gaps interact with fr units
The fr unit divides the space that is left after fixed sizes and gaps are subtracted. This means gaps never cause overflow by themselves when you use fr columns.
Take a container 1000px wide with this definition.
.grid {
display: grid;
grid-template-columns: 200px 1fr 1fr;
column-gap: 20px;
}The calculation is:
- Start with 1000px of available width.
- Subtract the fixed 200px column, leaving 800px.
- Subtract two gutters (2 × 20px = 40px), leaving 760px.
- Divide 760px between two
1frcolumns, giving 380px each.
If instead you used fixed track sizes that add up to more than the container, gaps add to the overflow. Three columns of repeat(3, 200px) with gap: 50px need 600px + 100px = 700px.
Gaps and spanning items
An item that spans several tracks also spans the gutters between them. Its size includes those gutter widths.
.grid {
display: grid;
grid-template-columns: repeat(4, 100px);
column-gap: 20px;
}
.wide {
grid-column: 1 / 3;
}The .wide item covers two 100px columns and the one 20px gutter between them, so it is 220px wide. Two separate items in those columns would be 100px each with 20px between them. This keeps spanning items aligned with the tracks around them.
Gaps and empty tracks
Gutters exist between tracks even when the tracks are empty. If a row has no items but has a defined size, the row and both of its neighboring gutters still take up space. This matters with grid-auto-rows and auto rows: an implicit row with no content has height 0, but the gutter next to it is still present when the track exists.
In practice, if you see an unexplained band of space at the bottom of a grid, check for an empty row created by grid-template-rows or by an item placed in a later row. Remove the empty track or reposition the item.
Gaps also work with auto-fill and auto-fit. When the browser calculates how many columns fit, it includes the gap in the calculation. In repeat(auto-fill, minmax(100px, 1fr)) with gap: 20px, a 460px container fits 4 columns, since 4 × 100 + 3 × 20 = 460.
Diagram: where the gutters sit
Common errors
- Putting
gapon the items. It has no effect there. It must be on the grid container. - Expecting space around the outside of the grid. Use
paddingon the container for that. - Reversing the two values in
gap: 24px 8px. The row gap comes first. - Using a percentage row gap in a container with automatic height.
- Adding both
gapand item margins, which produces double spacing. - Using a gap in a container that is not
display: grid(orinline-grid). In Flexbox and multi-column layoutsgapis supported in current browsers, but in a plain block container it does nothing.
Responsive gaps
Gaps can change with the viewport like any other length. A media query or clamp() keeps spacing proportional.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: clamp(0.75rem, 2vw, 2rem);
}The gap is never smaller than 0.75rem, never larger than 2rem, and scales at 2vw in between.
Exercise
A grid container is 900px wide with grid-template-columns: 100px 1fr 1fr; column-gap: 30px;. How wide is each 1fr column, and what is the width of an item with grid-column: 2 / 4?
Show answer
Subtract the fixed 100px column and two 30px gutters (60px) from 900px, leaving 740px. Each 1fr column is 370px. The item spans columns 2 and 3 plus the one gutter between them, so it is 370 + 30 + 370 = 770px wide.
Exercise
Write a rule that gives a grid 24px between rows and 12px between columns, using the gap shorthand. Then explain why adding margin: 12px to each item would not give the same result.
Show answer
Use gap: 24px 12px;. Margins would add space on all sides of every item, including the outer edges of the grid, and would add to the gap if one is also set. gap only adds space between tracks.