Sub topic 5
Spacing children with gap in flexbox and grid
This chapter assumes you know basic HTML, CSS selectors, the box model, and rem units. It also assumes you have a spacing scale like the one from the previous chapter, with custom properties such as --space-1 through --space-6.
Why gap exists
Before gap, developers spaced items in a row or grid with margins on the children. That caused two recurring problems. The first or last item had an extra margin at the container's edge, so you needed :last-child overrides. When items wrapped onto a new line, the margins on the wrapped items no longer lined up.
The gap property fixes both. You set it on the container, and it adds space only between items. It never adds space at the outer edges. The children carry no spacing rules of their own, so they can be reused in any layout.
Gap in grid
In a grid, gap sets the space between rows and between columns. Here is the card layout from the earlier chapters.
.card {
padding: var(--space-4);
border: 1px solid #ccc;
}
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: var(--space-4);
}Padding creates the space inside each card. gap creates equal space between the cards. Because the gap is --space-4 and the padding is also --space-4, the distance between two cards matches the distance from a card's border to its text. If you want cards to look more clearly separated from each other, make the gap larger than the padding.
Gap in flexbox
Flexbox supports gap in all current browsers. It works the same way, and it respects wrapping.
.tag-list {
display: flex;
flex-wrap: wrap;
gap: var(--space-2);
}When the tags wrap onto a second line, the space between the lines is also --space-2. With margins, you would have to handle the line spacing separately.
Row gap and column gap
gap is shorthand for row-gap and column-gap. One value sets both. Two values set the row gap first and then the column gap.
.toolbar {
display: flex;
flex-wrap: wrap;
gap: var(--space-3) var(--space-5);
}In this toolbar, wrapped lines sit --space-3 apart, and items on the same line sit --space-5 apart. Use two values when the horizontal and vertical relationships differ. The proximity principle applies here too. A small row gap and a large column gap make the items read as separate rows.
Common errors
- Using gap on a block container.
gaphas no effect on a normal block layout. The container needsdisplay: flex,display: grid, or multi-column layout. - Mixing gap and child margins. The two add together. If you set a gap of
1remand a margin of1remon the children, items end up2remapart. Choose one source of spacing for each container. - Expecting gap to separate content from the container edge. Gap never adds outer space. Use padding on the container for that.
- Using gap for stacked paragraphs. Paragraphs in normal flow are not flex or grid items. The next chapters cover how to space them with a flow rule.
Try it
Start with this markup and apply the CSS above.
<ul class="tag-list">
<li>Layout</li>
<li>Spacing</li>
<li>Typography</li>
<li>Color</li>
<li>Accessibility</li>
</ul>Resize the window and watch the tags wrap. The gap stays consistent in both directions, and nothing sticks out at the edges.
Exercise
A flex container has gap: 1rem, and each child also has margin-right: 1rem. How far apart are two neighbouring children, and how would you fix the design so the spacing comes from one source?
Show answer
They are 2rem apart, because the gap and the margin add together. The last child also has an extra 1rem margin at the container edge. Remove the margin from the children and keep only gap: 1rem on the container.
Exercise
You want wrapped items to be 0.5rem apart vertically and 2rem apart horizontally. Write the declaration.
Show answer
gap: 0.5rem 2rem; (the first value is the row gap, and the second is the column gap).