Sub topic 4
Spacing children with gap and space-x/space-y
Padding and margin put space inside and around a single element. This chapter covers space between siblings: the gap-* and space-* utilities. It assumes you know the spacing scale (4 is 1rem, 2 is 0.5rem) and the basics of flexbox and grid. It also assumes a project with Tailwind CSS installed.
Two ways to space siblings
You could add mr-4 to every item in a row, but then the last item carries a margin it doesn't need. Tailwind offers two utility families that put the spacing on the parent instead.
gap-*sets the CSSgapproperty on a flex or grid container. The browser's layout engine puts the space between items.space-x-*andspace-y-*add margin to the children, except the first or last one, through a generated selector.
Both take values from the spacing scale, so gap-4 and space-y-4 are both 1rem.
gap utilities
gap-4 compiles to gap: 1rem. It only works on containers that lay out their children as flex items, grid items, or multi-column content. On a plain block container it has no effect, and that is the most common reason a gap-* class seems to do nothing.
<ul class="flex gap-4">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>The utility adds 1rem between each pair of neighbors. There is no space before One or after Three.
Separate row and column gaps
gap-x-* sets the space between columns (column-gap). gap-y-* sets the space between rows (row-gap). Use them when the two directions need different values.
| Class | CSS property | Affects |
|---|---|---|
| `gap-4` | `gap` | Rows and columns |
| `gap-x-6` | `column-gap` | Space between columns |
| `gap-y-2` | `row-gap` | Space between rows |
<div class="grid grid-cols-3 gap-x-6 gap-y-2">
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
<div>E</div>
<div>F</div>
</div>This grid has wide gutters between its three columns and tight spacing between its two rows.
Gap in wrapping flex layouts
In a flex container, gap applies in both directions once items wrap. A tag list shows this well.
<div class="flex flex-wrap gap-2">
<span>Design</span>
<span>CSS</span>
<span>Accessibility</span>
<span>Performance</span>
<span>Typography</span>
</div>When the tags wrap onto a second line, the space between lines is also 0.5rem. With margins you would have to compensate for the edges yourself.
space-x and space-y utilities
space-y-2 adds vertical space between the children of a normal block container, where gap would do nothing.
<div class="space-y-2">
<p>First</p>
<p>Second</p>
<p>Third</p>
</div>The first paragraph gets no extra space above it. The second and third each get 0.5rem of margin on top. space-x-4 works the same way horizontally.
How the selector works
space-* styles the children from the parent's class. Simplified, the generated CSS looks like this:
.space-y-2 > :not(:last-child) {
margin-block-end: 0.5rem;
}Tailwind v4 uses this shape, wrapped in :where() so the selector adds no specificity. Tailwind v3 targets every child after the first one, using > :not([hidden]) ~ :not([hidden]), and sets a top margin. The details differ by version, but both versions use margin on the children, not the gap property.
That has three consequences:
- Margin on the children means you can use
space-*on any display type, includingblock. - A child's own margin utilities can conflict with it. The child's own margin wins or loses based on CSS specificity and order, so avoid mixing
space-y-*on the parent withmt-*ormb-*on children you want to control. - A child with
display: nonein v4 is still counted by:last-child. If the last child is hidden, the visible last child keeps a trailing margin. Hidden children in v3 are skipped through the[hidden]check, which only covers thehiddenattribute, not thehiddenclass.
Reversed order
If you reverse the visual order with flex-row-reverse or flex-col-reverse, the margin ends up on the wrong side. Add space-x-reverse or space-y-reverse to flip it.
<div class="flex flex-row-reverse space-x-4 space-x-reverse">
<div>1</div>
<div>2</div>
<div>3</div>
</div>With gap you avoid this, because the gap sits between items regardless of their order.
Negative values
Prefix with a dash to pull children together or overlap them. -space-x-2 is a common way to stack avatars.
<div class="flex -space-x-2">
<img class="size-8 rounded-full ring-2 ring-white" src="a.jpg" alt="Ana">
<img class="size-8 rounded-full ring-2 ring-white" src="b.jpg" alt="Ben">
<img class="size-8 rounded-full ring-2 ring-white" src="c.jpg" alt="Chi">
</div>Each avatar overlaps the previous one by 0.5rem. Negative gap values don't exist, because the CSS gap property can't be negative.
Choosing between gap and space
Prefer gap when you can. It is a layout feature, so it handles wrapping, reordering, and hidden children without extra classes. Reach for space-* when:
- The container is
display: blockand you don't want to change it to flex or grid just for spacing. - You need negative spacing, as in overlapping avatars.
- You are styling content you don't control, such as rendered Markdown, where you can only add classes to the wrapper.
| Situation | Better choice | Reason |
|---|---|---|
| Row of buttons in flex | `gap-*` | No margin on children |
| Wrapping tag list | `gap-*` | Spaces both rows and columns |
| Stack of paragraphs in a block | `space-y-*` | `gap` does nothing on a block |
| Overlapping avatars | `-space-x-*` | `gap` can't be negative |
| Reversed flex row | `gap-*` | No `-reverse` modifier needed |
| Grid with different row and column spacing | `gap-x-*` and `gap-y-*` | `space-*` has no grid support |
Responsive and arbitrary values
Both families accept breakpoint prefixes and arbitrary values, as other utilities do.
<div class="flex flex-col gap-2 md:flex-row md:gap-6">
<div>Left</div>
<div>Right</div>
</div>
<div class="space-y-2 lg:space-y-4">
<p>Intro</p>
<p>Details</p>
</div>
<div class="flex gap-[10px]">
<span>A</span>
<span>B</span>
</div>The first block stacks on small screens with a 0.5rem gap and becomes a row with a 1.5rem gap from the md breakpoint. The last block uses a square-bracket value for a one-off length. If you repeat that value, add it to the theme so the same name works for gap-*, space-*, padding, and margin, as the earlier chapter showed.
Common mistakes
- Using
gap-*on a block container. Nothing happens. Addflexorgrid, or usespace-y-*. - Mixing
space-y-*with child margins. The result depends on specificity and is hard to predict. Pick one source of spacing per container. - Expecting
space-*to handle wrapped rows. In aflex-wrapcontainer,space-x-*only adds horizontal margin. Wrapped lines get no vertical spacing, and the first item on each new line starts with a stray left margin. Usegap-*instead. - Forgetting
-reversemodifiers. Reversing the order of children withspace-*leaves the space on the wrong side. - Using
space-*withflex-coland conditional children. If the last child is hidden, a trailing margin may remain. Prefergap-*so hidden children take no space.
A worked example
This card list uses both families, each where it fits best.
<section class="space-y-6">
<h2 class="text-xl font-semibold">Recent posts</h2>
<ul class="grid gap-x-6 gap-y-4 sm:grid-cols-2">
<li class="space-y-1">
<h3 class="font-medium">Post one</h3>
<p class="text-sm text-gray-600">A short summary.</p>
</li>
<li class="space-y-1">
<h3 class="font-medium">Post two</h3>
<p class="text-sm text-gray-600">Another summary.</p>
</li>
</ul>
<div class="flex flex-wrap gap-2">
<a href="#">Newer</a>
<a href="#">Older</a>
</div>
</section>The outer <section> is a block, so space-y-6 separates the heading, the list, and the links. The list is a grid, so gap-x-6 gap-y-4 controls its columns and rows separately. Each list item is a block, so space-y-1 stacks its title and summary tightly. The links row uses gap-2, so wrapping works without extra classes.
Exercise
A container has the classes block gap-4, and its three children touch each other. Give two different fixes, and say when each is the better choice.
Show answer
Fix 1: change block to flex flex-col (or grid). gap-4 then works, and it is the better choice when the layout may wrap, reorder, or hide children. Fix 2: replace gap-4 with space-y-4. This is the better choice when the container must stay display: block, for example around content you can't restructure. Both give 1rem between children.
Exercise
You build a row of overlapping avatars with flex -space-x-2. A teammate says to swap it for -gap-2. Will that work?
Show answer
No. Tailwind has no negative gap utilities, and CSS gap can't be negative. Keep -space-x-2, which applies a negative margin to the children and makes them overlap.