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 */
Great tutorial, its something that can be used for multiple purposes and is very simple to integrate. Raphael raised a good point regarding IE6′s issue with the prices, hopefully there will be a fix posted.
Thanks.
thanks for the great fix for an age old problem for menu. You really helped me out. Danke!!
perfect css tutorial !
Thanks a lot for the tutorial, very useful. Best from Ralph http://www.deprowebs.com
this is good…very good!
Nice trick! Thank you!
Thanks for the tutorial. Very helpful. I am sure I can think of ways to implement this method. Thanks again.
While this solution is well written, I too ran into major issues with legacy browsers. To simplify the HTML, I choose to use the Definition Lists approach (DL, DT, DD) and a background image. The process is the same, lay in a background image and cover the desired areas with a color background.
Check out http://www.madisonmac.com/services_and_support/software-support/. The solution is solid and even worked in IE 5.5. Everything else goes to hell, but this works ;) If anyone wants more of an explanation, feel free to email me.
Very useful! Thank you, Nick!
Nice tutorial. Was looking into this myself for a friend.
Very nice website… your are the best…
There’s always a hater, (Dale Sande) Why are you still testing for IE 5.5 .. ?? are you kidding me… anyway… I love the extra fee for IE 6 support … LOL.. however 34% of the world unfortunately is still using this browser as far as IE 5.5 , 0% of the world is using this browser….
http://www.thecounter.com/stats/2009/February/browser.php
woww..great blog! ur tutorial so useful..thx :)
@Dale Sande – definition lists are not appropriate to mark up this information. The point of the tutorial was also to avoid using background images were possible. I’d also argue that you are wasting your time worrying about IE5 or other legacy browsers that make up <0.1% of the market and continue to shrink. However, I have already suggested in an earlier comment that there is a preferred method that doesn’t use absolute positioning.
@RaphaelDDL & @Max Weir – see comment 22
@TiS – em and strong can’t be used for presentational reasons or just to give you a hook to apply some css for positioning. This is simply a list and I don’t believe you can justify using these elements. The service / dish is not “important” and nor should the cost be “emphasised”. It is using these elements incorrectly. If you want to add appropriate semantics then you could use microformats with span tags. A further problem with this demo is that the cost is not represented with a currency (which is an actual problem because their is a failure to indicate the nature of the number) and the decimal place is missing.
I couldn’t grasp the demo image. However the areas of economy of code, blowing out IE5 and the approach is great. But, does this cdd work the same in FireFox, I wonder how many pros test in FireFox.
You are my hero, your site is amazing! Your tutorials are so awesome!
A nice idea to publish a price list on a website. Thank you for this tutorial. Ralph
Interesante técnica… gracias por compartirla
Great tutorial, this will come in handy.
Ok, I feel I need to clear my name here. I have taken a lot of flame here because I have posted an alternative solution that takes less IE counter actions. I do realize that this exercise was an attempt to solve this UI issue without images, but I also realize that there is a lot of additional effort needed in order to make this example work across different browsers.
I do not understand why using a Definition List in this example is such a bastardization of the HTML tag? I found this a simpler solution as in repeated execution I do not have to insert the additional tags?
Then the apparent bane of my post, IE5.5 support. I don’t know why this comment was taken with such harsh criticism. I simply stated that with the idea I proposed, without additional effort it also happened to work with IE5.5. If anyone had taken the opportunity to look at the site I posted in IE5.5, you will see that everything else pretty much goes to hell, but this one thing still works. I am a progressive developer and I maintain a clear sight on the horizon. I am not a IE zombie or a developer that unrealistically holds onto the past. So please, lay off.
@Organicum – “I love the extra fee for IE 6 support … LOL” What is that supposed to mean? Do I not use enough dramatic drop shadows and that is why you hate me?