CSS Flexbox Gap Explained with Examples

CSS Flexbox and CSS Grid are both powerful layout tools. Flexbox handles one-dimensional alignment, while CSS Grid covers two-dimensional layouts with rows and columns.

When you need spacing between elements in a flex layout, margins can get messy fast. The CSS gap property (shorthand for row-gap and column-gap) solves this cleanly.

Most developers know gap from CSS Grid, but it works just as well on flex containers. I’ve been using it in production for years and it’s supported across all modern browsers.

Spacing Elements in a Flex Container with CSS Gap

CSS Gap works with CSS Grid, but there are many cases where you need spacing without a defined grid – a flex container, for example.

<div class="flex-gap">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>
.flex-gap {
  display: flex;
  flex-wrap: wrap;
}

The flex-wrap property will allow those flex items to wrap in a row according to the parent element’s width.

Use the demo to compare no spacing, margins on the items, and gap on the container. Narrow the container to see what happens when the items wrap.

Demo 1: margin vs gap
12px
100%
1
2
3
4
5
6
Container edge to first item: 0pxBetween items: 0px

Spacing with Margins

If we want to add spacing between flex items, we can simply add margin to each of the elements:

.flex-gap {
  display: flex;
  flex-wrap: wrap;
}

.flex-gap > div {
  margin: 6px;
}

Margins work, but they don’t produce the same result as gap. Pick margin on items in the demo and watch the readout: the space spills out to the edges of the container.

A key difference: margin is applied to individual flex items, while gap is applied to the flex container itself. Gap only creates space between items, never on the outer edges.

Spacing with Gap

With gap, spacing applies only between elements, not between an element and its container. Here’s the same container using gap:

.flex-gap {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

Switch the demo over to gap on container. The edge readout drops to 0px, and the space appears only between the items.

You can also pass two values. The first is row-gap (between wrapped lines), and the second is column-gap (between items on a line):

.flex-gap {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
}

Or use the longhand properties directly:

.flex-gap {
  display: flex;
  flex-wrap: wrap;
  row-gap: 12px;
  column-gap: 24px;
}

Gap with Wrapped Flex Layouts

When flex items wrap to multiple lines, gap handles both axes. The column-gap controls horizontal spacing between items on the same line, while row-gap controls vertical spacing between wrapped lines.

This is where gap pulls ahead of margin. With margins, you’d need to calculate which items are on the last row and remove their bottom margin. With gap, there’s nothing to calculate:

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px; /* 8px between rows, 12px between items */
}

One thing to watch: gap doesn’t affect how flex-basis is calculated. If you want exactly 3 items per row with a 20px gap, you need to account for the gaps yourself:

.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.card-grid > * {
  /* 3 items per row: subtract 2 gaps, divide by 3 */
  flex-basis: calc((100% - 40px) / 3);
}

gap with flex-direction: column

With a column container, the two values switch roles. MDN puts it this way:

“For columns, the first value is the gap between items within a line, and the second is the gap between lines.” (MDN, gap)

.stack {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: flex-start;
  height: 240px;
  gap: 8px 24px; /* 8px between items, 24px between columns */
}

The align-content: flex-start line matters. Without it, the wrapped columns stretch across the container, and the space between them can grow well past 24px.

Gap with justify-content

When you combine gap with distributed alignment like justify-content: space-between, the gap value acts as a minimum gutter. The browser first ensures the gap is at least the size you specified, then distributes any leftover space according to the alignment rule.

.nav {
  display: flex;
  justify-content: space-between;
  gap: 16px;
}

If the container is wide enough, items spread out beyond 16px. If it’s tight, items will never get closer than 16px. This combination is useful for navigation bars and toolbars where you want even distribution but need a guaranteed minimum spacing.

gap in Tailwind and Bootstrap

Both frameworks include utility classes that compile to the same gap property, so everything in this post applies to them too.

In Tailwind, gap-4 sets gap, gap-x-4 sets column-gap, and gap-y-4 sets row-gap. Each step is a multiple of the --spacing variable.

<div class="flex flex-wrap gap-x-6 gap-y-2">...</div>

In Bootstrap 5.3, use gap-0 to gap-5 on a d-flex or d-grid container, plus row-gap-* and column-gap-* for one axis.

