Ch 18 / 45
Chapter 18 Beginner 4 min read Tailwind CSS

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.

Advertisement
Google Ad Placeholder · 728×90 Leaderboard
What you'll learn
What gap-* does and why it is the preferred spacing utility for flex and grid
Why gap-* is flex-wrap safe — wrapping rows get correct spacing automatically
Use gap-x-* for column-only gaps and gap-y-* for row-only gaps
Apply gap inside flex containers — button groups, nav links, tag clouds
Apply gap inside grid containers — card grids, photo galleries, dashboards
Use responsive gap — 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-* (both axes) vs gap-x-* (columns only) vs gap-y-* (rows only) gap-4 on grid — equal column-gap AND row-gap A col B col C row-gap (16px) D E F class="grid grid-cols-3 gap-4" gap-4 = 16px between every item in both axes — no extra code gap-4 → column-gap: 1rem; row-gap: 1rem gap-x-6 — column gap only 6 column-gap: 1.5rem; row-gap: 0 gap-y-4 — row gap only Row 1 Row 2 gap-y-4 row-gap: 1rem; column-gap: 0
Flex-wrap Safe
When flex items wrap to multiple lines, gap-* handles every row correctly — the first item on a new line never gets unwanted indentation. space-x-* cannot do this.
Both Axes at Once
gap-4 sets equal row-gap and column-gap simultaneously. One class replaces what used to need two separate properties.
Flex & Grid Only
gap-* only applies when display is flex or grid. On block elements it has no effect. For block children, use space-y-* or margin.
Same 4px Scale
gap-1 = 4px, gap-4 = 16px, gap-8 = 32px. Identical formula to padding, margin, and space-*: class number × 4 = pixels.
gap-* vs space-x-* — which to use The modern default for any flex or grid layout is gap-*. It handles wrapping, works equally on flex and grid, and requires no reverse variants. Only use 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-*.
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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.

gap-*
gap: N — Sets both 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-*
column-gap: N — Controls only the horizontal space between columns. Use when you want different column and row gaps: gap-x-6 gap-y-4 = 24px between columns, 16px between rows.
gap-y-*
row-gap: N — Controls only the vertical space between rows. In a flex container with wrap, this is the spacing between each wrapped line. gap-y-8 = 32px between rows while columns have no forced gap.
gap-0
gap: 0 — Removes all gap spacing. Used to reset inherited gaps or to override at a specific breakpoint: gap-4 lg:gap-0.
gap-px
gap: 1px — A 1-pixel gap. Useful for grid dividers where you want a hairline separator between items (e.g. a photo mosaic with 1px borders).
gap-[N]
Arbitrary value — 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.
HTML — all gap variants in action
<!-- 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.

flex-wrap + space-x-* (broken) vs flex-wrap + gap-* (correct) ✗ flex flex-wrap space-x-4 — broken Tag A Tag B Tag C Tag D 16px unwanted indent on Tag D! It's 2nd in DOM but 1st on row 2. space-x-4 > * + * can't know which child starts a new line. flex flex-wrap space-x-4 → Tag D gets ml-4 ✓ flex flex-wrap gap-4 — correct Tag A Tag B Tag C Tag D Tag D starts at left edge ✓ gap-4 uses CSS column-gap — browser handles wrapping natively. flex flex-wrap gap-4 → column-gap: 1rem
HTML — gap in flex containers
<!-- ✓ 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 -->
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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-*.

Grid gap patterns — uniform and asymmetric spacing grid-cols-3 gap-6 — uniform 24px all gaps Card A 24px Card B Card C row-gap 24px Card D Card E Card F gap-6 → column-gap: 1.5rem; row-gap: 1.5rem grid-cols-2 gap-x-8 gap-y-4 — asymmetric Item 1 32px Item 2 (wider) row-gap 16px Item 3 Item 4 (wider) gap-x-8: col-gap 32px; gap-y-4: row-gap 16px dashboard: wide column, tight row spacing
HTML — gap in grid containers
<!-- 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 -->
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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.

gap-3
all screens (mobile first)
12px — compact mobile
sm:gap-4
≥ 640px
16px between items
md:gap-6
≥ 768px
24px comfortable
lg:gap-8
≥ 1024px
32px generous desktop
gap-x-6 gap-y-4
asymmetric
col 24px / row 16px
gap-[13px]
arbitrary value
exact pixel control
HTML — responsive gap patterns
<!-- 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>
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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

Featured Course
Master Tailwind CSS — Grid, Flex & Production UI
From gap utilities to full dashboard layouts. Covers all Tailwind v4 utilities, custom themes, responsive grid systems, and 5 real-world projects with live mentoring.
45 Chapters 10+ Hours 5 Real Projects Certificate Live Mentoring
▦
⭐ 4.9 · 1,200+ students
Tailwind CSS Gap Cheat Sheet
Free one-page cheat sheet covering all gap-*, gap-x-*, gap-y-* utilities, the flex-wrap comparison, responsive gap patterns, and real-world flex and grid examples.
gap-* gap-x-* gap-y-* flex-wrap Grid Layouts Responsive
Chapter Summary
  • gap-* is the modern default for spacing flex and grid children. It maps directly to the CSS gap property and handles wrapping, grid, and flex containers all correctly — no hacks needed.
  • Flex-wrap safe — unlike space-x-*, gap-* uses the CSS column-gap and row-gap properties which the browser applies natively between cells. The first item on a wrapped row never gets unwanted spacing.
  • gap-* = both axes — gap-6 sets both column-gap: 1.5rem and row-gap: 1.5rem simultaneously. 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-4 gives 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, use space-y-* or my-* 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-8 scales 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 like w-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.

Question 1 of 4
Which class sets both column-gap and row-gap to 16px simultaneously?
Question 2 of 4
gap-* works correctly with flex-wrap, unlike space-x-*. Why?
Question 3 of 4
Which class sets ONLY the horizontal space between columns, leaving row gaps unchanged?
Question 4 of 4
Which container types support gap-*?

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