CSS gap property:

There is a new gap CSS property for multi-column, flexbox, and grid layouts that works in newer browsers now! (See Can I use link 1; link 2). It is shorthand for row-gap and column-gap.

#box {
  display: flex;
  gap: 10px;
}

CSS row-gap property:

The row-gap CSS property for both flexbox and grid layouts allows you to create a gap between rows.

#box {
   display: flex;
   row-gap: 10px;
}

CSS column-gap property:

The column-gap CSS property for multi-column, flexbox and grid layouts allows you to create a gap between columns.

#box {
  display: flex;
  column-gap: 10px;
}

Example:

#box {
  display: flex;
  flex-wrap: wrap;
  width: 200px;
  background-color: red;
  gap: 10px;
}
.item {
  background: gray;
  width: 50px;
  height: 50px;
  border: 1px black solid;
}
<div id='box'>
  <div class='item'></div>
  <div class='item'></div>
  <div class='item'></div>
  <div class='item'></div>
</div>

Answer from Flimm on Stack Overflow
🌐
Bootstrap
getbootstrap.com › docs › 5.3 › utilities › spacing
Spacing · Bootstrap v5.3
This can save on having to add margin utilities to individual children of a grid or flex container. Gap utilities are responsive by default, and are generated via our utilities API, based on the $spacers Sass map. ... <div style="grid-template-columns: 1fr 1fr;" class="d-grid gap-3"> <div class="p-2">Grid item 1</div> <div class="p-2">Grid item 2</div> <div class="p-2">Grid item 3</div> <div class="p-2">Grid item 4</div> </div> Support includes responsive options for all of Bootstrap’s grid breakpoints, as well as six sizes from the $spacers map (0–5).
🌐
Bootstrap
getbootstrap.com › docs › 4.1 › utilities › flex
Flex · Bootstrap
Quickly manage the layout, alignment, and sizing of grid columns, navigation, components, and more with a full suite of responsive flexbox utilities. For more complex implementations, custom CSS may be necessary.
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › utilities › flex
Flex · Bootstrap v5.0
"flex": ( responsive: true, property: flex, values: (fill: 1 1 auto) ), "flex-direction": ( responsive: true, property: flex-direction, class: flex, values: row column row-reverse column-reverse ), "flex-grow": ( responsive: true, property: flex-grow, class: flex, values: ( grow-0: 0, grow-1: 1, ) ), "flex-shrink": ( responsive: true, property: flex-shrink, class: flex, values: ( shrink-0: 0, shrink-1: 1, ) ), "flex-wrap": ( responsive: true, property: flex-wrap, class: flex, values: wrap nowrap wrap-reverse ), "gap": ( responsive: true, property: gap, class: gap, values: $spacers ), "justify-
🌐
Bootstrap
getbootstrap.com › docs › 5.3 › helpers › stacks
Stacks · Bootstrap v5.3
<div class="hstack gap-3"> <input class="form-control me-auto" type="text" placeholder="Add your item here..." aria-label="Add your item here..."> <button type="button" class="btn btn-secondary">Submit</button> <div class="vr"></div> <button type="button" class="btn btn-outline-danger">Reset</button> </div> ... .hstack { display: flex; flex-direction: row; align-items: center; align-self: stretch; } .vstack { display: flex; flex: 1 1 auto; flex-direction: column; align-self: stretch; }
🌐
W3Schools
w3schools.com › bootstrap5 › bootstrap_flex.php
Bootstrap 5 Flex
Text Colors Background Colors BS5 Tables BS5 Images BS5 Jumbotron BS5 Alerts BS5 Buttons BS5 Button Groups BS5 Badges BS5 Progress Bars BS5 Spinners BS5 Pagination BS5 List Groups BS5 Cards BS5 Dropdowns BS5 Collapse BS5 Navs BS5 Navbar BS5 Carousel BS5 Modal BS5 Tooltip BS5 Popover BS5 Toast BS5 Scrollspy BS5 Offcanvas BS5 Utilities BS5 Dark Mode BS5 Flex · BS5 Forms BS5 Select Menus BS5 Checks and Radios BS5 Range BS5 Input Groups BS5 Floating Labels BS5 Form Validation · BS5 Grid System BS5 Stacked/Horizontal BS5 Grid XSmall BS5 Grid Small BS5 Grid Medium BS5 Grid Large BS5 Grid XLarge BS5 Grid XXL BS5 Grid Examples · BS5 Basic Template BS5 Editor BS5 Exercises BS5 Quiz BS5 Syllabus BS5 Study Plan BS5 Interview Prep BS5 Certificate ... The biggest difference between Bootstrap 3 and Bootstrap 4 & 5 is that Bootstrap 5 now uses flexbox, instead of floats, to handle the layout.
🌐
FastBootstrap
fastbootstrap.com › docs › gap
Bootstrap Gap CSS - FastBootstrap
Use gap-{size} to change the gap between both rows and columns in grid and flexbox layouts.
Top answer
1 of 16
1151

