Sub topic 4
Spacing children with gap and space-x/space-y
This chapter assumes you know the Tailwind spacing scale and the padding and margin utilities from the previous chapters. You also need basic familiarity with flex and grid containers in CSS.
Padding and margin put space around a single element. When you want even space between the children of a container, Tailwind gives you two tools: gap-* and space-x-* / space-y-*. They look alike, but they work differently.
Gap: spacing for flex and grid
gap-4 maps to gap: 1rem. It uses the same scale as padding and margin, so gap-4 is 1rem in the default theme. The browser puts that space between children only. There is no extra space before the first child or after the last one.
<ul class="flex gap-4">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>Gap only works on flex and grid containers (and multi-column layouts). On a normal block container, gap-4 does nothing.
You can set each axis separately:
gap-x-*sets the space between columns (column-gap).gap-y-*sets the space between rows (row-gap).
This matters when a flex row wraps (flex flex-wrap). Gap spaces both the items in a row and the wrapped rows, with no extra margin logic.
Space-between: margin on siblings
For a normal block container, use space-y-* or space-x-*.
<div class="space-y-2">
<p>First</p>
<p>Second</p>
</div>space-y-2 adds vertical margin between the children of the container. Tailwind does this with a selector that matches every child except the first. Each of those children gets a top margin, so the first child gets none.
Use space-x-* for horizontal spacing. Negative values such as -space-x-2 overlap children, which is a common way to stack avatars.
Choosing between them
| Situation | Better choice | Reason |
|---|---|---|
| Flex or grid container | `gap-*` | Native CSS, no margin side effects |
| Wrapping flex row | `gap-*` | Spaces rows and columns correctly |
| Plain block children | `space-y-*` | Gap does not apply to block layout |
| Overlapping items | `-space-x-*` | Negative margins create the overlap |
The space-* utilities have known weak points:
- They set margin on the children. If a child already has its own margin utility, the two can conflict.
- They do not track wrapping. With
flex-wrap, items that wrap to a new line still get a left margin, so they look misaligned. - They ignore hidden children poorly in some setups. Because they depend on sibling position, spacing can look wrong when elements are removed or reordered with CSS.
In short, prefer gap-* whenever the container is flex or grid. Use space-* when the container is a plain block and you do not want to change its display type.
How the pieces fit
Common errors
- Adding
gap-4to adivthat has noflexorgridclass. Nothing happens, because a block container ignores gap. - Using
space-x-4on a wrapping flex row and getting uneven left edges on the second line. Switch togap-4. - Expecting space at the outer edges. Both gap and space utilities only add space between children. Use padding on the container for outer space.
Exercise
A div with the classes flex flex-wrap space-x-4 wraps its items onto a second line, and the second line starts indented. Which utility fixes this, and why?
Show answer
Replace space-x-4 with gap-4. The space-x-* utility adds a left margin to every child except the first one in the DOM, so the first item on the wrapped line still has that margin and is indented. Gap spaces only between items in the layout, so the wrapped line starts flush with the left edge, and it also spaces the rows from each other.