Turning a list into a navigation bar

I’ve received a couple of requests for a description of how I created the navigation bar that is currently used on this site. The CSS used isn’t all that advanced, and I hadn’t really thought about describing it in detail, but after being asked about it I decided to do a write-up.

Note: Since this article was published I have changed the markup and CSS for the navigation bar a bit, so the technique for highlighting the active link is no longer in use.

I’ve cleaned up the HTML and CSS slightly, so if you compare this to what is actually used on the site there will be some small differences. In case I have redesigned by the time you read this, check out the finished example to see what the menu looked like at the time of this writing.

The HTML

The markup is very simple. It’s an unordered list, with each link in a separate list item:

Why use a list? Because a navigation bar, or menu, is a list of links. The best (most semantic) way of marking up a list of links is to use a list element. Using a list also has the benefit of providing structure even if CSS is disabled.

At this stage, with no CSS applied, the list will look like any old (normally bulleted) list, styled only by the browser’s defaults.

I’ve given id attributes to the ul and li elements. The id attribute for the ul element is used by the CSS rules that style the entire list. The li elements have different id values to enable the use of CSS to highlight the currently selected link. This is done by specifying an id for the body element. More on that later.

The CSS

I’ll describe the CSS I’ve used to style the list in a step-by-step fashion.

First of all, I set the margins and padding of the list and list items to zero, and tell the list items to be displayed inline:

This will make all the links display one after another on the same line, as if the list wasn’t there. It will also remove the list bullets, since they are only displayed when display:list-item (the default display mode for list items) is used. Some browsers are said to incorrectly display the list bullets even though display:inline has been applied to the list items. I haven’t seen this happen in any of the browsers I tested in, but if you want to make sure that no browsers display list bullets, you can add list-style-type:none to the rule for #nav.

Next, it’s time to start styling the menu tabs. I do this by adding styles to the links, not to the list items. The reason for that is that I want the entire area of each tab to be clickable. First a bit of colour to make the changes more obvious:

That’s a bit better. But there is a potential problem that isn’t visible here. Since the links are inline elements, their vertical padding will not add to their line height. It’s easier to see this if the ul element has a background, so I’ll add a background colour and a background image:

Oops. Now the links are sticking out of the list element. To fix this, I’ve turned the links into block boxes by floating them to the left. I’ve also set their width to auto, to make them shrink to fit their content:

Adding display:block to the CSS rule for the links would also have made them block boxes, but since a floated element automatically generates a block box, that isn’t necessary.

As you may have noticed, the background disappeared when the links were floated. That’s because floated elements are taken out of the document flow, which causes the ul element containing them to have zero height. Thus, the background is there, but it isn’t visible. To make the ul enclose the links, I’ve floated that too. I’ve also set its width to 100%, making it span the whole window (except for the padding I’ve given the body element in this example):

To visually separate the links from each other, I’ve added a right border to the links. Then, to give the first link a left border as well, I’ve used a :first-child pseudo-class to apply a rule only to the link in the very first list item. I’ve also added top and bottom borders to the ul element:

#nav {

margin:0;

padding:0;

background:#808259 url(nav_bg.jpg) 0 0 repeat-x;

float:left;

width:100%;

border:1px solid #42432d;

border-width:1px 0;

}

#nav a:link,

#nav a:visited {

color:#000;

background:#b2b580;

padding:20px 40px 4px 10px;

float:left;

width:auto;

border-right:1px solid #42432d;

}

#nav li:first-child a {

border-left:1px solid #42432d;

}

The :first-child pseudo-class is not recognised by Internet Explorer for Windows, so the first link won’t have a left border in that browser. In this case, that isn’t a major problem, so I’ve left it like that. If it’s really important to you, you’ll need to add a class to the first list item (or the link in it), and then use that to give the link a left border.

Next I’ve changed the way the link text is displayed by removing the underlining, making the text bold, specifying font size, line-height, and a different font family, making the text uppercase, and adding a little bit of drop shadow. The drop shadow is created with the text-shadow property, a CSS3 property that is currently only supported by Safari and OmniWeb:

In the final step, I’ve added rules that will make the selected link look different than the others, to show visitors where they are on the site.

In case you haven’t seen an example of specifying an id attribute for the body element to style the “current” navigation tab differently before, that’s what the first two rules do. In the examples linked to from this article, I’ve set id of the body element to “home”, which makes the “Home” tab the current one. Changing it to “about” would make the “About” tab the current one, and so on.

I’ve also made the selected link stay the same when it’s hovered over. It can be argued that the current menu item should not be a link at all. In this case, I’ve chosen to leave the link in the markup and use CSS to remove the visual feedback on hover.

To give some visual feedback when you click on one of the links, I’ve given the :active state of the links the same styling as the selected link:

That’s it. This step-by-step tutorial makes the whole thing look more advanced than it really is. View source on the final example to see the complete set of CSS rules. By the way, with a couple of small exceptions (the left border on the first link, and the text shadow), this works in just about any browser, even Internet Explorer (version 5 or newer).

I hope you’ve been able to follow along well enough to be able to create your own navigation menu. The styling possibilities are almost endless.

Comments

Very nice writeup. You could remove the #nav li:first-child a reference by changing the #nav border-width to this: border-width:1px 0 1px 1px;. That would put a left border on the whole list, and it would work with IE as well.

