search

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.

Without any spacing, it looks like this:

1
2
3
4
5
6

And when we narrow the width of the parent element (container) due to the use of flex-wrap:

1
2
3
4
5
6

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;
}
1
2
3
4
5
6

And here’s how it looks when we narrow the width of the parent element:

1
2
3
4
5
6

The margin works, but it doesn’t produce the same result as CSS Gap. Notice the extra spacing around the outer edges of the container – that’s the margin bleeding outward.

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, the spacing applies only between elements – not between an element and its container. The difference becomes obvious in the next example:

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

And it looks like this:

1
2
3
4
5
6

1
2
3
4
5
6

You can also pass two values to control row and column spacing separately:

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

Or use the longhand properties directly:

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

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.

Data on support for the flexbox-gap feature across the major browsers from caniuse.com

You might think of using @supports to detect 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.

What’s Next: Gap Decorations

A new CSS feature called Gap Decorations is being developed as a W3C Working Draft. It extends the existing column-rule property to work with Grid and Flexbox, and introduces a new row-rule property.

This will let you add visual separators (lines, patterns) between items without extra DOM elements or pseudo-elements. Chrome and Edge 139+ already support it behind the “Experimental Web Platform features” flag.

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?
gap for Flexbox is supported in all modern browsers. Firefox added support in October 2018, Chrome in July 2020, and Safari in April 2021. No polyfills or fallbacks are needed.
Can I use gap with CSS Grid and Flexbox at the same time?
The gap property works identically in both Grid and Flexbox - same syntax, same behavior. If you switch a container from display: grid to display: flex (or vice versa), the gap value carries over without changes.
What are CSS Gap Decorations?
Gap Decorations is a new CSS feature (W3C Working Draft) that lets you add visual rules (lines, patterns) inside gaps between flex or grid items. It extends column-rule to Grid and Flexbox and introduces a new row-rule property. Currently available behind a flag in Chrome and Edge 139+.

That covers the CSS gap property for Flexbox. Once you start using it, you won’t go back to margin-based spacing.

Join the Discussion
1 Comments  ]
  • 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!

Savvy WordPress Development official logo