CSS gap property:

There is a new gap CSS property for multi-column, flexbox, and grid layouts that works in newer browsers now! (See Can I use link 1; link 2). It is shorthand for row-gap and column-gap.

#box {
  display: flex;
  gap: 10px;
}

CSS row-gap property:

The row-gap CSS property for both flexbox and grid layouts allows you to create a gap between rows.

#box {
   display: flex;
   row-gap: 10px;
}

CSS column-gap property:

The column-gap CSS property for multi-column, flexbox and grid layouts allows you to create a gap between columns.

#box {
  display: flex;
  column-gap: 10px;
}

Example:

#box {
  display: flex;
  flex-wrap: wrap;
  width: 200px;
  background-color: red;
  gap: 10px;
}
.item {
  background: gray;
  width: 50px;
  height: 50px;
  border: 1px black solid;
}
<div id='box'>
  <div class='item'></div>
  <div class='item'></div>
  <div class='item'></div>
  <div class='item'></div>
</div>

2 of 16
687

Before ~20202, Flexbox didn't have anything akin to border-spacing for tables. The answer below is from that time. Now, the gap property fulfills this role and is recommended for this application.


Flexbox doesn't have collapsing margins1. Therefore, achieving what you are asking for is a bit more difficult.

In my experience, the "cleanest" way that doesn't use :first-child/:last-child and works without any modification on flex-wrap:wrap is to set padding:5px on the container and margin:5px on the children. That will produce a 10px gap between each child and between each child and their parent.

JSFiddle Demo

.upper {
  margin: 30px;
  display: flex;
  flex-direction: row;
  width: 300px;
  height: 80px;
  border: 1px red solid;

  padding: 5px; /* this */
}

.upper > div {
  flex: 1 1 auto;
  border: 1px red solid;
  text-align: center;

  margin: 5px;  /* and that, will result in a 10px gap */
}

.upper.mc /* multicol test */ {
  flex-direction: column;
  flex-wrap: wrap;
  width: 200px;
  height: 200px;
}
<div class="upper">
  <div>aaa<br/>aaa</div>
  <div>aaa</div>
  <div>aaa<br/>aaa</div>
  <div>aaa<br/>aaa<br/>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
</div>

<div class="upper mc">
  <div>aaa<br/>aaa</div>
  <div>aaa</div>
  <div>aaa<br/>aaa</div>
  <div>aaa<br/>aaa<br/>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
</div>

