Why use CSS Menus?
The short answer … because they are awesome. If you do much web programming, especially if you are more of a programmer than a designer, you have probably had the opportunity to sit in front of the computer and come up with some type of menu schema that will look nice and work properly across all browsers. This can often be a very frustrating experience, but help has arrived … I give you CSS and an unordered list. This concept is certainly not new, but it is powerful and worth reviewing.
The truth is that you can find many different controls that will produce a menu for you. Among them are:
There is even the Microsoft provided menu control, which you can find here:
Now, it is not my intention to bash any of these controls. I actually think that they are all very beautiful and quite impressive. Here at TLS, I have used several Telerik controls with excellent results. Nevertheless, building a great looking menu is not anywhere near as difficult as it used to be and it can now be done using nothing but an unordered list and some CSS tags. If you want to spruce your menu up a little bit more, you can also include an image or two but that is not necessary.
Making a menu like this has several advantages.
- Cross Browser Compatibility – Any browser that supports CSS (which by now should be all of them) will render your menu correctly. If you feel compelled to do so, you could add in some CSS for older IE versions, but I say …. If you are still using IE5 or IE6, that is your problem, not mine.
- Code Simplicity – By this I mean that the amount of code on the page necessary to actually render the menu using an unordered list is much smaller than any other kind of menu you could use.
- Graceful Failability – In other words, if for whatever reason, your styles are not working or javascript is turned off or the user has some ancient browser that doesn’t support modern CSS or Javascript, then the menu will still render as a normal Unordered List, which works perfectly fine as a menu, even if it is not the flashy affair that you envisioned.
- Mobile Device Scalability – Lets face it, gone are the days in which you could plan on a user having either an IE or a Mozilla browser. Now a user could be on any number of browsers as well as an iPhone, an iPad, an Android Phone (all 2,394 models each with a different resolution), a Kindle Fire, a Windows Phone (I hope) and even an old non-smart phone with internet access. A menu done this way is far more likely to work correctly and look good on the lot of them.
Creating a CSS Menu
So … lets see some examples. First of all, we need to create our menu. This is very simple, just create an unordered list of some values. Mine will look like this:
<ul>
<li><a href="#">Michael Westen</a></li>
<li><a href="#">Sam Axe</a></li>
<li><a href="#">Fiona Glenanne</a></li>
<li><a href="#">Madeline Westen</a></li>
<li><a href="#">Jesse Porter</a></li>
</ul>
When actually rendered on a page, it will look like so (Bullets and all):
As I mentioned, while not pretty, this is certainly serviceable as a menu if the CSS wasn’t working for some reason. By contrast, go look at a website using a complex menu control and then disable javascript and css … the result will be ugly and generally not even recognizable as a menu. But to make ours into a pretty menu, we are first going to add some styles to our unordered list which can then be used to make the list look more like a classic webpage menu. The first thing that we have to do, is set up a style identifying this list as a menu so that it will behave differently than other unordered lists that we might have on our webpage. The first thing that we need to do is to encase the unordered list in a containing tag. I will use a “div” tag. This gives us a couple of advantages, such as making sure that it extends to 100% of the element that it is inside of and allowing us to set a background color for the menu as a whole. Then we will give the container a class name … I’ll call it CssMenu. Like so:
<ul id="Menu">
<li><a href="#">Michael Westen</a></li>
<li><a href="#">Sam Axe</a></li>
<li><a href="#">Fiona Glenanne</a></li>
<li><a href="#">Madeline Westen</a></li>
<li><a href="#">Jesse Porter</a></li>
</ul>
Now, this will not do anything to make the menu display different because we haven’t defined a CSS Style for “CssMenu” yet. So the next step is to style the unordered list so that it is starting to appear the way we want it to appear. To begin with, we will set the margins and paddings to 0 and then also set the height. Most importantly, we will set the “list-style-type” attribute to “None” so that it does not actually look like a list. The style tag would look like this:
#Menu
{
margin: 0px;
padding: 0px;
list-style-type: none;
height: 25px;
}
Doing that gives us the following result:
If you wanted a vertical menu, then what we have right here would work pretty well, but I want our menu to be horizontal, so I will add additional style tags to make the “li” items in the list line up next to one another as opposed to on top of one another. The style for that looks like this:
#Menu li
{
float: left;
}
At this point, our list items are lining up one after the other like this:

There is nothing earth shattering here, I know, but I’m trying to go step by step. Believe it or not, we are almost there. From here, we just need to style the links on each list items so that they look pretty. Anyone with CSS experience should be able to do this with relative ease. In my case, I added things like a border, a background color, font family, font size etc. By adding padding also, you can add some space between the end of the word and the end of the link. By adding a right margin, you can separate each link into its own space. Also, by setting the display to “block” you can set the height or the width and have that value stick. Since block elements take up 100% of whatever they are inside of unless told otherwise, this means that the link will take the entire width of the list item. By adding in the following styles:
#Menu a
{
border: solid 1px #000066;
border-bottom: solid 1px #99000;
background-color: #EDECD1;
padding-right: 20px;
padding-left: 20px;
margin-right: 4px;
display: block;
line-height: 25px;
font-family: Verdana, Tahoma, Arial;
font-size: 10pt;
color: #333333;
text-decoration: none;
}
My unordered list now looks like this:

To add some pizzazz, now, I am also going to add a style to the link so that it changes color when the mouse pointer hovers over it. Once again, this is very simple css code …. like this:
#Menu a:hover
{
background-color: #990000;
color: #ffffff;
border-bottom: solid 1px #990000;
}
Now when the mouse hovers over a menu item, it will change color.

At this point, it is quite easy to put a content div tag right below the menu and make the colors match so that the menu appears to work in conjunction with the are below it … like this:

Conclusion
From here, the possibilities are endless. You could do all kinds of things using a few more simple css concepts like adding images or wrapping the menu in other tags. I’m not going to go into that, but I will say that in just a few lines of code here (with no images or javascript) we have made a nice little menu. If you are interested in more ideas as to how you could use these concepts, click here … but prepare to have your mind blown when you see all that can be accomplished just using CSS.
Tags: css, menu, unordered list