<div class="d-flex flex-wrap gap-3">...</div>

Browser Support

gap for Flexbox is supported across all major browsers. Firefox shipped it first in October 2018, Chrome followed in July 2020, and Safari added it in April 2021. The property is also flow-relative, meaning it adapts automatically to different writing modes – handy if you work with CSS logical properties.

Browser gap in Flexbox since
Chrome and Edge 84
Firefox 63
Safari 14.1
Safari on iOS 14.5

According to caniuse, global support is about 97% of users (checked November 2026).
You might think about using @supports to check for gap support in Flexbox:

@supports (display: flex) and (gap: 10px) {
  .container {
    display: flex;
    gap: 10px;
  }
}

There’s a catch: @supports (gap: 10px) returns true if the browser supports gap in any layout context (including Grid). It can’t distinguish between Grid-only and Flexbox support. In practice this no longer matters since all current browsers support both, but it’s worth knowing.

Percentage gap gotcha: a percentage like gap: 10% on a flex container with no explicit height makes the row-gap resolve to 0.

Percentages in row-gap are calculated against the container’s height. When that height comes from the content, the browser can’t resolve it, so stick to px or rem unless the container has a set height.

Flex Gap Not Working?

When gap appears to do nothing, check these in order:

  • The container has display: flex (or inline-flex). gap does nothing on a regular block.
  • The items are direct children of that container. gap doesn’t extend to grandchildren.
  • You’re not using a percentage row-gap on a container without a set height. In my test, row-gap: 10% in a column container with no height came out as 0px.
  • The value isn’t negative. gap only accepts zero or positive lengths, so gap: -5px is ignored.
  • The browser is recent enough. Safari before 14.1 (iOS before 14.5) ignores gap in Flexbox.

Gap Decorations: Lines Between Flex Items

Gap decorations let you draw lines inside the gaps without extra elements or pseudo-elements. column-rule, which used to work only in multi-column layout, now works in Flexbox and Grid too, and there’s a new row-rule property.

Chrome 149 shipped them without a flag. Firefox and Safari don’t support them yet, and MDN still marks row-rule as experimental.

Demo 2: gap decorations
1
2
3
4
5
6
7
8
9
10

Browsers without support skip the two declarations, so you can add them today without a fallback.

FAQs

What is the difference between gap and margin in Flexbox?
gap creates spacing only between flex items, while margin adds space around every item including the outer edges. With gap you set it once on the container; with margin you typically need to target individual items and handle edge cases (like removing margin from the last item).
Can I use different values for row-gap and column-gap?
Yes. You can either use the shorthand gap: 12px 24px (row-gap first, column-gap second) or set them individually with row-gap: 12px and column-gap: 24px.
Does CSS gap work with flex-wrap?
Yes. When flex items wrap to a new line, gap applies spacing between the wrapped rows as well. The row-gap value controls the vertical space between lines, and column-gap controls the horizontal space between items.
Is gap supported in all browsers?
Yes, in all current browsers: Chrome and Edge 84+, Firefox 63+, Safari 14.1+ and Safari on iOS 14.5+. caniuse puts global support at about 97%, so you don't need a fallback.
Can gap be negative?
No. gap, row-gap and column-gap accept only zero or positive values, so a negative value is ignored. To pull items closer together, use negative margins on the items instead.
What are CSS Gap Decorations?
They let you draw lines inside the gaps between flex or grid items, using column-rule and the new row-rule. Chrome 149 and later support them; Firefox and Safari don't yet.
Why is my flex gap not working?
The usual causes: the container is missing display: flex, the items aren't direct children, the value is negative, or you're using a percentage row-gap on a container with no set height (it resolves to 0). Very old Safari (before 14.1) also ignores it.
Can I use gap and margin together?
Yes. They serve different purposes and stack. Use gap on the container for consistent spacing between items, and margin on individual items when you need extra space around a specific element or want to push an item with margin-left: auto.

Summary

gap puts space between flex items and between wrapped lines, and never on the outer edges. Set it once on the container instead of adding margins to every item.

Watch for percentage row-gap values on containers without a height, and try column-rule and row-rule if you want lines in the gaps.

Discussion
[  1 Comments  ]
  • Ronen
    Ronen 19 December 2024, 16:05

    Thanks for that! Short and to the point 🙂

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!