Sub topic 7
Spacing items with gap
This chapter assumes you can already create a flex container, and that you know flex-direction, justify-content, align-items and flex-wrap. It also assumes you know the box model and CSS length units. Here you will learn how gap separates flex items, how it behaves in wrapped and column layouts, and where it differs from margins.
What gap does
The gap property sets the space between the items of a flex container. It adds space only between neighbors, never before the first item or after the last one.
.nav {
display: flex;
gap: 1rem;
}With this rule, every pair of adjacent items is separated by 1rem. The container's outer edges get no extra space. Doing this with margins is awkward. A margin-right on every item leaves a stray margin after the last one, so you need a :last-child override or a * + * selector. gap removes that bookkeeping.
The value is a length (8px, 1rem), a percentage, or a calc() expression. The default is normal, which in a flex container behaves like 0.
row-gap and column-gap
gap is a shorthand for two longhand properties.
| Property | Controls | Space between |
|---|---|---|
| `column-gap` | Horizontal space | Items on the same row, or columns |
| `row-gap` | Vertical space | Rows (flex lines) |
| `gap` | Both | Sets `row-gap` first, then `column-gap` |
The shorthand accepts one or two values. One value applies to both axes. Two values are read as row first, then column.
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem; /* row-gap 0.5rem, column-gap 1.5rem */
}Note the order. It is easy to expect "horizontal then vertical" because margin and padding read differently, but gap always lists the row gap first.
Which gap applies depends on direction and wrapping
The names row-gap and column-gap describe the physical layout, not the flex main axis. That matters when you change flex-direction.
- In
flex-direction: row, items sit side by side. The space between them iscolumn-gap. The space between wrapped lines isrow-gap. - In
flex-direction: column, items stack vertically. The space between them isrow-gap. The space between wrapped columns iscolumn-gap.
.stack {
display: flex;
flex-direction: column;
gap: 1rem;
}Because the single-value shorthand sets both, .stack works without thinking about which longhand is in play. You only need the longhands when the two spaces should differ.
Gap and wrapping
Gap is most useful with flex-wrap: wrap. Margins on wrapped items double up or leave uneven edges at line breaks. Gap gives the same spacing between items and between lines without any adjustments.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
}The gap counts toward the space used on a line. The browser first decides how many items fit, and the gaps between them are part of that calculation. Only then does flex-grow share what is left. Two items of 200px with a 1rem gap need about 416px in total (at a 16px root font size), not 400px.
Gap reduces space for percentage widths
A common mistake is sizing items with percentages and then adding a gap.
.broken {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.broken > * {
flex: 0 0 33.333%;
}Three items at 33.333% fill the line exactly, so there is no room for the two gaps. The third item wraps to the next line. You have two common fixes.
- Let the items shrink and grow instead of fixing their width:
flex: 1 1 200px. - Subtract the gaps with
calc(). For three items per row and a1remgap, each item is one third of the width minus two thirds of a gap:
.fixed > * {
flex: 0 0 calc((100% - 2rem) / 3);
}The 2rem is two gaps of 1rem. If you change the gap, change this number too. A custom property keeps them in sync:
.fixed {
--gap: 1rem;
display: flex;
flex-wrap: wrap;
gap: var(--gap);
}
.fixed > * {
flex: 0 0 calc((100% - 2 * var(--gap)) / 3);
}Gap and justify-content
Gap is a minimum. With justify-content: space-between, the browser places the gaps first and then distributes any remaining free space on top of them.
.toolbar {
display: flex;
justify-content: space-between;
gap: 1rem;
}If the container is wide, the items spread to the edges and the visible space is larger than 1rem. If the container is narrow, the gap guarantees at least 1rem between items before the items overflow or shrink. This is a useful combination for navigation bars, because the gap protects against items touching on small screens.
Edge cases
- Hidden items. An item with
display: nonecreates no box, so it creates no gap. An item withvisibility: hiddenstill takes up space, and its gaps remain. - Absolutely positioned children. They are out of flow and are not flex items, so they take no part in gap.
- Pseudo-elements.
::beforeand::afterwithcontentbecome flex items and receive gaps. An empty decorative pseudo-element can add an unexpected gap. - Reordering. Gaps follow the visual order. If you use
orderto move items, the gap still sits between visual neighbors. - Percentages. A percentage
column-gapresolves against the container's content box width. A percentagerow-gapresolves against its height. When the container's height is not set by a definite value, a percentagerow-gapis treated as0. Use lengths such asremif you want predictable results. - Margins still combine. Gap does not replace margin. Item margins add to the gap. If you set both, the space between two items is the margin of each plus the gap.
Gap versus margins
Choose gap when the spacing is between siblings and is the same for all of them. Choose margins when one item needs its own spacing, such as pushing a single item away with margin-left: auto. The two work well together: gap for the uniform rhythm, auto margins for the exceptions.
Practice
Exercise
A container uses display: flex, flex-wrap: wrap and gap: 10px 30px. Which value is the space between two items on the same line, and which is the space between two lines?
Show answer
The first value is row-gap, so the space between lines is 10px. The second value is column-gap, so the space between items on the same line is 30px.
Exercise
You want three equal items per row in a wrapping flex container with gap: 1.5rem. Write the flex declaration for the items so that all three fit on one line.
Show answer
Subtract the two gaps from the full width before dividing by three: flex: 0 0 calc((100% - 3rem) / 3); Two gaps of 1.5rem equal 3rem. Using flex: 0 0 33.333% fails because the gaps leave no room and the third item wraps.