CSS3 Flexbox spacing between items
Asked Answered
E

3

22

Being somewhat new to Flexbox (although experienced in CSS), it seems to me that one thing conveniently "glossed over" by most tutorials I've read is the spacing between flex items.

For example, one of the most cited tutorials is this one at CSS Tricks.

It's very good and has been helpful, diagrams like this have thrown me off:

enter image description here

Notice the spaces between the flex items. Although not mentioned anywhere, nor in the sample code, it would seem the only way to get the spaces is with css margins.

Correct, or am I missing something important here?

Because what i need to create is this, a lot like the "center" demo above: enter image description here

However, when I try it myself, I of course get this: enter image description here

if I use space-around, I get this instead. Huge space. enter image description here

Therefore it seems I need to add margin-right to the first 2 boxes to get 3 centered boxes with a small gap between them.

Is this simply a bad use case for Flexbox? Because I see little advantage creating my 3 boxes with Flexbox over using simple margins and centering.

Am I missing something obvious here?

Entail answered 13/6, 2015 at 3:52 Comment(0)
E
9

Nope - you're not missing anything. Flexbox is terrific for ordering elements and defining the general alignment of those elements along either the main or cross axes, but doesn't speak directly to individual item spacing. If you take a look at this Codepen used in the Flexbox article, you'll notice they use:

margin-top: 10px

to define element spacing. Hope this helps!

Encyclopedia answered 13/6, 2015 at 4:53 Comment(3)
Thanks Ryan, my problem was that the items always had spaces on the left and right sides even with justify-content: space-between. With your answer I noticed I had left a forgotten margin: auto in the items CSS from a previous style. After I removed it, the "flex spacing" worked properly (no more irregular spaces on the sides).Bueno
You'll also notice that that example does not use margin-top to create spacing between flexbox items.Abeyant
Is it flawed to think that with grid-gap, we should have flex-gap, too?Sankhya
T
2

.rope {
  width: 393px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  background-color: aquamarine;
}
.box {
  height: 100px;
  width: 100px;
  margin: 15px;
  background: red;
}
<div class='container'>
  <div class='rope'>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
</div>
Torray answered 13/6, 2015 at 7:17 Comment(0)
C
2

The CSS spec has recently been updated to apply gap properties to flexbox elements in addition to CSS grid elements. This feature is supported on the latest versions of all major browsers. With gap properties, you can get what you want with something like column-gap: 10px (or whatever size you want).

Consortium answered 10/8, 2019 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.