Sub topic 4
Gutters: the space between columns
This chapter assumes you know basic HTML and CSS syntax, units such as px, rem, and %, and the box model. You should also know that a column grid divides a page into repeating tracks. Here you will learn what gutters are, how to size them, and how to set them with CSS Grid.
What a gutter is
A gutter is the empty space between two neighboring columns. Content never sits in a gutter. It sits in columns, and the gutter keeps adjacent items from touching.
A twelve-column grid has eleven gutters, because gutters appear only between columns. The space at the left and right edges of the grid is a margin, not a gutter. That is the subject of the next chapter.
Choosing a gutter size
A gutter should be large enough that items read as separate, and small enough that the page still feels like one layout. Common choices:
- Small screens:
1rem(16px) is usually enough. - Larger screens:
1.5remto2remgives wider columns more room. - Dense interfaces such as dashboards:
0.5remto1rem.
Use rem rather than px. Gutters then scale with the user's font-size setting, so spacing stays in proportion to the text.
Pick one gutter value and reuse it. If the horizontal gap is 1.5rem and the vertical gap is 0.4rem, the layout looks accidental. Many designers also match the vertical gap to the horizontal gutter, so cards are separated evenly in both directions.
Setting gutters with CSS Grid
CSS Grid has dedicated properties for gutters. The container below creates twelve flexible columns separated by a 1.5rem gutter:
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}column-gap sets the space between columns. row-gap sets the space between rows. The shorthand gap sets both, and gap: 1rem 2rem means row gap first, then column gap.
Gaps are drawn by the grid container, so items need no margins of their own. The gap also appears only between tracks. Unlike item margins, it adds no space at the outer edges. That is why a grid with gap lines up flush with its container.
Gutters and column widths
With 1fr columns, the gutters are subtracted first and the remaining width is shared equally. For a 1152px container (72rem) with twelve columns and a 24px gutter:
- Total gutter width is 11 × 24px = 264px.
- Remaining width is 1152px − 264px = 888px.
- Each column is 888px ÷ 12 = 74px.
A wider gutter therefore makes every column narrower. If a layout feels cramped, check whether the gutter is taking more space than you intended.
Gutters across screen sizes
A gutter that suits a desktop can waste space on a phone. A common approach is to keep a smaller value by default and raise it at a wider breakpoint:
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
}
@media (min-width: 48em) {
.grid {
grid-template-columns: repeat(12, 1fr);
gap: 1.5rem;
}
}Common gutter errors
- Using margins on items as well as
gap. The spacing doubles, and the outer edges gain extra space. Choose one method. - Faking gutters with empty columns. This adds markup and breaks the column count. Use
gap. - Applying a percentage gap and expecting it to match the columns. Percentage gaps resolve against the container, so they can surprise you. Prefer
rem. - Forgetting that gutters shrink columns. On narrow containers a large gutter can leave columns too thin for their content.
Exercise
A container is 960px wide and has six equal 1fr columns with a 20px column gap. How wide is each column?
Show answer
There are five gutters, so 5 × 20px = 100px. The remaining width is 960px − 100px = 860px. Each column is 860px ÷ 6, which is about 143.3px.
Exercise
Why is gap usually better than putting a right margin on every grid item?
Show answer
gap adds space only between tracks, so the outer edges stay flush and nothing needs special handling for the last item. Margins add space at the edges too, and they can combine with the gap to double the spacing.