Sub topic 7
Spacing items with gap
This chapter assumes you can create a flex container with display: flex, that you know the main axis and cross axis, and that you are comfortable with rem and px units. You will replace margin-based spacing with the gap property.
Why gap exists
Before gap worked in flexbox, authors spaced items with margins. Margins belong to the items, so the first and last item also get a margin, and you end up writing rules to cancel it. The gap property sets space between items on the container. It adds space only between neighbors, never at the outer edges.
The three properties
column-gapsets the space between items along a row, which is the horizontal space in a left-to-right row layout.row-gapsets the space between flex lines, which matters once items wrap.gapis the shorthand. One value sets both. Two values setrow-gapfirst, thencolumn-gap.
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1rem; /* row-gap 0.5rem, column-gap 1rem */
}column-gap and row-gap are named by direction on the page, not by flex axis. If you change flex-direction to column, row-gap then separates the items in the stack, and column-gap only has an effect between wrapped columns.
Gap and wrapping
With a single line of items, only the gap along the main axis is visible. Once flex-wrap: wrap creates several lines, the row gap appears between lines.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
}Here the browser fits as many 200px cards as it can in each line, counting the gaps. The leftover space is then shared by flex-grow. A margin-based version would need negative margins on the container to hide the extra space at the edges. The gap version needs none.
Gap versus margin
| Need | Use |
|---|---|
| Even space between all items | `gap` |
| Extra space after one specific item | `margin` on that item |
| Push an item and its followers to the end | `margin-left: auto` |
| Space between the container and its content | `padding` on the container |
Gap and margins add together. If the container has gap: 1rem and an item has margin-right: 1rem, the space after that item is 2rem.
Common errors
- Expecting gap to create space at the edges. Use container padding for that.
- Using
row-gapon a single-line row and seeing no change. With no wrapped lines there is no space between lines. - Writing the two-value shorthand in the wrong order. The row gap comes first.
Exercise
A container has display: flex; flex-wrap: wrap; gap: 2rem 0.5rem;. How much space is there between items on the same line, and how much between two lines?
Show answer
Between items on the same line there is 0.5rem, because the second value is column-gap. Between two lines there is 2rem, because the first value is row-gap.
Exercise
Why is gap usually better than giving every item margin-right: 1rem?
Show answer
Margins add space after the last item too, so you need extra rules to remove it. gap puts space only between items, and you set it once on the container.