Find elsewhere
🌐
Medium
medium.com › @sqkhor › bootstrap-5-perfect-gaps-between-buttons-212fe025fd9f
Bootstrap 5: Perfect Gaps Between Buttons | by Shuqi Khor | Medium
March 22, 2023 - Now, just make the container a Flexbox using .d-flex, and then use .flex-wrap for overflow wrapping and .gap-1 to set the gaps, done!
🌐
Bootstrap
getbootstrap.com › docs › 4.4 › utilities › spacing
Spacing · Bootstrap
Bootstrap includes a wide range of shorthand responsive margin and padding utility classes to modify an element’s appearance.
🌐
GitHub
github.com › twbs › bootstrap › issues › 39705
Flex gap · Issue #39705 · twbs/bootstrap
February 24, 2024 - Flex gap is useful when using flex and supported by modern browsers: https://caniuse.com/flexbox-gap
Author   SharkMachine
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › utilities › spacing
Spacing · Bootstrap v5.0
<div class="d-grid gap-3"> <div class="p-2 bg-light border">Grid item 1</div> <div class="p-2 bg-light border">Grid item 2</div> <div class="p-2 bg-light border">Grid item 3</div> </div> Support includes responsive options for all of Bootstrap’s grid breakpoints, as well as six sizes from the $spacers map (0–5).
🌐
Bootstrap
getbootstrap.com › docs › 5.3 › utilities › flex
Flex · Bootstrap v5.3
Quickly manage the layout, alignment, and sizing of grid columns, navigation, components, and more with a full suite of responsive flexbox utilities. For more complex implementations, custom CSS may be necessary.
🌐
CodePen
codepen.io › emdeoh › pen › NWdmgVq
Bootstrap flexbox and gap utilities
## `display: flex;` <div class="d-flex gap-3 border border-danger mb-4"> <div class="p-2 border border-primary">Flex item</div> <div class="p-2 border border-primary">Flex item</div> </div> ## `display: grid;` <div class="d-grid gap-3 border border-danger"> <div class="p-2 border border-primary">Flex item</div> <div class="p-2 border border-primary">Flex item</div> </div> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet">
🌐
W3Schools
w3schools.com › bootstrap4 › bootstrap_flex.asp
Bootstrap 4 Flex
Use flex classes to control the layout of Bootstrap 4 components.
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › layout › gutters
Gutters · Bootstrap v5.0
Gutters are the padding between your columns, used to responsively space and align content in the Bootstrap grid system. ... Gutters are the gaps between column content, created by horizontal padding.
Top answer
1 of 3
43
  • The gap property only applies to flex-parents, not flex-items (flex-parents use it to control the gap between their child flex-items).

    • (The gap property also applies to display: grid;, but that's off-topic in this QA)
  • To adjust the spacing of flex-items use an overridden margin property instead.

    • For flex-direction: horizontal you'll want to set margin-left and margin-right (or margin-inline-start and margin-inline-end).

    • For flex-direction: vertical you'll want to set margin-top and margin-bottom (or margin-block-start and margin-block-end).

    • To have a larger effective gap then set any positive margin value.

      • e.g. with gap: 5px and margin-left: 1px then the effective gap to the left will be 6px.
    • To have a smaller effective gap then set a negative margin value.

      • e.g. with gap: 5px and margin-left: -1px then the effective gap to the left will be 4px.
    • This approach won't work for items with margin: auto, but that's okay because gap: isn't useful with auto margins anyway.

    • Don't forget that you can also use calc and --custom-properties to make the CSS easier to read, and to mix units (e.g. 3.2em margin with a 10px effective gap adjustment).


In your case, there's 20px on either side of your items, so applying a margin-left of -15px will give you a 5px left gap, and a margin-right of -15px will give you a 5px right gap.

Which looks like this:


Like so:

.container{
  display: flex;
  gap: 20px;
}

.items{
  width: 100px; 
  height: 100px;
  background: red;
  
}

.item-4 {
  background: blue;
  margin-left: -15px; /* Adapt 20px gap to 5px */
  margin-right: -15px; /* Adapt 20px gap to 5px */
}
<div class="container">
  <div class="items item-1"></div>
  <div class="items item-2"></div>
  <div class="items item-3"></div>
  <div class="items item-4"></div>
  <div class="items item-5"></div>
  <div class="items item-6"></div>
</div>

2 of 3
0

One way is, obviously, something along the lines of:

<div class="container">
  <div class="items item-1"></div>
  <div class="items item-2"></div>
  <div>
    <div class="items item-3"></div>
    <div class="items item-4"></div>
  </div>
  <div class="items item-5"></div>
  <div class="items item-6"></div>
</div>
🌐
GitHub
github.com › twbs › bootstrap › issues › 37384
Gap for flexbox · Issue #37384 · twbs/bootstrap
October 27, 2022 - Flexbox has the gap property just as css grid does. And its available across all browsers. Why does bootstrap still use paddings and negative margin hacks...
Author   saltnpixels
🌐
Zero To Mastery
zerotomastery.io › blog › bootstrap-flexbox
Beginners Guide To Flexbox With Bootstrap | Zero To Mastery
March 25, 2024 - Learn how to use Flexbox features for responsive design inside of Bootstrap. I've also included some code examples to show you how easy it is to start using!
🌐
GeeksforGeeks
geeksforgeeks.org › bootstrap-5-flex-auto-margins-with-align-items
Bootstrap 5 Flex Auto margins with align-items | GeeksforGeeks
April 28, 2025 - Bootstrap 5 Flex Auto margins with align-items are used to place the flex items vertically on top or bottom of the container using the Bootstrap classes.