Tailwind CSS Space Between
Add spacing between child elements in Tailwind CSS with space-x-* and space-y-* utilities. Alternative to gap for flex and flow layouts.
The space between utilities in Tailwind CSS add consistent spacing between child elements using margin. Instead of adding margin to each child individually, you apply space-x-* or space-y-* to the parent and Tailwind distributes the spacing automatically via adjacent sibling selectors. It is a convenient alternative to gap that works in flow layouts, not just flex and grid.
How to Use Tailwind Space Between
Apply space classes to a parent element to add margins between its direct children:
space-x-{size}- Adds horizontal margin between children (left margin on all but the first).space-y-{size}- Adds vertical margin between children (top margin on all but the first).space-x-reverse- Reverses the direction of horizontal spacing for RTL layouts.space-y-reverse- Reverses the direction of vertical spacing for reversed flex columns.
<div class="flex space-x-4"> <div class="bg-blue-500 text-white p-4 rounded">One</div> <div class="bg-blue-500 text-white p-4 rounded">Two</div> <div class="bg-blue-500 text-white p-4 rounded">Three</div></div>Preview
Vertical Spacing with space-y
Use space-y-* for stacked layouts where children are arranged vertically:
<div class="space-y-4"> <div class="bg-indigo-100 text-indigo-800 p-4 rounded-lg">First block</div> <div class="bg-indigo-100 text-indigo-800 p-4 rounded-lg">Second block</div> <div class="bg-indigo-100 text-indigo-800 p-4 rounded-lg">Third block</div></div>Preview
Space Between vs Gap
Both space-* and gap-* add spacing between elements, but they work differently:
- gap uses the CSS
gapproperty and only works in flex and grid containers. It handles wrapping gracefully. - space uses margin on child elements via adjacent sibling selectors. It works in any layout but doesn't account for wrapped items.
<!-- Using gap (handles wrapping) --><div class="flex flex-wrap gap-4"> <div class="bg-green-200 px-4 py-2 rounded">gap</div> <div class="bg-green-200 px-4 py-2 rounded">handles</div> <div class="bg-green-200 px-4 py-2 rounded">wrapping</div></div>
<!-- Using space (margin-based) --><div class="flex flex-wrap space-x-4"> <div class="bg-amber-200 px-4 py-2 rounded">space</div> <div class="bg-amber-200 px-4 py-2 rounded">may not</div> <div class="bg-amber-200 px-4 py-2 rounded">wrap well</div></div>Preview
gap-4 (flex with wrap)
space-x-4 (margin-based)
Use gap when you have flex or grid layouts with wrapping. Use space for simple stacks or inline elements in flow layout.
Practical Pattern: Form Layout
<form class="max-w-md space-y-5"> <div> <label class="block text-sm font-medium text-gray-700 mb-1">Full Name</label> <input type="text" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="John Doe" /> </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1">Email</label> <input type="email" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="john@example.com" /> </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1">Message</label> <textarea class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500" rows="3" placeholder="Your message..."></textarea> </div> <button class="bg-blue-600 text-white px-6 py-2 rounded-lg text-sm font-semibold hover:bg-blue-700">Submit</button></form>Preview
Practical Pattern: Tag / Chip Row
<div class="flex space-x-2"> <span class="bg-blue-100 text-blue-700 text-xs font-medium px-3 py-1 rounded-full">React</span> <span class="bg-green-100 text-green-700 text-xs font-medium px-3 py-1 rounded-full">Vue</span> <span class="bg-purple-100 text-purple-700 text-xs font-medium px-3 py-1 rounded-full">Svelte</span> <span class="bg-orange-100 text-orange-700 text-xs font-medium px-3 py-1 rounded-full">Angular</span></div>Preview
Negative Space
You can use negative space values to overlap elements:
<div class="flex -space-x-3"> <div class="w-10 h-10 rounded-full bg-blue-500 border-2 border-white flex items-center justify-center text-white text-xs font-bold">A</div> <div class="w-10 h-10 rounded-full bg-green-500 border-2 border-white flex items-center justify-center text-white text-xs font-bold">B</div> <div class="w-10 h-10 rounded-full bg-red-500 border-2 border-white flex items-center justify-center text-white text-xs font-bold">C</div> <div class="w-10 h-10 rounded-full bg-purple-500 border-2 border-white flex items-center justify-center text-white text-xs font-bold">D</div></div>Preview
Negative spacing creates the classic avatar stack pattern seen in many apps.
Responsive Spacing
Adjust spacing at different breakpoints:
<div class="space-y-2 md:space-y-4 lg:space-y-6"> <div class="bg-gray-200 p-4 rounded">Compact on mobile</div> <div class="bg-gray-200 p-4 rounded">Medium on tablet</div> <div class="bg-gray-200 p-4 rounded">Spacious on desktop</div></div>Preview
Arbitrary Value Usage
Use bracket notation for custom spacing values:
<div class="space-y-[18px]"> <p>Custom 18px vertical spacing</p> <p>Between these paragraphs</p></div>
<div class="flex space-x-[0.625rem]"> <span>Custom horizontal spacing</span> <span>Using rem values</span></div>Tailwind Space Between Class Table
| Class | Properties |
|---|---|
| space-x-0 | margin-left: 0px; |
| space-x-0.5 | margin-left: 0.125rem; |
| space-x-1 | margin-left: 0.25rem; |
| space-x-2 | margin-left: 0.5rem; |
| space-x-3 | margin-left: 0.75rem; |
| space-x-4 | margin-left: 1rem; |
| space-x-5 | margin-left: 1.25rem; |
| space-x-6 | margin-left: 1.5rem; |
| space-x-8 | margin-left: 2rem; |
| space-x-10 | margin-left: 2.5rem; |
| space-x-12 | margin-left: 3rem; |
| space-y-0 | margin-top: 0px; |
| space-y-0.5 | margin-top: 0.125rem; |
| space-y-1 | margin-top: 0.25rem; |
| space-y-2 | margin-top: 0.5rem; |
| space-y-3 | margin-top: 0.75rem; |
| space-y-4 | margin-top: 1rem; |
| space-y-5 | margin-top: 1.25rem; |
| space-y-6 | margin-top: 1.5rem; |
| space-y-8 | margin-top: 2rem; |
| space-y-10 | margin-top: 2.5rem; |
| space-y-12 | margin-top: 3rem; |
| space-x-reverse | --tw-space-x-reverse: 1; |
| space-y-reverse | --tw-space-y-reverse: 1; |
Worth Reading
- Tailwind Gap - The modern CSS gap alternative
- Tailwind Margin - Manual margin utilities
Windframe Tailwind blocks
Windframe is an AI visual editor for rapidly building stunning web UIs & websites
Start building stunning web UIs & websites!

