Pseudo Menu Items

  • 5 Comments
  •       603 words

Back in September I wrote an article on styling drop caps using pseudo elements. Since then I’ve been thinking about what other elements of a website you could style in a similar fashion.

One such item that has always irritated me when creating a site is what to do with an inline menu system. I’m not sure about you but when I’m rapid prototyping and I’m tackling an inline menu I normally always style them the same way.

<ul>
<li><a href="#">Item One</a></li>
<li>|</li>
<li><a href="#">Item Two</a></li>
<li>|</li>
<li><a href="#">Item Three</a></li>
<li>|</li>
<li><a href="#">Item Four</a></li>
</ul>

with the end result looking like the following

Item One | Item Two | Item Three | Item Four

However I’m sure you will all agree that this is far from ideal. In that example alone we’re sticking in an extra three list items that contribute absolutely nothing other than presentation and as we’re all told, we should be separating content from presentation with the use of css.

So where do the psuedo elements come in? Well, if we strip out the three redundant list items our code will now look like the following

<ul>
<li><a href="#">Item One</a></li>
<li><a href="#">Item Two</a></li>
<li><a href="#">Item Three</a></li>
<li><a href="#">Item Four</a></li>
</ul>

Now to add the presentation through our css

ul li {
list-style:none;
display:inline;
float:left;
margin:0 5px 0 0;
}
ul li:after {
content:"|";
margin:0 0 0 5px;
}
ul li:last-child:after {
content:"";
}

We could also code the last section like

ul li:last-child:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}

But I’m not really sure of the advantages of each method. If anyone can tell me the proper way to do the :last-child example then that would be great.

Anyway I digress, by applying the following css to our code our example menu item now looks exactly like what we were aiming for

Item One | Item Two | Item Three | Item Four

Lets now breakdown the CSS and see what each part plays in order to achieve our final result?

The first section of css will set out a list like normal, removing all list styles, making the menu display inline, floating every list item left to make them all fall in line with each other and finally adding a margin to the right to add more spacing to the list items.

In the next section we make use of the :after pseudo element on the list elements. We use the content property to add the vertical line after each menu option and adding a margin to the left to make sure that the vertical line is evenly positioned between it’s surrounding li elements.

Within the final section of our css code we make use of two final pseudo elements; :last-child and :after. The :last-child pseudo element will find the last element within any parent element. In the second section of our css we used the content property to add a vertical line after each li but because we are targeting the last element we can remove the vertical line by adding in another content property and leaving it blank and the :after element tells the browser which side of the element we are targeting.

If you liked this article then please feel free to Tweet it
  • Who I am

    My name is Jack Osborne and I am a Glasgow–based designer and writer. You should follow me on Twitter.

5 Responses to “Pseudo Menu Items”

  1. [...] Jack Osborne delved into the realm of pseudo-elements. [...]

  2. Kevin Holesh says:

    I like the idea of this post, but I think the desired effect your looking for would be best accomplished with a border-right rather than using the :after pseudo selector. I’m all for progressive enrichment, but this seems like it would be just as easy using the border property.

    I know it’s a bit nitpicky, but I like to use whatever property is most widely supported whenever I can. Perhaps your example would be better if you were trying to separate a list with a faded bullet point ().

  3. Jack Osborne says:

    Kevin, thanks for the comment.

    Yeah border-right would definately be another way to do, I hadn’t actually thought of that. Messing around with a faded bullet point would be fun to try but probably the most complicated out of the three.

  4. Nenad says:

    I agree with Kevin, but I use a different approach. I add border-left to list items and remove it within li:first-child { border: none }. As Kevin said I like to use whatever property is most widely supported whenever I can, and this doesn’t work only in IE6.

  5. Find Jobs says:

    Cool, there is actually some good points on here some of my readers will maybe find this relevant, will send a link, many thanks.