Apple-Style Nav Menu

October 1st, 2009 / Tutorials

I'm a big fan of using sprites for navigation menus. While they don't offer the flexibility of a solely HTML & CSS menu due to the fact that you have to create a new graphic every time you want to change a menu item; sprites do offer you infinite styling possibilities limited only by your creativity with graphics software. Apple's website is a well-known example of a sprite-driven nav menu, so I'll use that as the inspiration and walk through how to create something similar.

The purpose of this tutorial is to walk you through setting up a sprite-driven nav menu, and the HTML/CSS required to make it work. The fact that it mimics Apple's menu is just to give you an example of a well-known design and show you how to re-create it while learning how to use sprites. The emphasis isn't on the style, which is why I don't show the steps of creating the demo sprite, but rather the code behind it.

There are three steps to this tutorial: Making the sprite, setting up the HTML and lastly, writing the CSS that makes it work.

1. The Sprite

This is where you get to be creative. Sprites with fixed-width buttons make it easier when you get to the CSS, and that's what I'm using for this tutorial. You need to be pixel-perfect when setting up your different states so they look right when used in your menu. Guides are your friend:

Whole nav sprite in photoshop

Entire sprite in Photoshop, with each of the 4 button states as rows.

The first thing you need to do is a little math. In my example, each button will be a fixed size of 114 x 36px, so each row (state) of the menu is 456 x 36px. Then you must decide how many states your buttons will have—this tutorial will use 4 states:

  1. Inactive
  2. Hover
  3. Click (mousedown)
  4. Active page

So, the total size of my demo sprite will be 456 x 144px. Figuring this out ahead of time based on your own design will make creating your sprite much faster. I recommend setting up your guides first, and doing all the buttons for the first state. Then just copy it down to the next state, edit and repeat until done. The PSD I used for this tutorial is included with the download at the end of this post.

View the completed sprite used for this tutorial.

2. HTML

We'll use an unordered list to layout our menu:

<ul id="nav">
  <li><a class="me" href="#"><span>Me</span></a></li>
  <li><a class="skill" href="#"><span>Skill Set</span></a></li>
  <li><a class="work" href="#"><span>Work</span></a></li>
  <li><a class="journal" href="#"><span>Journal</span></a></li>
</ul>

We'll give the <ul> the id "nav", and each link has a class: me, skill, work and journal. For accessibility, the text for what each button does is enclosed in span tags. That's it, we don't need to touch the HTML again until later.

3. CSS

This is where everything happens. First, set up the menu:

#nav{
  list-style-type:none;
/* Remove bullet points */
  width:456px;
/* Overall width of menu */
  height:36px;
/* Overall height */
  margin:50px auto;
/* For demo display purposes, not required */
}
#nav li{
  float:left;
/* So all items will display in a row, and not on top of each other */
}
#nav li a{
  background:url(nav.png) no-repeat;
/* Tells each link to use the sprite for its background */
  outline:none;
/* Gets rid of the dotted border in some browsers when you click */
  display:block;
/* Gives each button substance */
  width:114px;
/* Width of button */
  height:36px;
/* Height of button */
}
#nav li a span{
  display:none;
/* Hides the span text */
}

Now, we set up the initial state of each button:

#nav li a.me{
  background-position:0 0;
}
#nav li a.skill{
  background-position:-114px 0;
}
#nav li a.work{
  background-position:-228px 0;
}
#nav li a.journal{
  background-position:-342px 0;
}

What that does is set the appropriate background sprite position for each button. If this doesn't make sense, imagine two stacked pieces of paper, the one on the bottom has our sprite on it, and the top piece has a small box cut out of it. What we're doing is sliding the bottom piece of paper to different positions so different sections are shown through the hole in the top sheet. The two values for background-position are the X & Y coordinates based off a top-left registration point.

Where we are at so far:

Buttons are displaying properly.

Everything looks right, but it doesn't do anything yet.

You can see each button is displaying as it should, now let's make it interactive. First we'll add hover:

#nav li a.me:hover{
  background-position:0 -36px;
}
#nav li a.skill:hover{
  background-position:-114px -36px;
}
#nav li a.work:hover{
  background-position:-228px -36px;
}
#nav li a.journal:hover{
  background-position:-342px -36px;
}

All we did was bump everything down 36px (button state 2), and now we have hover interaction.

Hover states are all working.

Hover states are all working.

So far so good, now let's make it look like the button is pressing down when you click on it, by bumping everything down another 36px (button state 3):

#nav li a.me:active{
  background-position:0 -72px;
}
#nav li a.skill:active{
  background-position:-114px -72px;
}
#nav li a.work:active{
  background-position:-228px -72px;
}
#nav li a.journal:active{
  background-position:-342px -72px;
}

Our click interaction is working. To make it look like the button is going down, I bumped the text in state 3 and 4 down 1px when I was making my sprite graphic.

Oooh, it looks like a real button being pressed.

Oooh, it looks like a real button being pressed.

Now all that's left is to give the button its ON state. For this we go back to the HTML and add class="on" to the button that we want to be active. Let's make “Journal” active:

And the CSS for each ON state, moving the background down a final 36px (button state 4):

#nav li a.me.on{
  background-position:0 -108px;
}
#nav li a.skill.on{
  background-position:-114px -108px;
}
#nav li a.work.on{
  background-position:-228px -108px;
}
#nav li a.journal.on{
  background-position:-342px -108px;
}

Now we have all states working properly. In the menu HTML for each page of your site, just add class="on" to the appropriate link, and that's it!

Final state.

Final state of the button, for the active page.

Not so fast…

You didn't think we could make such a nice menu without adding some sort of fix for the red-headed step-child of browsers, did you? With the code as it is now, the ON button still reacts to hover in IE6. To get around it we have to turn this original code:

#nav li a.whatever.on{
  background-position:-342px -108px;
}

Into this:

#nav li a.whatever.on:link,
#nav li a.whatever.on:hover,
#nav li a.whatever.on:active,
#nav li a.whatever.on:visited{
  background-position:-342px -108px;
}

For each button, we have to explicitly say that each state (link, hover, active and visited) for a link with class="on" should have the same background position. Adjust accordingly for each of the buttons, and you're golden. A relatively easy fix, just adds a little extra bloat, but both the HTML and CSS will validate.