This is a quick CSS tutorial to show you how to create a menu list using either the CSS border style or a background image. The trick is to apply a bottom border to the <li> element, then use the absolute position property to shift the nested elements down to cover the border. It is very flexible — you can easily change the layout by altering the border or background image. It even works when the browser's font size is being scaled (increased or decreased).
1. HTML Code
Take a look at the HTML code and the diagram. They will help you understand how the CSS work.
<ul>
<li><strong>CSS Design</strong> <em>250<sup>95</sup></em></li>
</ul>

2. CSS Code
The key points are:
- Specify the
<li>element toposition:relativeand apply a bottom border style. - Use
position:absolutewith negativebottomvalue to shift the<strong>and<em>element below the border. - Remember: use relative value (em) to control the padding space.
.menu {
width: 500px;
list-style: none;
margin: 0 0 2em;
padding: 0;
font: 150%/100% Arial, Helvetica, sans-serif;
}
.menu li {
clear: both;
margin: 0;
padding: 0 0 1.8em 0;
position: relative;
border-bottom: dotted 2px #999;
}
.menu strong {
background: #fff;
padding: 0 10px 0 0;
font-weight: normal;
position: absolute;
bottom: -.3em;
left: 0;
}
.menu em {
background: #fff;
padding: 0 0 0 5px;
font: 110%/100% Georgia, "Times New Roman", Times, serif;
position: absolute;
bottom: -.2em;
right: 0;
}
.menu sup {
font-size: 60%;
color: #666;
margin-left: 3px;
}
3. Change Border Style
You can easily change the style by editing the CSS border and padding for the <li> element.
li {
border-bottom: dashed 1px #000;
padding: 0 0 2.3em 0;
}
4. Use Image as Border (see final demos)
You can also use a background image.
li {
background: url(images/circle.gif) repeat-x left bottom;
}
5. IE6 Version (see IE6 demo)
If you are still using the old and buggie IE6 browser, you may notice the layout doesn't render properly. To fix the issue, simply add the clearfix trick to the <li> element.
/* clearfix */
.menu li:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.menu li {display: inline-block;}
/* Hides from IE-mac \*/
* html .menu li {height: 1%;}
.menu li {display: block;}
/* End hide from IE-mac */
That’s incredibly clever – thanks for that neat little trick.
seems like there is no way around to learn CSS ;)
Clean, sweet, useful and well-explained. That’s what we expect and what we got – Well done Nick, thanks for sharing :) .
Hey Nick!
I’ve used kind of the same trick a few month ago on a friends restaurants website.
Get a bit trickier, when it comes to two-lined List-Elements.
You can check out what I mean over at http://www.il-bistrorante.de
See the Source-Code for my solution…
All the best from bonn, germany,
Oliver
(The restaurant is in Bonn, greetings from Cologne!;)
Very interesting. Will see it later.
Greetings from France (Marseille, la famille !)
you’r attention to detail is outstanding, much appreciated!
Neat trick! I will definitely be trying that out sometime soon.
Under section 5, if you’re only serving up the clearfix code for IE6, why bother with the
menu li:afterrule that IE6 won’t understand and other browsers (presumably) won’t need?In fact, all you need to do to make it work in IE6 (I don’t know what this does on IE-Mac, but does anybody really use that browser any more?) is this:
.menu li {display: inline-block;}.menu li {display: block;}
That code should do nothing at all, but it clears the display bug that otherwise messes up IE6′s display of your clever code. God (and Bill) alone knows why.
thanks for the tut
Very nice! As always well explained tutorials.
Very nice end product!
I was just describing to a client how this would not work in the web world. I enjoy eating my words. Excellent tutorial!
Well … just one remark on this otherwise great tut.
Tables are not dead.
Of course not as flixible as this one.
Just wanted to say there is still use for tables in displaying … tables :-)
Henning
great idea and tutorial as always!
A weakness of this technique would be difficulty putting this on a page with an organic or pattern background because of the background color needed to offset the dotted line.
Very Nice and Easy Tutorial Nick…. Thanks For sharing such a useful stuff !!
DKumar M.
Can you explain why ur using :after on IE, when it doesnt understand it at all?
Nice, simple effect. A bit of a beginner’s tutorial, but hey – they need tutorials too! Thanks!
On my IE7 browser, some of the numbers were being cut off at the bottom? They display find on Firefox and Chrome though, nice tutorial! Thanks :)