Horizontal navbars: CSS/HTML how-to

It’s Christmas time, so what better time of year to offer loyal Leon Paternoster readers some elegant navbar CSS.

Here’s how I’ve been implementing horizontal navbars (note: only tested in FF3 and IE7 at this moment):

The problem

We want a navbar that:

  1. is horizontal
  2. shows the reader where in the site they are
  3. indicates where the reader is about to go by changing the style of a hovered–over link
  4. doesn’t use any unnecessary divs

See a demo of my horizontal navbar.

Horizontal navbar

Horizontal navbar

The code

Our HTML couldn’t be cleaner and simpler—no meaningless divs, just semantic classes for the ul and the selected link:

<ul class="nav">

	<li class="current">Home</li>
	<li><a href="#">About</a></li>
	<li><a href="#">Journal</a></li>
	<li><a href="#">Portfolio</a></li>

</ul>

And the CSS:

ul.nav {
	height: 2em;
	text-transform: lowercase;
	font-variant: small-caps;
	letter-spacing: 0.1em;
	margin-bottom: 1.5em;
	font-family: lucida grande, liberation sans serif, sans-serif;
	background-color: #efefef;
	color: #333;
}

ul.nav li {
	list-style-type: none;
             float:left;
}

ul.nav li a {
	float: left;
	width: 10em;
	text-align: center;
	line-height: 2em;
	background-color: #efefef;
	color: #333;
	text-decoration: none;
	border-right: 0.0625em solid #333;
}

ul.nav li a:hover, ul.nav li.current {
	float: left;
	width: 10em;
	text-align: center;
	line-height: 2em;
	background-color: #ffd100;
	color: #333;
	text-decoration: none;
	border-right: 0.0625em solid #333;
}

You can, of course, play around with the colours and fonts.

How it works

The real magic is in the height and line-height properties: as long as the ul height is the same as the li line-height we can ensure that the borders that separate the nav links match the height of the background bar.

Have a very merry Christmas!

Comments

  1. Michael Tuck says:

    Sweet. Minimalist without losing elegance or function, and (I imagine) very accessible. Might want to fix your font stack:

    font-family: "lucida grande", "liberation sans serif", sans-serif;

    I’m not sure the font code will work without the quotation marks.

  2. Michael Tuck says:

    Rats, the PRE coding in the font-stack code line in the comment didn’t work. :P

  3. Leon says:

    Hi Michael – yes, I’d play about around with the fonts a bit more if I was using it on a site.

    Have you viewed it in IE? Does it work there?

    WordPress ignores pre tags in comments, although code is sufficient for one line.

    Did I leave the quotes out? Should still work, I think.

    —Leon

  4. Michael Tuck says:

    Just now tried it in IE6 (don’t have 7 at home), and it is indeed busted. Or I should say IE is busted, since your code should work in any browser worth its salt.

    You’re right, the font stack works (in FF) without the quotes. I didn’t think it would. :)

  5. Leon says:

    Now there’s a surprise.

    Being a good little Linux user, I don’t have access to IE at home. I’ll fix this on Monday at work.

    You don’t have IE7? Are you using a pre–XP system? I was using Windows 2000 up until 18 months ago on an 800X600 CRT monitor (and I rather liked it). Gave me a different perspective on web development.

  6. Michael Tuck says:

    Nope, doesn’t work in IE7 either. I’m no IE expert, so I can’t pick out the “error” that makes IE choke. Want me to post the code on SitePoint? They’ll know.

    I’m using a reclaimed WinME (!) machine now running XP. The version of XP I have came with IE6. My version of Opera is up to date, but until Firefox 3 can handle all of the extensions I use, I’m sticking with FF2. The monitor is the original, relatively small and rather dark. Not good. When I am left large sums of money by the mysterious uncle who will no doubt include me in his will, I will buy a decent #@$%&! computer. Until then (and after), I keep reminding the SP crowd that there are large numbers of unfortunates such as myself out there that can’t afford spiffy new computers and need to be kept in mind when design decisions are made.

    • Leon says:

      I was fond of my Win2000 machine: the CRT was the size of a coffin but the picture was crisp. I liked 800X600 as it made everything big. Of course, you have to scroll horizontally with an 800X600 these days. On most sites, that is.

  7. Leon says:

    Hi Michael,

    Yes, that’d be great. Now I don’t have Windows I can’t fix it at home, and work’s rather busy at the moment.

    Ta,

    Leon

  8. Michael Tuck says:

    Posted in the SP forums:

    Why doesn’t this navbar work in IE?

    It will be interesting to see what gets said. As I wrote in the post, I can’t find anything remotely wonky in the code except for the lack of quotes in the font stack, and I can’t believe that would have any effect on the layout being broken. I truly despise IE. :)

  9. Michael Tuck says:

    CSS / HTML Zen master Dan Schulz has a reply. I’ll quote it verbatim:

    Set an equal line-height on the list, then display: inline; and white-space: nowrap; on the list items. Then replace the line-height with a height equal to the list itself on the anchors. If you haven’t done so yet, zero out your margins and padding so the browser defaults don’t get used.

    And yes, those multi-word font declarations HAVE to be enclosed with quotation marks.

    Sounds relatively straightforward…?

    • Leon says:

      Hi Michael,

      I did have 5 mins last night to take a look at the answer: much appreciated. I’ll reply to the post some point later today and make the changes to the CSS this evening.

      The article’s great, by the way; really detailed and authorative. I’ll add a comment on SP too.

    • Leon says:

      The fix was simpler than that, even: I added float: left; to the ul.nav li selector.

      Thanks for your help,

      Leon

  10. Michael Tuck says:

    The article’s gutted. Maybe a quarter of it survived the editor’s machete. Grrrrr. But thanks for the kind words anyhow.

Add a comment