Space Between in Tailwind CSS
space-x-* and
space-y-*
solve a common layout problem elegantly: adding equal spacing
between a row or column of siblings
without touching the children at all. One class
on the parent — space-x-4 —
and Tailwind uses the CSS adjacent-sibling combinator to add a
left margin to every child except the first. No more
writing ml-4 on every item,
and no trailing gap after the last one. This is the
sibling-spacing shortcut every Tailwind developer reaches for
daily.
space-x-* and space-y-* do and
why they exist
space-x-reverse and
space-y-reverse with reversed flex containers
space-* vs
gap-* vs individual margin utilities
What is Space Between? — One Class, All Gaps
Before space-x-* existed,
adding equal spacing to a row of buttons meant writing
ml-4 on every child, then
removing it from the first with a selector or extra class.
space-x-* eliminates that
entirely. Place it on the parent and Tailwind's
CSS adjacent-sibling combinator (> * + *) targets every direct child that follows another — every child
except the first — so there is never a trailing gap at either end.
space-x-4 once on
the container. Tailwind spaces every direct child
automatically — no class needed on the children themselves.
> * + * CSS
selector applies margin only to children preceded by another
sibling — the first item never gets margin, and nothing
appears after the last.
space-x-1 = 4px,
space-x-4 = 16px,
space-x-8 = 32px.
Identical formula to padding and margin: class number ×
4 = pixels.
space-x-reverse alongside
space-x-* when using
flex-row-reverse — it
flips the margin to the right side so gaps appear correctly.
display:flex or display:grid, but
correctly handles flex-wrap. The rule: single
non-wrapping row or column? Use space-*. Items wrap
to multiple lines? Always use gap-*.
space-x-* & space-y-* — Complete Utility Reference
Tailwind provides horizontal (space-x-*) and vertical (space-y-*)
variants, both using the same 4px scale. Each has a
-reverse companion for
reversed flex directions, and all accept responsive breakpoint
prefixes and arbitrary values.
space-x-4 = 16px between items. The default
choice for toolbars, navbars, and button groups.
space-y-4 =
16px between rows. Use on flex-col containers or
block lists instead of adding mb-4 to every
child.
space-x-4 space-x-reverse.
flex-col-reverse space-y-*.
space-x-4 lg:space-x-0 gives spacing on mobile,
removes it on large screens.
space-x-[13px] or space-y-[1.5rem].
Any valid CSS length inside square brackets overrides the
scale for pixel-perfect control.
<!-- space-x-3: button group — ml-3 on every button except the first -->
<div class="flex items-center space-x-3">
<button class="px-4 py-2 bg-cyan-500 text-white rounded-lg">Save</button>
<button class="px-4 py-2 bg-gray-700 text-white rounded-lg">Preview</button>
<button class="px-4 py-2 bg-red-900 text-white rounded-lg">Delete</button>
</div>
<!-- space-y-2: stacked chapter list -->
<ul class="flex flex-col space-y-2 p-0 list-none">
<li class="px-4 py-3 bg-gray-800 rounded-lg">Ch 15 — Padding</li>
<li class="px-4 py-3 bg-gray-800 rounded-lg">Ch 16 — Margin</li>
<li class="px-4 py-3 bg-gray-800 rounded-lg">Ch 17 — Space Between</li>
</ul>
<!-- flex-row-reverse: always pair space-x-* with space-x-reverse -->
<div class="flex flex-row-reverse space-x-4 space-x-reverse items-center">
<span class="px-3 py-1 bg-gray-700 rounded">A</span>
<span class="px-3 py-1 bg-gray-700 rounded">B</span>
<span class="px-3 py-1 bg-gray-700 rounded">C</span>
</div>
How Space Between Works — The CSS Selector Behind It
Knowing the generated CSS makes
space-x-* behaviour completely
predictable — and reveals its key limitation. Tailwind writes a
single rule using the adjacent-sibling combinator
> * + *, which matches
every direct child that is immediately preceded by another direct
child. That is every child except the first one. The rule also
uses a CSS custom property so that adding
space-x-reverse simply flips
which side gets the margin, without needing a separate utility
value.
/* space-x-4 — margin-left by default, flips to margin-right with space-x-reverse */
.space-x-4 > * + * {
--tw-space-x-reverse: 0;
margin-right: calc(1rem * var(--tw-space-x-reverse));
margin-left: calc(1rem * calc(1 - var(--tw-space-x-reverse)));
}
/* Adding space-x-reverse sets --tw-space-x-reverse: 1 */
/* Result: margin-right: 1rem, margin-left: 0 — direction flipped */
/* space-y-4 — margin-top by default, flips with space-y-reverse */
.space-y-4 > * + * {
--tw-space-y-reverse: 0;
margin-bottom: calc(1rem * var(--tw-space-y-reverse));
margin-top: calc(1rem * calc(1 - var(--tw-space-y-reverse)));
}
space-x-* uses > * + * — a DOM-order
selector. When items wrap to a new line, the first item on that
wrapped line is still the second child in the DOM, so it receives
margin-left. This causes unwanted left-indentation on
wrapped rows.
Fix: use gap-x-* (Chapter 18) whenever items may
wrap. Reserve space-x-* for guaranteed single-row
containers.
Responsive Space Between — Different Gaps at Each Breakpoint
Like all Tailwind utilities,
space-x-* and
space-y-* accept responsive
prefixes. Tailwind is mobile-first: the unprefixed class applies
from 0px up, and prefixed classes activate at that breakpoint and
above. Layer them to step spacing up as the screen grows.
<!-- Navbar: tight on mobile, comfortable on desktop -->
<nav class="flex items-center space-x-2 sm:space-x-4 lg:space-x-6">
<a href="/courses.html" class="text-gray-300 text-sm">Courses</a>
<a href="/events.html" class="text-gray-300 text-sm">Events</a>
<a href="/blog.html" class="text-gray-300 text-sm">Blog</a>
</nav>
<!-- Stacked list: compact mobile, spacious desktop -->
<ul class="flex flex-col space-y-2 md:space-y-4 p-0 list-none">
<li class="p-4 bg-gray-800 rounded-xl">Item A</li>
<li class="p-4 bg-gray-800 rounded-xl">Item B</li>
<li class="p-4 bg-gray-800 rounded-xl">Item C</li>
</ul>
<!-- Responsive override: space on mobile, switch to gap on desktop -->
<div class="flex items-center space-x-3 lg:space-x-0 lg:gap-5">
<iconify-icon icon="solar:home-linear" width="20"></iconify-icon>
<iconify-icon icon="solar:bell-linear" width="20"></iconify-icon>
<iconify-icon icon="solar:user-linear" width="20"></iconify-icon>
</div>
Space Between vs Gap vs Margin — When to Use Each
Tailwind offers three ways to add spacing between elements. They look interchangeable at a glance but have different mechanisms, constraints, and ideal contexts.
| Utility | Mechanism | flex-wrap safe? | Best for |
|---|---|---|---|
space-x-* |
margin-left on every child except first | ✗ No — wrapped rows indent incorrectly | Single-row toolbars, navbars, button groups ★ |
space-y-* |
margin-top on every child except first | ✓ Yes — always single column | Stacked lists, form fields, sidebar menus ★ |
gap-x-* |
column-gap on flex/grid parent | ✓ Yes | Wrapping card grids, tag lists, any multi-row layout ★ |
gap-y-* |
row-gap on flex/grid parent | ✓ Yes | Vertical rows in grid or wrapped flex |
gap-* |
row-gap + column-gap together | ✓ Yes | Grid card layouts — uniform spacing in both axes ★ |
mt-* / mb-* / ml-* |
margin on individual elements | ✓ Yes | One-off overrides, asymmetric spacing, specific children |
<!-- ✓ space-x: single-row, non-wrapping button group -->
<div class="flex items-center space-x-3">
<button>Bold</button>
<button>Italic</button>
<button>Underline</button>
</div>
<!-- ✓ gap: card grid that wraps across rows -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
<div class="p-6 bg-white rounded-xl shadow">Card 1</div>
<div class="p-6 bg-white rounded-xl shadow">Card 2</div>
<div class="p-6 bg-white rounded-xl shadow">Card 3</div>
</div>
<!-- ✓ space-y: stacked sidebar nav -->
<nav class="flex flex-col space-y-1 p-4 bg-gray-900 rounded-xl">
<a href="#" class="px-3 py-2 rounded-lg text-gray-300 hover:bg-gray-700">Dashboard</a>
<a href="#" class="px-3 py-2 rounded-lg text-gray-300 hover:bg-gray-700">Courses</a>
<a href="#" class="px-3 py-2 rounded-lg text-gray-300 hover:bg-gray-700">Settings</a>
</nav>
<!-- ✗ DON'T: space-x with flex-wrap (first wrapped item gets margin-left) -->
<div class="flex flex-wrap space-x-3"> <!-- ✗ broken --> </div>
<!-- ✓ DO: use gap instead -->
<div class="flex flex-wrap gap-3"> <!-- ✓ correct --> </div>
Quick Reference — All Space Between Utilities
| Class | CSS Property | Notes |
|---|---|---|
space-x-0 |
margin-left: 0 on siblings |
Remove horizontal spacing |
space-x-px |
margin-left: 1px |
1px between items |
space-x-1 |
margin-left: 0.25rem |
4px micro spacing ★ |
space-x-2 |
margin-left: 0.5rem |
8px compact ★ |
space-x-3 |
margin-left: 0.75rem |
12px ★ |
space-x-4 |
margin-left: 1rem |
16px — most common ★★ |
space-x-6 |
margin-left: 1.5rem |
24px comfortable ★ |
space-x-8 |
margin-left: 2rem |
32px generous |
space-x-reverse |
Flips to margin-right | Pair with flex-row-reverse ★ |
space-x-[Npx] |
Custom value | Arbitrary: space-x-[13px] |
space-y-0 |
margin-top: 0 on siblings |
Remove vertical spacing |
space-y-1 |
margin-top: 0.25rem |
4px vertical |
space-y-2 |
margin-top: 0.5rem |
8px tight list ★ |
space-y-4 |
margin-top: 1rem |
16px standard stack ★★ |
space-y-6 |
margin-top: 1.5rem |
24px section stacking ★ |
space-y-reverse |
Flips to margin-bottom | Pair with flex-col-reverse ★ |
space-y-[Npx] |
Custom value | Arbitrary vertical |
sm:space-x-* |
At ≥640px | Responsive prefix ★ |
★ = commonly used • ★★ = used on nearly every project
- space-x-* is a parent utility — write one class on the container and Tailwind adds equal horizontal spacing between every direct child automatically. No classes needed on the children themselves.
-
CSS adjacent-sibling selector —
> * + *adds margin to every child except the first one. No gap before the first item, no trailing gap after the last — exactly what you want for toolbars and button groups. -
Same 4px scale as p-* and m-* —
space-x-1 = 4px,space-x-4 = 16px,space-x-8 = 32px. The formula is identical: class number × 4 = pixels. -
space-y-* for vertical stacking — adds
margin-topinstead ofmargin-left. Ideal for stacked lists, sidebar menus, and form field groups on aflex-colparent. -
flex-wrap caveat — use gap-* instead — because
space-x-*uses a DOM-order selector, the first item on any wrapped line still receives margin-left. This causes unwanted indentation. Usegap-*(Chapter 18) whenever items can wrap. -
space-x-reverse with flex-row-reverse — always
pair them together.
space-x-reversesets the CSS custom property that flips the margin from left to right, ensuring gaps appear on the correct side in reversed containers. -
Responsive — prefix any class with a
breakpoint:
space-x-2 sm:space-x-4 lg:space-x-6gives compact gaps on mobile scaling up to generous spacing on desktop with no media queries written. -
Next chapter — Gap covers
gap-*,gap-x-*, andgap-y-*— the flex-wrap-safe alternative that uses CSScolumn-gapandrow-gapproperties directly on the container.
Quick Check — Questions & Answers
Test your understanding. Click a question to reveal the answer.
space-x-* uses the CSS adjacent-sibling
combinator (> * + *) to add
margin-left to every direct child except the
first. It works on any container — flex or block — with no
display requirement.
gap-x-* sets the CSS
column-gap property directly on a
flex or grid container. It
handles flex-wrap correctly: the first item
on a wrapped row gets no gap, because
column-gap is a grid concept, not a margin.
Use space-x-* for single non-wrapping rows.
Use gap-x-* (or gap-*) whenever
items may wrap to multiple lines.
Tailwind generates a single CSS rule targeting
.space-x-4 > * + * — every direct child
immediately preceded by another child. The rule uses a CSS
custom property:
--tw-space-x-reverse: 0; margin-right: calc(1rem *
var(--tw-space-x-reverse)); margin-left: calc(1rem *
calc(1 - var(--tw-space-x-reverse)));
By default this gives
margin-left: 1rem (16px) and
margin-right: 0. When you also add
space-x-reverse, the custom property becomes
1, flipping to
margin-right: 1rem and
margin-left: 0.
The > * + * selector works purely on DOM
order — it has no knowledge of how items visually arrange
on screen. When items wrap to a new row, the first item on
that row is still the second (or third, etc.) child in the
DOM, so it receives margin-left.
This creates unwanted left-indentation on wrapped rows. CSS has no mechanism to detect "first on a new visual line" — only grid gap properties can do that implicitly.
Solution: always use gap-x-* or
gap-* when items wrap.
Reserve space-x-* for containers where items
are guaranteed to stay on one row (e.g.
flex without flex-wrap, or
overflow-x: auto).
space-x-reverse is a companion class that
sets the CSS custom property
--tw-space-x-reverse: 1, which causes the
space-x-* rule to apply
margin-right instead of
margin-left.
You need it when your flex container uses
flex-row-reverse. In a reversed row, the
visual "first" item is the last in DOM order. Without
space-x-reverse, gaps appear to the left of
items that need them on their right.
Always pair:
flex-row-reverse space-x-4 space-x-reverse.
The same logic applies to
space-y-reverse with
flex-col-reverse: it switches the margin to
margin-bottom instead of
margin-top.
Use space-x-* when you have a single-row flex container and want uniform horizontal gaps with zero changes to child elements — classic use cases are button groups, navigation link lists, and icon toolbars.
Use space-y-* for single-column flex or block stacks — sidebar menus, form fields, or any vertical list where items don't wrap.
Use gap-* whenever items may wrap, or when you're using a grid container. It's also the preferred choice in Tailwind v4+ projects since gap properties are more predictable overall.
Use individual margins (mt-*, mb-*, ml-*) for one-off spacing or when different children need different amounts of space.
MCQ Quiz — Test Yourself
Select the best answer. Correct answers are revealed on click.
space-x-4
to add spacing between children?
space-x-4
use to target children?
flex-row-reverse, which class pairing is correct?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.