Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dividing line between flex items with equal space distribution

Tags:

html

css

flexbox

I have a list with different items which have auto widths (no fixed width can be given in my case). I use justify-content: space-between because my first item has to start at the beginning of the container and my last item at the end.

All of the above works fine, but whenever I try to add a line between these list items, the problems start to emerge. I have no way to determine how many px or % I have to position these lines. Is there any way to 'dynamically' position the lines between the different list-items or not?

The html we are using is not editable as it is rendered by the CMS we are using.

This is what I have:

This is what I have

This is what I try to achieve

Here is what I would like to achieve

Here is the code I currently have

html {      box-sizing: border-box;  }    .Container {      max-width: 70%;      margin-right: auto;      margin-left: auto;      background: blue;      padding-top: 20px;      padding-bottom: 20px;  }    .Flex {      display: flex;      flex-flow: row wrap;      justify-content: space-between;      list-style: none;      margin: 0;      padding: 0;  }    .Flex-item {      background: red;      position: relative;  }    .Flex-item:after {      content: "";      position: absolute;      background: white;      width: 1px;      height: 40px;      top: 50%;      transform: translateY(-50%);  }
<div class="Container">      <ul class="Flex">           <li class="Flex-item">Lorem</li>           <li class="Flex-item">consectetur</li>           <li class="Flex-item">vestibulum</li>           <li class="Flex-item">nec</li>           <li class="Flex-item">condimentum</li>      </ul>  </div>
like image 732
Nicholas Avatar asked Jan 13 '17 09:01

Nicholas


People also ask

How do I add equal space between Flex items?

In fact, all major browsers consider pseudo-elements on a flex container to be flex items. Knowing that, add ::before and ::after to your container. With justify-content: space-between and zero-width pseudo-elements, the visible flex items will appear evenly spaced.

How do I add a gap between bootstrap Flex items?

justify-content: space-around , justify-content: space-evenly , justify-content: space-between . They provide spacing between elements and should help. Show activity on this post. If you are using bootstrap you need to add class.

Which value should be used to evenly distribute the Flex items within the container?

The value space-between displays equal spacing between flex items. For equal spacing around every flex item, use the value space-around . A margin set to auto will absorb any extra space around a flex item and push other flex items into different positions.

How do you distribute evenly space in CSS?

The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.


2 Answers

I'm using this solution on a project I'm working on.

It sets justify-content: space-between; on the flex container and flex: 1 1 auto; on the children with a left border on all childrens except first.

I modified your example CSS so you can have a look. I wasn't sure if you were going to have background color on the children so I just used line-height to get larger borders.

html {      box-sizing: border-box;  }    .Container {      max-width: 70%;      margin-right: auto;      margin-left: auto;      background: blue;      padding-top: 20px;      padding-bottom: 20px;  }    .Flex {      display: flex;      flex-flow: row wrap;      justify-content: space-between;      list-style: none;      margin: 0;      padding: 0;  }    .Flex-item {      flex: 1 1 auto;      background: red;      position: relative;      text-align: center;      line-height: 40px;  }    .Flex-item + .Flex-item {      border-left: solid 1px white;  }    /** Optional for OPs exact layout */    .Flex-item:first-child {      text-align: left;  }    .Flex-item:last-child {      text-align: right;  }
<div class="Container">      <ul class="Flex">          <li class="Flex-item">Lorem</li>          <li class="Flex-item">consectetur</li>          <li class="Flex-item">vestibulum</li>          <li class="Flex-item">nec</li>          <li class="Flex-item">condimentum</li>      </ul>  </div>

No modification to HTML.

like image 110
Stefan Lindberg Avatar answered Oct 06 '22 15:10

Stefan Lindberg


You can make it work by using a nested flexboxes - I understand you can't change the markup, but at least you have to wrap the contents of the li into a span like I have here:

  1. Make .flex-item also a flexbox with the text in a span (this would have the red background now) and the separator as an :after element

  2. Apply flex-grow and flex-shrink to 1 and flex-basis to auto for the Flex-item.

  3. The flex: 0 to the last Flex-item and margin-auto to the :after also contributes to the effect.

A demo may explain it better - see below:

html {    box-sizing: border-box;  }  .Container {    max-width: 70%;    margin-right: auto;    margin-left: auto;    background: blue;    padding-top: 20px;    padding-bottom: 20px;  }  .Flex {    display: flex;    justify-content: space-between;    list-style: none;    margin: 0;    padding: 0;  }  .Flex-item {    display: flex;    justify-content: space-between;    align-items: center;    flex: 1 1 auto;  }  .Flex-item span {    background: red;  }  .Flex-item:not(:last-child):after {    content: "";    border: 1px solid white;    height: 40px;    margin: auto;  }  .Flex-item:last-child {    flex: 0;  }
<div class="Container">    <ul class="Flex">      <li class="Flex-item">        <span>Lorem</span>      </li>      <li class="Flex-item">        <span>consectetur</span>      </li>      <li class="Flex-item">        <span>vestibulum</span>      </li>      <li class="Flex-item">        <span>nec</span>      </li>      <li class="Flex-item">        <span>condimentum</span>      </li>    </ul>  </div>
like image 24
kukkuz Avatar answered Oct 06 '22 16:10

kukkuz