Steve: The reason I didn’t do what you’re suggesting in this case is that I’ve set set the width of the ul element to 100%. Adding a left/right border (or padding) to it would make its total width exceed 100%, which would complicate things, and potentially introduce a horizontal scrollbar.

In fixed width situations it would work though — you’d just need to subtract the border width from the width of the ul.

Very useful and exactly what I was looking for for an internal site I’m doing, esp. the bit about making the whole object clickable. I assume the if I wanted the list to be vertical not hortizontal I would put something else instead of inline?

One question: you float the <ul> Element so that it stays “open”. This was a valuable piece of information to me but I couldn’t find anything about it in the specs. Is there a rule that says that a parent element stays open when it is floated itself even though all elements inside are floated too? Or did you just happen on this?

Roger: Good point. I rarely deal with percentages, so I tend to forget about adding borders. It makes me wish that in future versions of CSS you could combine %’s and, say, px using equations. For example, here it would work well if you could do something like

Thanks heaps for this one! It helped me heaps with a site I was working on.

One thing I did with was add secondary sub-lists. The top list highlights as well as the sub-list to what the specific page is. I used your step 11, but used the specific child selector (>) to highlight the top list, then normal #name #othername to highlight the second.

Hope some of that makes sense. I’ll see if I can post samples (it’s on an admin area of a site).

Hmmm…. tried to make the my top links more intelligent by using the “body id” tag so the user knows what section they’re in. Works fine in IE on PC but doesn’t work in Firefox or Safari. Can anyone tell me what I’m doing wrong?

Roger, this was fantastic. I too am a newbie to CSS and by following your guide I was able to create my nav bar using CSS. Thanks for that!

I did notice though that my nav bar wraps as the window gets smaller. I see on your website that you added a #wrap div tag. I assume that was done to prevent the nav bar from wrapping. Can you tell me what CSS you used with the #wrap div tag (if that is indeed why you used it)?

Tracy: The #wrap div is used to give the whole site a fixed width. A side effect is that it prevents the navigation bar from wrapping as the window is resized. However, it doesn’t stop it from wrapping if you enlarge the text enough. You need to pump it up quite a bit but eventually, it will happen.

This has been very useful, excellent write-up! One question: is there any way I can have one of the links not change colour on mouseover, and the rest change colour? I am making a site where the client has one area that he wants included in the menu, but to not be actually selectable yet?

Fantastic writeup. I’ve been looking for css ways to implement this sort of thing without any php, for some reason though I can’t seem to get the state where link to the page is highlighted if the browser is displaying that page.

I’m just testing it out, so I’ve included for the first link only, which should ideally work right?

yeah sorry Roger, realised that in actual fact your code works a peach, it’s just that I’ve got to come up with a way to dynamically change the body id tag for different types of pages to bring out the css…Once again thanks for the tutorial.

Hello everyone. I need some help. My navigation bar is working fine but I can’t make the active link work so you know where you are. I doesn’t work on any of the browsers, is there any code wrong? Can anyone help me?
url: www.actonit.org.uk
You can see the style sheet: www.actonit.org.uk/style2.css

Hi, I need some help with this because the floats are messing up the rest of my layout. It is pushing the main content far to the right. When I use align left, it makes the background very narrow. How to make sure the floats work well with the rest of the page?

Thanks for your easy and useful tutorial! Any hope for an update on how to make this work in a sub-menu code? It seems like Tarwin never pop in with his;-)
I’ve tried for a while to get it working with nested list but can’t get it working. Toplevel selected marks the submenu elements too, selecting submenus seems to work fin.
Any hints her would be nice!-)

I’ll put making a sub-menu on my to-do list. It will take a while before I get some spare time for it though. If you want to give it a go on your own, a good starting point may be CSS Tabs, separate lists or CSS Tabs, nested lists.

Hello. I really liked your tutorial. But if have soem questions. Why is the active option only working in IE and not in FF. THe orange stay oranger in IE, but it just flashes for a sec in FF.
And the second thing is, i have been testing out css for a while, and im wondering how you centered the whole site, so that the menu is under the banner and all that stuff.

I’m a begininer of CSS menu, and reading all the comments helped me to understand a lot.

Since I’m trying to do a 2-level menu nav, I took the “CSS Tabs, nested lists” example and updated it with some javascript to test the menu.

Here’s a link of my demo: http://www.kk-production.com/testmenu.htm

My 2 questions are:

how can I remove the gap between all the parent-level menu <li> item? (i.e. space between parent1 and parent2, etc)

I tried changing “submenu” style into 6 different submenu, in order to use absolute position to place the submenu. But my effort failed misserably. Is there any way that I can setup absolute position of the submenu? Say when I click on parent6, I want that submenu item to be “position:absolute; left:100px” for example?

I’ve always used this method to make my menues and submenues using css. In my most recent one however, I tried to make one using background images only without text for the links because I want to use a non web-safe font. It works as expected in Firefox, but in IE it flashes to the background color as you mouse over. Anyone have any idea what I did wrong or could do to fix it? It’s at http://nicolekidman.fan-wire.com

Comments are disabled for this post (read why), but if you have spotted an error or have additional info that you think should be in this post, feel free to contact me.