Sub topic 4
Gutters: the space between columns
A gutter is the empty space between two neighbouring columns. This chapter covers how to size gutters, how they affect column widths, and how to build them in CSS Grid. It assumes you know basic CSS syntax, units such as px, rem, and %, and the box model. It also assumes you have seen display: grid and repeat(12, 1fr).
What a gutter does
Without gutters, adjacent content touches. Text in one column runs straight into the text of the next, and cards merge into a single block. Gutters give each column a visible edge and let the eye separate one piece of content from another.
Gutters also have a structural role. They are part of the grid's arithmetic, so they decide how wide each column is and how wide a multi-column item becomes.
Three properties define a gutter:
- A gutter exists only between two columns. A 12-column grid has 11 gutters, not 12.
- Every gutter in the grid has the same width.
- A gutter holds no content. Items never start or end inside one.
Space outside the first and last columns is a margin, which is a separate concept covered in the next chapter. Mixing the two up is the most common source of uneven spacing.
The width arithmetic
With n columns and a gutter of g, the column width in a container of width W is:
column = (W - (n - 1) × g) / n
Take a 72rem container (1152px at the default 16px root size) with 12 columns and a 1.5rem (24px) gutter:
- Total gutter space: 11 × 24px = 264px.
- Remaining space for columns: 1152px − 264px = 888px.
- Each column: 888px / 12 = 74px.
An item that spans k columns is wider than k columns alone, because it also swallows the k - 1 gutters inside it. The formula is k × column + (k - 1) × g:
| Span | Columns | Gutters inside | Width |
|---|---|---|---|
| 4 | 4 × 74px = 296px | 3 × 24px = 72px | 368px |
| 8 | 8 × 74px = 592px | 7 × 24px = 168px | 760px |
| 12 | 12 × 74px = 888px | 11 × 24px = 264px | 1152px |
Check the main-plus-sidebar layout from the previous chapter: 760px + 24px (the gutter between the two items) + 368px = 1152px. The widths add up exactly, which is the reason the grid keeps content aligned.
Creating gutters with the gap properties
In CSS Grid, gutters come from the gap properties on the grid container.
column-gapsets the space between columns, which is the gutter.row-gapsets the space between rows.gapis shorthand. One value sets both. Two values set row then column.
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
row-gap: 2rem;
}
/* Equivalent shorthand: row-gap first, then column-gap */
.grid {
gap: 2rem 1.5rem;
}Gap only appears between tracks. The browser adds no space before the first column or after the last, so you do not need :first-child or :last-child overrides. That is the main advantage over spacing items with margins.
The gap property also works on flexbox containers in current browsers. Flexbox has no column tracks, so the gap there is only a spacing rule between items, not a grid line system.
Why not use margins on items?
Developers used to build gutters with margin-right on each item, or with padding and negative margins on a row wrapper. Gap is simpler for these reasons:
| Technique | Edge handling | Affects item width? | Typical problem |
|---|---|---|---|
| `margin-right` on items | Last item needs an override | Yes, items shrink or overflow | Extra space on the last column |
| Padding inside each column, negative margin on the row | Row must cancel the outer padding | Padding sits inside the item | Backgrounds and borders show the padding |
| `gap` on the grid | None needed | No, gap is outside items | None for plain layouts |
Because gap is outside the items, backgrounds, borders, and width: 100% all behave the way you expect.
Choosing a gutter size
There is no universal value, but these guidelines hold up in practice:
- Typical gutters run from 16px to 32px (1rem to 2rem). On small screens, 12px to 16px is common. On wide desktop layouts, 24px to 32px.
- The gutter should be visibly smaller than a column. If it approaches the column width, the grid looks like a row of islands.
- The gutter should be large enough to separate text columns. A narrow gutter between two text blocks makes lines from one column blend into the next.
- Pick values from a spacing scale you already use, such as multiples of 0.5rem or 8px. This keeps the grid consistent with the rest of the spacing.
- Use
remrather thanpxwhen you want gutters to scale with the user's font size settings. Usepxonly when you need exact pixel alignment.
Row gap and column gap do not have to match. Many designs use a larger row gap, since stacked cards or sections often need more vertical room. If your design shows a uniform grid, keep them equal.
Gutters that change with screen width
A fixed gutter that looks right at 1200px often feels heavy at 360px, where 24px of gutter on a four-column grid eats a large share of the width. You have two ways to adapt.
The first is to change the gap at a breakpoint, as in the responsive example from the plan:
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
}
.card {
grid-column: 1 / -1;
}
@media (min-width: 48em) {
.grid {
grid-template-columns: repeat(12, 1fr);
gap: 1.5rem;
}
.card {
grid-column: span 4;
}
}The second is a fluid gutter that grows smoothly between a minimum and a maximum, using clamp() and a custom property:
:root {
--gutter: clamp(1rem, 0.5rem + 2vw, 2rem);
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: var(--gutter);
}Here the gutter never drops below 1rem or exceeds 2rem, and it scales with the viewport in between. Storing it in --gutter lets other rules reuse the value, for example to match the padding of a card to the space around it.
Keep one rule in mind: the gutter should change at the same breakpoints as the column count, or at least in a way that keeps the layout predictable. A grid whose columns and gutter change at unrelated widths is hard to reason about and to debug.
Edge cases and failure modes
Fractional widths
Gutters rarely divide a container into whole pixels. With a 1000px container, 12 columns, and a 24px gutter, each column is (1000 − 264) / 12 ≈ 61.33px. Browsers handle sub-pixel values, but borders and images can look slightly blurry. Do not try to force whole numbers with fixed pixel columns. Accept the fractions and let fr do the work.
`1fr` and overflow
The fr unit shares only the space left after gutters are removed. But 1fr is shorthand for minmax(auto, 1fr), so a column will not shrink below its content's minimum size. A long unbroken string or a wide image can push a column wider, and the grid overflows its container. The gutters stay the same, but the columns grow.
The fix is to allow the minimum to be zero:
.grid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
gap: 1.5rem;
}If items still overflow, check for long words, pre blocks, or images with a fixed width, and give those elements max-width: 100% or overflow-wrap: anywhere as appropriate.
Gutters that cannot shrink
Gaps are fixed lengths. If you use a large gutter on a narrow screen, the columns can become very thin and may collapse to near zero width. With 12 columns and a 24px gutter, 264px of a 320px screen is gutter, which leaves 56px for 12 columns. This is why small screens should use fewer columns, a smaller gutter, or both.
Percentage gaps
A percentage gap resolves against the grid container's own content size, so the gutter changes whenever the container changes. This can be useful but also surprising: the row gap and column gap resolve against different dimensions, so gap: 5% can give unequal visible spacing. Prefer rem or clamp() unless you want that behaviour.
Empty and spanned areas
Gaps are drawn between grid tracks, not between items. An item that spans several columns has no gutter drawn inside it. This is why the width of a span includes the gutters it crosses. If you need the content inside a spanning item to line up with the page's columns, use a nested grid with grid-template-columns: subgrid, which is covered in a later chapter.
Summary of decisions
- Use
column-gapfor the gutter androw-gapfor vertical spacing, orgapfor both. - Pick a value from your spacing scale, in
rem, between roughly 1rem and 2rem. - Use
minmax(0, 1fr)if content can force columns wider than their share. - Reduce gutters, columns, or both on narrow screens.
- Compute spans as
k × column + (k - 1) × gutterwhen you need to check a width.
Exercise
A grid container is 960px wide and has 12 columns with a 20px gutter. How wide is one column, and how wide is an item that spans 3 columns?
Show answer
There are 11 gutters, so 11 × 20px = 220px of gutter space. The columns share 960px − 220px = 740px, so each column is 740 / 12 ≈ 61.67px. A 3-column item contains 3 columns and 2 gutters: 3 × 61.67px + 2 × 20px ≈ 185px + 40px = 225px.
Exercise
Your repeat(12, 1fr) grid overflows its container on small screens because one item contains a very long URL. The gap is correct. What change to grid-template-columns fixes it, and why?
Show answer
Use repeat(12, minmax(0, 1fr)). A plain 1fr means minmax(auto, 1fr), so a column cannot shrink below its content's minimum size. Setting the minimum to 0 lets the column stay at its share of the free space. You can also add overflow-wrap: anywhere to the item so the URL wraps.