Add a Pipe Separator After Items in an Unordered List Unless That Item Is the Last on a Line

Add a pipe separator after items in an unordered list unless that item is the last on a line

This is possible with flex-box

The keys to this technique:

  • A container element set to overflow: hidden.
  • Set justify-content: space-between on the ul (which is a flex-box) to force its flex-items to stretch to the left and right edges.
  • Set margin-left: -1px on the ul to cause its left edge to overflow the container.
  • Set border-left: 1px on the li flex-items.

The container acts as a mask hiding the borders of any flex-items touching its left edge.

.flex-list {
position: relative;
margin: 1em;
overflow: hidden;
}
.flex-list ul {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
margin-left: -1px;
}
.flex-list li {
flex-grow: 1;
flex-basis: auto;
margin: .25em 0;
padding: 0 1em;
text-align: center;
border-left: 1px solid #ccc;
background-color: #fff;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css" rel="stylesheet"/>
<div class="flex-list">
<ul>
<li>Dogs</li>
<li>Cats</li>
<li>Lions</li>
<li>Tigers</li>
<li>Zebras</li>
<li>Giraffes</li>
<li>Bears</li>
<li>Hippopotamuses</li>
<li>Antelopes</li>
<li>Unicorns</li>
<li>Seagulls</li>
</ul>
</div>

What is the proper way to put divider lines between navigation links?

Pipe characters work, but you could do the same thing with some CSS.

If your markup is like this:

<nav>
<a href="/about.html">About</a>
<a href="/portfolio.html">Portfolio</a>
<a href="/contact.html">Contact</a>
</nav>

You can add some CSS to style the "right side" of each link element to have a border, except for the last element because you don't want a floating divider line on the end of the links.

nav > a {
border-right: solid 1px #eff0f1;
}
nav > a:last-of-type {
border-right: none;
}

This will add a border to the right side of all your nav links, but then overrides the final nav link to not have a right border.

Separators for Navigation

Simply use the separator image as a background image on the li.

To get it to only appear in between list items, position the image to the left of the li, but not on the first one.

For example:

#nav li + li {
background:url('seperator.gif') no-repeat top left;
padding-left: 10px
}

This CSS adds the image to every list item that follows another list item - in other words all of them but the first.

NB. Be aware the adjacent selector (li + li) doesn't work in IE6, so you will have to just add the background image to the conventional li (with a conditional stylesheet) and perhaps apply a negative margin to one of the edges.

Responsive Separator for Horizontal List

You can exploit fact that trailing and line trailing white space automatically collapses: