27

Flexbox's justify-content: space-around makes my list items horizontally evenly spaced. Is there a way to have exactly the same thing with the only difference that the first item on the left has no space on its left? (that is, the list "starts" from the left edge of the container)

TylerH
  • 20,799
  • 66
  • 75
  • 101
drake035
  • 3,955
  • 41
  • 119
  • 229
  • 1
    If this layout is what you're intending to do, it makes no sense to use any of the `space-*` property, because it will evenly distribute all the space in between. If you want to remove the spacing on the left-most item, how do you want the rest of the space to be distributed? This is another question that you will need to ask yourself. Should the remaining space be total width minus the calculated left spacing? – Terry Mar 23 '17 at 12:17

5 Answers5

44

Instead of using justify-content: space-around, use auto margins on the items.

By giving each flex item margin-right: auto, container space will be distributed evenly between items (like justify-content), but the first item will remain at the left border edge.

flex-container[one] {
  display: flex;
  justify-content: space-around;
  border: 1px dashed green;
}

flex-container[one]>flex-item {
  background-color: lightgreen;
}

flex-container[two] {
  display: flex;
  border: 1px dashed red;
}

flex-container[two]>flex-item {
  margin-right: auto;
  background-color: orangered;
}

flex-item {
  width: 50px;
  height: 50px;
}
<code>justify-content: space-around</code>
<flex-container one>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
</flex-container>

<br>

<code>margin-right: auto</code>
<flex-container two>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
</flex-container>

jsFiddle demo

Learn more about flex auto margins here: In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 7
    Genius, simply genius! – HelloWorld May 16 '18 at 19:30
  • 2
    Good stuff. I applied the same principle to a vertical layout using `justify-content: space evenly` with `margin-top: auto` to get the items to stick to the bottom and maintain responsive, even spacing. The gap property just won't work for all screen sizes, and it beats having to write a special rule with a magic number along 5 media queries (which flexbox is supposed to solve!). – PsiKai Feb 27 '22 at 19:22
  • 1
    this is such a great answer I had comment. Simple, explanatory and concise, great work. – Gabriel Belini Jun 09 '22 at 16:24
12

You can use justify-content: space-between, but the last content will have also no space on the right.

A good documentation.

Shiva127
  • 2,413
  • 1
  • 23
  • 27
4

.container {
    display: flex;
    justify-content: space-evenly;
}
.container .item {
    margin: 0 auto 0 0;
}
<div class="container">
    <div class="item">Apple</div>
    <div class="item">Orange</div>
    <div class="item">Pineapple</div>
</div>

Using auto for margin-right items will be "forced" to left.

Programmeur
  • 1,483
  • 4
  • 22
  • 32
3

Another way :

  • Create a "splitter" element with the css attribute flex-grow: 1.
  • Add the CSS attribute display: flex to the wrapper panel.
  • Add the splitters where you need them

Short example below, the splitter elements are marked in red

.main
{
  display: flex;
  
  border: 1px dashed black;
}

.element
{
  width: 50px;
  height: 50px;
  margin: 5px;
  border: 1px solid blue;
}

.splitter
{
  flex-grow: 1;
    
  border: 1px solid red;
  margin: 3px;
}
<div class="main">
  <div class="element">
  </div>
  <div class="element">
  </div>

  <div class="splitter">
  
  </div>

  <div class="element">
  </div>

  <div class="splitter">
  
  </div>
  
  <div class="element">
  </div>
  <div class="splitter">
    
  </div>
</div>
Pascal Goldbach
  • 977
  • 1
  • 15
  • 34
  • Very nice idea! Used this to override non-fully supported "justify-content: space-evenly" on Microsoft Edge 18 – serpaulius Mar 20 '20 at 15:02
0

Use justify-content: space-between instead of space-around to get what you are trying to achieve here.

Refer code:

ul {
  display: flex;
  padding-left: 0;
}

li {
  justify-content: space-between;
  display: flex;
  width: 25%;
}
<ul>
  <li>Apple</li>
  <li>Orange</li>
  <li>Mango</li>
  <li>Pineapple</li>
</ul>
nashcheez
  • 5,067
  • 1
  • 27
  • 53
  • Thanks a lot I appreciate the time you spend on your answer, but Shiva answered first so I accepted his/her answer. – drake035 Mar 23 '17 at 17:14