Scott Klarr Jr
How to make a CSS menu
Standards Compliant Menu
A menu is nothing more than a list of links for navigation, so the most ideal way to code your menus are by using a list, styled with CSS. This makes the styling ability very flexible while keeping the content-end of the list completely separate from any styling and in a format that is easy to read when style sheets are not in use or disabled.
First thing we need to do is build the list of links using the unorderd list tag as follows:
<div class="menu">
<ul>
<li><a href="url">Link 1</a></li>
<li><a href="url">Link 2</a></li>
<li><a href="url">Link 3</a></li>
<li><a href="url">Link 4</a></li>
<li><a href="url">Link 5</a></li>
</ul>
</div>
Next we will explore the basic code structure to turn this into a horizontally laid menu. The following code is CSS
1. Simple Horizontal Menu - Centered
.menu ul {
display: block;
padding: 0px;
margin: 0px;
list-style: none;
text-align: center;
}
.menu ul li {
display: inline;
padding: 0px 10px;
margin: 0px;
list-style: none;
}
This will lay the links out next to each other with a nice spacing between links. Because the li blocks are set to an inline type, you will not be able to specify a height for the menu items. That makes this particular code not an ideal method for menus in which each item much have a background image with specific dimensions. We will get to that later.
2. Horizontal Menu - Basic CSS Styling
.menu ul {
display: block;
padding: 0px;
margin: 0px;
list-style: none;
text-align: center;
}
.menu ul li {
display: inline;
margin: 0px;
list-style: none;
border: 1px solid #c8c8c8;
background-color: #e8e8e8;
padding: 1px;
}
.menu a {
padding: 0px 20px;
text-decoration: none;
color: darkblue;
border: 1px solid white;
}
.menu a:hover {
background-color: white;
border: 1px solid #d8d8d8;
color: black;
}
This example adds a little bit of styling via CSS with basic border, background, and foreground properties.
3. Horizontal Menu with Background Image
.menu ul {
display: block;
padding: 0px;
margin: 0px;
list-style: none;
text-align: center;
}
.menu ul li {
display: block;
float: left;
margin: 0px;
list-style: none;
}
.menu a {
display: block;
background: url('button.gif') top left no-repeat;
width: 99px;
height: 28px;
padding: 3px 5px 0px 0px;
margin: 0px;
border: 0px;
color: white;
text-decoration: none;
font-size: 12px;
}
.menu a:hover {
color: black;
}
By setting the display type of block, and using the float left property, we are able to manipuate the height dimesions of the link items which is necessary when doing this sort of styling on menus. There is one unfortunate caveat to doing this thoguh, and that is by using the float left, the links are going to stack to the left of their parent container. This can be troublesome if you want your menu centered.
You can overcome this with margins and/or paddings of parent container(s), but its not fluid so if you add or remove a link, you will need to modify the padding/margins you set in order to realign the menu. I have searched for solutions to this and tried many a strange ideas of my own with no real working hack to overcome this issue. If you happen to know of a solutionthat works on all major browsers AND is w3c valid, please let me know!
4. Simple Vertical Menu
.menu ul {
display: block;
padding: 0px;
margin: 0px;
list-style: none;
}
.menu ul li {
display: block;
padding: 0px 10px;
margin: 0px;
list-style: none;
}
The major difference here between the horizontal set is that the li is now being set to a block display type. Pretty boring as is, but you can make some nice looking menus very easily with a few simple css style additions.
5. Horizontal Menu - Simple CSS Styling
.menu {
width: 150px;
}
.menu ul {
display: block;
padding: 0px;
margin: 0px;
list-style: none;
}
.menu ul li {
display: block;
margin: 0px;
list-style: none;
}
.menu a {
display: block;
color: darkblue;
text-decoration: none;
padding: 5px;
border-bottom: 1px solid #e8e8e8;
}
.menu a:hover {
background-color: #e8e8e8;
border-bottom: 1px solid #d8d8d8;
color: black;
}
Here we have added some spacing, a bottom border, modified the link colors, and added a rollover highlight effect. This makes for a very clean, simple, yet attractive menu.
6. Horizontal Menu with Background Image
.menu {
width: 150px;
}
.menu ul {
display: block;
padding: 0px;
margin: 0px;
list-style: none;
}
.menu ul li {
display: block;
padding: 0px 10px;
margin: 0px;
list-style: none;
}
.menu a {
display: block;
background: url('button.gif') top left no-repeat;
width: 99px;
height: 28px;
padding: 3px 0px 0px 0px;
margin: 0px;
border: 0px;
color: white;
text-decoration: none;
text-align: center;
font-size: 12px;
}
.menu a:hover {
color: black;
}
Click here to see all these menus in action









sesebian Feb 11, 2008
hii, button download gif?
Oyeyemi TFrancis Mar 26, 2008
u are too much, send me an update on vbasic profile if available..
Example Aug 10, 2008
CSS "Cascading Style Sheets" Lessons
css list style Properties and examples -- http://css-lessons.ucoz.com/list-css-examples.htm
Graeme Sep 08, 2008
That's all great. But how can we add pull downs - are we still restricted to Java?
Chris Bourton Nov 13, 2008
Thats nice and effective. Thanks for the code examples.
hadi Feb 09, 2009
hi, i tried in mozilla , why is not working? Why only list of link's show but the css not works ?
hadi Feb 09, 2009
Hi, is hadi again, thanks it works now...the problem is in the link rel code..thanks great article...it's help me..
Cesar Mar 10, 2009
Hi, nice simple example.
Could it be possible for you to explain submenus?
Thanks.
Shelly Mar 19, 2009
I am new to developing websites and your article helped a lot. Thanks!
I can tell you that I added
position:relative;
left: XXpx; /* you choose how much to move it */
to the .menu a class and it worked in both Firefox 2.0 and IE 6. I have a really crappy, old computer so that's all I could test it with.
Shelly Mar 19, 2009
Oh, sorry, I forgot to mention that I did that to center the horizontal menu!