Separators for Navigation

CssNavigationUsabilitySeparator

Css Problem Overview


I need to add separators between elements of navigation. Separators are images.

Separators between elements.

My HTML structure is like: ol > li > a > img.

Here I come to two possible solutions:

  1. To add more li tags for separation (boo!),
  2. Include separator in image of each element (this is better, but it makes possibility that user may click on, example, "Home", but get to "Services", because they are one behind the other and user may accidentally click on separator that belongs to "Services");

What to do?

Css Solutions


Solution 1 - Css

If there isn't a pressing need to use images for the separators, you could do this with pure CSS.

nav li + li:before{
    content: " | ";
    padding: 0 10px;
}

This puts a bar between each list item, just as the image in the original question described. But since we're using the adjacent selectors, it doesn't put the bar before the first element. And since we're using the :before pseudo selector, it doesn't put one at the end.

Solution 2 - Css

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.

Solution 3 - Css

The other solution are OK, but there is no need to add separator at the very last if using :after or at the very beginning if using :before.

SO:

case :after

.link:after {
  content: '|';
  padding: 0 1rem;
}

.link:last-child:after {
  content: '';
}

case :before

.link:before {
  content: '|';
  padding: 0 1rem;
}

.link:first-child:before {
  content: '';
}

Solution 4 - Css

To get the separator vertically centered relative to the menu text,

.menustyle li + li:before {
  content: ' | ';
  padding: 0;
  position: relative;
  top: -2px;
}

Solution 5 - Css

You can add one li element where you want to add divider

<ul>
    <li> your content </li>
    <li class="divider-vertical-second-menu"></li>
    <li> NExt content </li>
    <li class="divider-vertical-second-menu"></li>
    <li> last item </li>
</ul>

In CSS you can Add following code.

.divider-vertical-second-menu{
   height: 40px;
   width: 1px;
   margin: 0 5px;
   overflow: hidden;
   background-color: #DDD;
   border-right: 2px solid #FFF;
}

This will increase you speed of execution as it will not load any image. just test it out.. :)

Solution 6 - Css

Add the separator to the li background and make sure the link doesn't expand to cover the separator, which means the separator won't be click-able.

Solution 7 - Css

For those using Sass, I have written a mixin for this purpose:

@mixin addSeparator($element, $separator, $padding) {
	#{$element+'+'+$element}:before {
	    content: $separator;
	    padding: 0 $padding;
	}
}

Example:

@include addSeparator('li', '|', 1em);

Which will give you this:

li+li:before {
  content: "|";
  padding: 0 1em;
}

Solution 8 - Css

I believe the best solution for this, is what I use, and that is a natural css border:

border-right:1px solid;

You might need to take care of padding like:

padding-left: 5px;
padding-right: 5px;

Finally, if you don't want the last li to have that seperating border, give it's last-child "none" in "border-right" like this:

li:last-child{
  border-right:none;
}

Good luck :)

Solution 9 - Css

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondaGrevisView Question on Stackoverflow
Solution 1 - CssjrueView Answer on Stackoverflow
Solution 2 - CssajcwView Answer on Stackoverflow
Solution 3 - CssT04435View Answer on Stackoverflow
Solution 4 - Cssuser7149870View Answer on Stackoverflow
Solution 5 - CssRajiv PingaleView Answer on Stackoverflow
Solution 6 - CssSoufiane HassouView Answer on Stackoverflow
Solution 7 - Cssd4nyllView Answer on Stackoverflow
Solution 8 - CssAviView Answer on Stackoverflow
Solution 9 - CssFaustView Answer on Stackoverflow