Skip to main content
Syllabus

CSS gap, row-gap and column-gap in Flexbox and Grid

Badar KhalilUpdated October 1, 2026 3 min read

What is the CSS gap property?

The CSS gap property sets the space between flex or grid items. It is a shorthand for row-gap and column-gap. Unlike margin, gap adds space only between items and never on the outer edges, which makes spacing clean, predictable and easy to maintain. It is supported in all modern browsers for both Flexbox and CSS Grid.

Syntax

Code
.container {
  display: flex;
  gap: 16px;              /* row and column */
  gap: 20px 10px;         /* row-gap column-gap */
  row-gap: 20px;
  column-gap: 10px;
}

Properties explained

PropertyControlsExample
gapShorthand for both directionsgap: 16px 24px;
row-gapSpace between rows (vertical in a row layout)row-gap: 20px;
column-gapSpace between columns (horizontal in a row layout)column-gap: 10px;

How the shorthand works

With one value, it applies to both row and column gaps. With two values, the first is row-gap and the second is column-gap. Remember the order: row first, column second.

Allowed units

  • Absolute: px
  • Relative: rem, em, vw, vh
  • Percentages (resolve against the container size, mostly useful in Grid)
  • Functions: calc(), clamp(), min(), max()
  • CSS variables: gap: var(--space-4);

gap with flex-wrap

When flex items wrap, row-gap adds vertical space between lines and column-gap adds horizontal space between items. This replaces the old margin hacks with negative margins and gives perfect spacing on every line.

gap vs margin

Featuregapmargin
Edge spacingNone, only between itemsAdded on every side
Last-item fix neededNoYes (:last-child hacks)
Set onParent containerEach child
Wrapping layoutsCleanNeeds negative margin trick
CollapsingNoVertical margins can collapse (not in flex)

Fluid and responsive gap

Use clamp() to scale spacing smoothly with the viewport:

Code
.grid {
  display: flex;
  flex-wrap: wrap;
  gap: clamp(12px, 2vw, 32px);
}

Using gap in CSS Grid

The same property works for Grid: display: grid; grid-template-columns: repeat(3, 1fr); gap: 24px;. Older names grid-gap, grid-row-gap and grid-column-gap are legacy aliases; use the unprefixed gap in new code.

Browser support

Grid gap has been supported for years. Flexbox gap is supported in Chrome 84+, Firefox 63+, Edge 84+ and Safari 14.1+, so it is safe for modern production projects.

Real-world use cases

  • Card grids and product lists.
  • Navbar link spacing.
  • Form fields stacked in a column.
  • Button groups and icon toolbars.
  • Tag and badge clouds with wrapping.

Common mistakes

  • Expecting gap to add outer padding. It never does; use padding for that.
  • Mixing up the order of the two-value shorthand (row then column).
  • Using gap on a container that is not flex or grid.
  • Adding both gap and margin on children and getting double spacing.
  • Using percentage gaps in flex layouts and getting unexpected results with undefined container size.

Best practices

  • Prefer gap over margin for sibling spacing.
  • Define a spacing scale with CSS variables and reuse it.
  • Use clamp() for fluid spacing on responsive layouts.
  • Use rem units so spacing respects user font-size settings.

Tailwind CSS equivalents

gap-4, gap-x-6, gap-y-2, gap-0. Example: flex flex-wrap gap-x-4 gap-y-2.

CSS gap property, flexbox gap, row-gap column-gap, CSS grid gap, gap vs margin, responsive spacing clamp, Tailwind gap-4, modern CSS spacing 2026, CSS flex gap browser support, design tokens spacing.

Frequently Asked Questions

Does gap work with Flexbox?

Yes, in all modern browsers including Safari 14.1 and above.

What is the order of values in gap: 10px 20px?

10px is row-gap and 20px is column-gap.

Is gap better than margin?

For spacing between siblings, yes. It is cleaner and needs no last-child fixes.

Does gap add space at the container edges?

No. Use padding on the container for edge spacing.

One-click copy

Code blocks ke Copy button se code copy karein. Cheat Sheet mein gap ke sab patterns ready hain.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself CSS
Output

Press Run to execute.

Exercise: Replace Margins with gapHTML

The buttons use margin for spacing. Remove the margin and use the gap property on the container so there are 12px between buttons horizontally and 20px between wrapped lines vertically.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
No margin on buttons; container uses gap: 20px 12px so lines have 20px row spacing and 12px column spacing.

This is a self-check — compare your result with the expected output above.

Quiz: CSS gap Quiz Pass 70%
Loading quiz…

Was this page helpful?