Gap in Tailwind CSS
gap-* is the
flex-wrap-safe way to add spacing between
children in a flex or
grid container. Unlike
space-x-* (which uses margin
and breaks when items wrap),
gap-* maps directly to CSS
gap — a native browser
property that handles both row and column spacing correctly, even
across multiple wrapped lines. Use
gap-* for uniform spacing in
both directions, gap-x-* for
column gaps only, and
gap-y-* for row gaps only.
gap-* does and why it is the preferred
spacing utility for flex and grid
gap-* is flex-wrap safe — wrapping rows get
correct spacing automatically
gap-x-* for column-only gaps and
gap-y-* for row-only gaps
gap-4 md:gap-6 lg:gap-8 with
no media queries written
What is Gap? — Native Browser Spacing for Flex & Grid
gap-* is a CSS-native
property: it sets the
gap shorthand which controls
both row-gap and
column-gap simultaneously. The
browser applies it between items — not around the outside
edges. There is never extra space before the first item or after
the last. Most importantly, when items wrap to multiple lines,
each new row correctly starts with no indentation — something
space-x-* cannot achieve.
gap-* handles every row
correctly — the first item on a new line never gets unwanted
indentation.
space-x-* cannot do this.
gap-4 sets equal
row-gap and
column-gap
simultaneously. One class replaces what used to need two
separate properties.
gap-* only applies when
display is
flex or
grid. On block elements
it has no effect. For block children, use
space-y-* or
margin.
gap-1 = 4px,
gap-4 = 16px,
gap-8 = 32px. Identical
formula to padding, margin, and space-*: class number ×
4 = pixels.
space-x-* (Chapter 17) when you need spacing on a
non-flex block container, or when the container explicitly has no
wrapping. For all other cases — card grids, tag clouds, nav bars
with wrapping — prefer gap-*.
All Gap Utilities — Complete Reference
Tailwind maps three gap utilities to the corresponding CSS
properties.
gap-* is the shorthand for
both axes. gap-x-* sets only
column gaps, gap-y-* sets only
row gaps. All three follow the 4px scale and accept responsive
prefixes and arbitrary values.
row-gap and
column-gap to the same value in a single class.
gap-4 = 16px between every pair of adjacent items
in both horizontal and vertical directions. The most commonly
used variant.
gap-x-6 gap-y-4 = 24px between columns,
16px between rows.
gap-y-8 = 32px
between rows while columns have no forced gap.
gap-4 lg:gap-0.
gap-[13px],
gap-x-[1.5rem], gap-y-[2vw]. Any
valid CSS length. Use when the standard 4px scale doesn't hit
the exact spacing the design requires.
<!-- gap-*: equal column + row spacing on a grid -->
<div class="grid grid-cols-3 gap-6">
<div>Card A</div>
<div>Card B</div>
<div>Card C</div>
</div>
<!-- gap-6 = column-gap: 24px AND row-gap: 24px -->
<!-- gap-x-* + gap-y-*: different column and row gaps -->
<div class="grid grid-cols-2 gap-x-8 gap-y-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</div>
<!-- gap-x-8 = 32px between columns, gap-y-4 = 16px between rows -->
<!-- gap-y-*: row gap only, no column gap -->
<div class="flex flex-col gap-y-3">
<div class="p-4 bg-gray-800 rounded-xl">Section A</div>
<div class="p-4 bg-gray-800 rounded-xl">Section B</div>
</div>
<!-- Arbitrary value -->
<div class="grid grid-cols-4 gap-[13px]">
<div>Photo 1</div><div>Photo 2</div>
<div>Photo 3</div><div>Photo 4</div>
</div>
Gap with Flexbox — The flex-wrap Fix
The biggest advantage of
gap-* over
space-x-* is its behaviour
with flex-wrap. When flex
items overflow and wrap to a new line,
gap-* correctly spaces every
item in every row — no item ever gets a gap applied on its outer
edge, even when it becomes the first item on a new line.
<!-- ✓ Wrapping tag cloud — gap handles new rows correctly -->
<div class="flex flex-wrap gap-2">
<span class="px-3 py-1 bg-cyan-900 text-cyan-300 rounded-full text-sm">HTML</span>
<span class="px-3 py-1 bg-blue-900 text-blue-300 rounded-full text-sm">CSS</span>
<span class="px-3 py-1 bg-yellow-900 text-yellow-300 rounded-full text-sm">JavaScript</span>
<span class="px-3 py-1 bg-purple-900 text-purple-300 rounded-full text-sm">React</span>
<span class="px-3 py-1 bg-green-900 text-green-300 rounded-full text-sm">Tailwind CSS</span>
<span class="px-3 py-1 bg-red-900 text-red-300 rounded-full text-sm">Node.js</span>
</div>
<!-- ✓ Flex button toolbar — non-wrapping, gap-3 -->
<div class="flex items-center gap-3 p-2 bg-gray-800 rounded-xl">
<button class="px-3 py-1.5 bg-gray-700 rounded-lg text-sm text-white">Bold</button>
<button class="px-3 py-1.5 bg-gray-700 rounded-lg text-sm text-white">Italic</button>
<button class="px-3 py-1.5 bg-gray-700 rounded-lg text-sm text-white">Link</button>
<div class="w-px h-5 bg-gray-600"></div>
<button class="px-3 py-1.5 bg-gray-700 rounded-lg text-sm text-white">Align</button>
</div>
<!-- ✓ Different row and column gaps on flex wrap -->
<div class="flex flex-wrap gap-x-4 gap-y-8">
<div class="w-40 p-4 bg-gray-800 rounded-xl">Item 1</div>
<div class="w-40 p-4 bg-gray-800 rounded-xl">Item 2</div>
<div class="w-40 p-4 bg-gray-800 rounded-xl">Item 3</div>
</div>
<!-- 16px between columns, 32px between wrapped rows -->
Gap with Grid — Column & Row Control
CSS Grid is where gap-* shines
most. A single gap-6 on a grid
container gives every cell equal spacing in both dimensions — no
margin calculations, no clearing hacks. When you need asymmetric
spacing (wider columns, tighter rows), combine
gap-x-* and
gap-y-*.
<!-- Standard card grid: equal gap in both axes -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
<div class="p-6 bg-white dark:bg-gray-800 rounded-2xl shadow-md">
<h3 class="font-bold text-lg mb-2">Course Title</h3>
<p class="text-gray-500 text-sm">Description text.</p>
</div>
<!-- ... more cards -->
</div>
<!-- Dashboard layout: wider columns, tighter rows -->
<div class="grid grid-cols-2 lg:grid-cols-4 gap-x-6 gap-y-4">
<div class="p-4 bg-gray-800 rounded-xl">Total Sales</div>
<div class="p-4 bg-gray-800 rounded-xl">Revenue</div>
<div class="p-4 bg-gray-800 rounded-xl">Students</div>
<div class="p-4 bg-gray-800 rounded-xl">Completion</div>
</div>
<!-- gap-x-6 = 24px between columns; gap-y-4 = 16px between rows -->
<!-- Photo mosaic: 1px gap hairline separator -->
<div class="grid grid-cols-3 gap-px bg-gray-700">
<img src="photo1.jpg" alt="" class="w-full aspect-square object-cover">
<img src="photo2.jpg" alt="" class="w-full aspect-square object-cover">
<img src="photo3.jpg" alt="" class="w-full aspect-square object-cover">
</div>
<!-- gap-px = 1px lines between cells; bg-gray-700 shows through as divider -->
Responsive Gap — Scale Spacing with Screen Size
Every gap utility accepts responsive prefixes. The standard pattern is compact on mobile and increasingly generous as the viewport grows — cards breathe more on large screens while staying compact on phones where space is limited.
<!-- Card grid: scale gap with screen size -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4 sm:gap-6 lg:gap-8">
<div class="p-6 bg-white dark:bg-gray-800 rounded-2xl shadow">Card 1</div>
<div class="p-6 bg-white dark:bg-gray-800 rounded-2xl shadow">Card 2</div>
<div class="p-6 bg-white dark:bg-gray-800 rounded-2xl shadow">Card 3</div>
</div>
<!-- 16px → 24px → 32px as screen grows -->
<!-- Dashboard: responsive asymmetric gap -->
<div class="grid grid-cols-2 lg:grid-cols-4 gap-3 md:gap-x-6 md:gap-y-4">
<div class="p-4 bg-gray-800 rounded-xl text-center">
<p class="text-3xl font-bold text-cyan-400">1,200</p>
<p class="text-sm text-gray-400">Students</p>
</div>
<div class="p-4 bg-gray-800 rounded-xl text-center">
<p class="text-3xl font-bold text-purple-400">45</p>
<p class="text-sm text-gray-400">Chapters</p>
</div>
</div>
<!-- Flex row: no wrapping, responsive gap -->
<div class="flex items-center gap-2 sm:gap-4 lg:gap-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="/resources.html" class="text-gray-300 text-sm">Resources</a>
</div>
Quick Reference — All Gap Utilities
| Class | CSS Property | Notes |
|---|---|---|
gap-0 |
gap: 0 |
Remove all gap |
gap-px |
gap: 1px |
Hairline separator ★ |
gap-1 |
gap: 0.25rem |
4px micro spacing |
gap-2 |
gap: 0.5rem |
8px compact ★ |
gap-3 |
gap: 0.75rem |
12px ★ |
gap-4 |
gap: 1rem |
16px — most common ★★ |
gap-5 |
gap: 1.25rem |
20px |
gap-6 |
gap: 1.5rem |
24px comfortable ★★ |
gap-8 |
gap: 2rem |
32px generous ★ |
gap-10 |
gap: 2.5rem |
40px section gap |
gap-1 … gap-96 |
0.25rem … 24rem | Full 4px scale |
gap-[N] |
Custom value | Arbitrary: gap-[13px] |
gap-x-0 |
column-gap: 0 |
Remove column gap |
gap-x-4 |
column-gap: 1rem |
16px column gap ★ |
gap-x-6 |
column-gap: 1.5rem |
24px column gap ★ |
gap-x-[N] |
Custom column-gap | Arbitrary column gap |
gap-y-0 |
row-gap: 0 |
Remove row gap |
gap-y-4 |
row-gap: 1rem |
16px row gap ★ |
gap-y-6 |
row-gap: 1.5rem |
24px row gap ★ |
gap-y-[N] |
Custom row-gap | Arbitrary row gap |
sm:gap-* |
At ≥640px | Responsive prefix ★ |
md:gap-* |
At ≥768px | Responsive prefix ★ |
lg:gap-* |
At ≥1024px | Responsive prefix ★ |
★ = commonly used • ★★ = used on nearly every project
-
gap-* is the modern default for spacing flex
and grid children. It maps directly to the CSS
gapproperty and handles wrapping, grid, and flex containers all correctly — no hacks needed. -
Flex-wrap safe — unlike
space-x-*,gap-*uses the CSScolumn-gapandrow-gapproperties which the browser applies natively between cells. The first item on a wrapped row never gets unwanted spacing. -
gap-* = both axes —
gap-6sets bothcolumn-gap: 1.5remandrow-gap: 1.5remsimultaneously. One class handles what used to take two properties. -
gap-x-* and gap-y-* for asymmetric — combine
them for different column and row gaps:
gap-x-8 gap-y-4gives 32px between columns and 16px between rows — ideal for dashboards and data tables. -
Flex and grid only —
gap-*has no effect on block containers. For spacing between block children, usespace-y-*ormy-*margin utilities. -
Same 4px scale —
gap-1 = 4px,gap-4 = 16px,gap-6 = 24px,gap-8 = 32px. Identical formula to padding, margin, and space-*. -
Responsive —
gap-4 sm:gap-6 lg:gap-8scales gap with screen size. The most common pattern on card grids scales from compact mobile to generous desktop. -
Next chapter — Width covers
w-*,w-full,w-screen,max-w-*, and fractional widths likew-1/2— the utilities that control how wide an element is.
Quick Check — Questions & Answers
Test your understanding. Click a question to reveal the answer.
gap-* uses the CSS
column-gap and
row-gap properties applied natively by the
browser between flex or grid cells. It works correctly
with flex-wrap, handles both axes
simultaneously, and requires no reverse variants.
space-x-* uses the CSS adjacent-sibling
selector > * + * to add
margin-left to every child except the first.
It works on any container (flex or block), but breaks with
flex-wrap because DOM order and visual row
position diverge.
In Tailwind v4 projects, gap-* is the
preferred default for flex and grid. Only use
space-x-* for non-flex/non-grid containers,
or when you specifically need margin-based spacing.
gap-* works on
both flex and grid containers. You only
need display: flex or
display: grid on the parent — gap properties
are fully supported in both layout modes in all modern
browsers.
On a flex container, gap adds
spacing between flex items in the main axis (and between
wrapped rows in the cross axis). On a
grid container,
column-gap applies between columns and
row-gap between rows.
The only containers where gap-* has no effect
are block elements (display: block) and
inline elements. For those, use space-y-* or
individual margin utilities.
gap-x-* sets only the
column-gap — the horizontal space between
columns in a grid or between items in a flex row.
gap-y-* sets only the
row-gap — the vertical space between rows in
a grid or between wrapped rows in a flex container.
You can combine both: gap-x-8 gap-y-4 gives
32px between columns and 16px between rows.
gap-* alone sets both to the same value
simultaneously.
Use gap-px on the grid container and set the
container's background colour to the separator colour. The
1px gap acts as a window that reveals the background
through the hairlines between cells.
Example:
<div class="grid grid-cols-3 gap-px
bg-gray-700">
with images inside. Each image fills its cell, and the 1px
gap shows the gray background as a thin divider line — an
Instagram-style photo mosaic pattern.
You can also control horizontal and vertical separators
independently: gap-x-px gap-y-2 gives 1px
vertical lines between columns and 8px between rows.
Yes — all gap utilities accept responsive breakpoint prefixes like any other Tailwind class. Tailwind is mobile-first: the unprefixed class applies from 0px up, and breakpoint-prefixed classes activate at that width and above.
The most common pattern for card grids:
gap-4 sm:gap-6 lg:gap-8 — 16px on mobile,
24px on tablet, 32px on desktop. This makes cards feel
compact when stacked vertically and spacious when
displayed in a wider multi-column layout.
You can also make columns and rows responsive
independently:
gap-x-4 lg:gap-x-8 gap-y-4 lg:gap-y-6.
MCQ Quiz — Test Yourself
Select the best answer. Correct answers are revealed on click.
column-gap
and
row-gap
to 16px simultaneously?
gap-*
works correctly with
flex-wrap, unlike
space-x-*. Why?
gap-*?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.