Equal space between flex items

Viewed 82703

Is there a way to put a full unit of space on both sides of all items, including the first and last?

I am trying to find a way to have equal spacing around flexbox children.

In this article it seems like the nearest thing is justify-content: space-around. It says that:

space-around: items are evenly distributed in the line with equal space around them. Note that visually the spaces aren't equal, since all the items have equal space on both sides. The first item will have one unit of space against the container edge, but two units of space between the next item because that next item has its own spacing that applies.

6 Answers

This is a perfect use case for flex-basis and justify-content: space-between if you know how many components will be in your row beforehand. Specify a flex-basis percentage on your flex-items that totals less than 100% for all items. The leftover percentages will become margins.

No psuedo elements, child selectors or padding/margin.

div {
  display: flex;
  justify-content: space-between;
  height: 100px;


}
span {
  flex-basis: 32%;
  background: red;
}
<div>
  <span></span>
  <span></span>
  <span></span>
</div>

Related