Tutorials Tailwind CSS Space Between
Ch 17 / 45
Chapter 17 Beginner 4 min read Tailwind CSS

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.

Advertisement
Google Ad Placeholder · 728×90 Leaderboard
What you'll learn
What space-x-* and space-y-* do and why they exist
Which CSS selector Tailwind generates — why no margin appears after the last item
Apply uniform horizontal or vertical sibling spacing with a single parent class
Use space-x-reverse and space-y-reverse with reversed flex containers
Apply responsive spacing — different gaps at each breakpoint with no media queries
Know when to choose 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-* (horizontal) vs space-y-* (vertical) — single parent class each class="flex space-x-4" on parent Item 1 no ml (first) ml-4 Item 2 ml-4 Item 3 ml-4 Item 4 no trailing gap ✓ class="flex flex-col space-y-4" on parent Item A — no mt (first) mt-4 added Item B mt-4 added Item C Key behaviours ✓ Apply to parent — affects all direct children ✓ No gap before first or after last item ✓ Same 4px scale as p-* and m-* ✓ Works with flex and block children ✗ Breaks with flex-wrap (use gap-* instead)
Parent Utility
Write space-x-4 once on the container. Tailwind spaces every direct child automatically — no class needed on the children themselves.
No Trailing Gap
The > * + * CSS selector applies margin only to children preceded by another sibling — the first item never gets margin, and nothing appears after the last.
Same 4px Scale
space-x-1 = 4px, space-x-4 = 16px, space-x-8 = 32px. Identical formula to padding and margin: class number × 4 = pixels.
Reverse Variants
Add space-x-reverse alongside space-x-* when using flex-row-reverse — it flips the margin to the right side so gaps appear correctly.
space-x-* vs gap-* space-x-* works on any container (flex or block). gap-* (Chapter 18) requires 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-*.
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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-*
margin-left on every sibling except first — Horizontal gap between flex-row children. space-x-4 = 16px between items. The default choice for toolbars, navbars, and button groups.
space-y-*
margin-top on every sibling except first — Vertical gap between stacked items. space-y-4 = 16px between rows. Use on flex-col containers or block lists instead of adding mb-4 to every child.
space-x-reverse
Companion for flex-row-reverse — Switches margin to margin-right. In a reversed row, the first visual item is last in DOM order; without this, gaps appear on the wrong side. Always pair: space-x-4 space-x-reverse.
space-y-reverse
Companion for flex-col-reverse — Switches margin to margin-bottom for vertically reversed columns. Always pair with flex-col-reverse space-y-*.
space-x-0
Removes horizontal sibling spacing — Useful for resetting at a breakpoint: space-x-4 lg:space-x-0 gives spacing on mobile, removes it on large screens.
space-x-[N]
Arbitrary value — space-x-[13px] or space-y-[1.5rem]. Any valid CSS length inside square brackets overrides the scale for pixel-perfect control.
HTML — space-x and space-y in action
<!-- 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.

Generated CSS — space-x-4 uses the adjacent-sibling combinator .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))); } Selector breakdown > direct children only * any element type + immediately preceded by * another element = NOT first DOM order → visual result with space-x-4 Child 1 no ml ml-4 Child 2 ml-4 Child 3 ml-4 Child 4 ⚠ flex-wrap caveat Items wrapping to a new row still get margin-left applied causing unwanted indentation.
CSS — what Tailwind generates for space-x-4 and space-y-4
/* 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)));
}
The flex-wrap Problem 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.
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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.

space-x-2
all screens (mobile first)
8px between items
sm:space-x-4
≥ 640px
16px between items
lg:space-x-6
≥ 1024px
24px between items
space-y-3 md:space-y-6
vertical grow
12px → 24px top
lg:space-x-0
reset at breakpoint
remove spacing
space-x-[10px]
arbitrary value
exactly 10px
HTML — responsive space-between patterns
<!-- 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>
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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
HTML — choosing the right spacing utility
<!-- ✓ 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>
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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

Featured Course
Master Tailwind CSS — Spacing, Layout & Production UI
From spacing utilities to pixel-perfect dashboards. Covers all Tailwind v4 utilities, custom themes, and 5 real-world projects with live mentoring in Vadodara.
45 Chapters 10+ Hours 5 Real Projects Certificate Live Mentoring
⇄
⭐ 4.9 · 1,200+ students
Tailwind CSS Space Between Cheat Sheet
Free one-page cheat sheet covering all space-x-* and space-y-* utilities, reverse variants, the CSS selector mechanics, and when to use space-* vs gap vs margin.
space-x-* space-y-* space-x-reverse vs gap-* Responsive CSS Selector
Download Free Cheat Sheet
Chapter Summary
  • 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-top instead of margin-left. Ideal for stacked lists, sidebar menus, and form field groups on a flex-col parent.
  • 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. Use gap-* (Chapter 18) whenever items can wrap.
  • space-x-reverse with flex-row-reverse — always pair them together. space-x-reverse sets 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-6 gives compact gaps on mobile scaling up to generous spacing on desktop with no media queries written.
  • Next chapter — Gap covers gap-*, gap-x-*, and gap-y-* — the flex-wrap-safe alternative that uses CSS column-gap and row-gap properties 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.

Question 1 of 4
On which element do you place space-x-4 to add spacing between children?
Question 2 of 4
What CSS selector does space-x-4 use to target children?
Question 3 of 4
Which utility adds equal spacing between vertically stacked flex-col children?
Question 4 of 4
When using flex-row-reverse, which class pairing is correct?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel