Horizontal CSS menu using a pointer and background images

OK, so the title is a bit too descriptive but I’m trying to make this post findable in case anyone out there needs to solve a similar problem. So this will be about… that’s right: creating a horizontal menu, using CSS and XHTML. Each menu item will have a small pointer underneath (an arrow if you will), and I want it all done using images, just to make it a little harder.

Here’s what I wanted the menu to look like:


As you can see, some menu items are longer, some shorter. The menu sits in a gray bar and is separated from the remainder of the page by a green “tube”. I want the selected item and the hover state to be represented by a dark box with a pointer that breaks through the green tube.

At first I made the item an li which had the dark box as a background image and was immediately followed by an empty em tag which then had the pointer as a background image and was offset using relative positioning at 50% left.

A menu item looked a bit like this:

<li><a href="whatever">Homepage</a><em></em></li>

The problem was the positioning of the whole thing as in some browsers, the pointer would become detached from the dark box, and I couldn’t fix that with negative top margins because then the pointer would be offset in yet another browser. A mess. Plus, with this method, I needed a minimum width for each item, so I could then calculate the mid point to place the pointer. No good.

But there is quite a practical solution that came to me while thinking about the sliding doors method of making tabbed navigation menus: marvel then, at the ability to stack background images.

I couldn’t have used a single background image, because then, all my items would have to be the same width, since stretching the background image would distort the triangular pointer, so enter the bkg stack!

I took the li elements of an ul and styled them so they had the correct height: dark box plus triangular pointer. Then, I added a span inside each li, so I could style two overlapping elements with two different images. Then, in the CSS, I applied a background image to the li and another (the pointer) to the span. Keeping the stretching and centering is dead easy like this: you make the dark box around the text repeat on the x-axis and you turn off stretching on the pointer. Plus, you can easily center the pointer by just using “center” on the background declaration.

Here’s the HTML:

<div id="menuarea">
  <ul class="menu">
    <li class="current"><a href="#"><span>Homepage</span></a></li>
    <li><a href="#"><span>Funcionalidades</span></a></li>
    <li><a href="#"><span>Contactos</span></a></li>
    <li><a href="#"><span>SMS</span></a></li>
    <li><a href="#"><span>Chamadas</span></a></li>
    <li><a href="#"><span>Webmessenger</span></a></li>
    <li><a href="#"><span>Comunidade</span></a></li>
    <li><a href="#"><span>Passatempos</span></a></li>

And the CSS:

#menuarea {
  background:url('images/menuarea_background.gif') repeat-x;

.menu {
  margin:0 auto;
  padding:6px 0 0 0;

.menu li {

.menu a {

.menu a span {
  padding:9px 8px 8px 8px;

* html .menu a span {
} /*IE Fix*/

.menu a em {

.menu a:hover {
  background:url('images/menuhover_background.gif') repeat-x;

.menu a:hover span {
  background:url('images/menu_pointer.gif') top center no-repeat;

.menu a:hover {
  background-position:0 0;
} /*IE fix*/

.menu .current a {
  background:url('images/menuhover_background.gif') repeat-x;

.menu .current span {
  background:url('images/menu_pointer.gif') top center no-repeat;

This is what the images look like:

menu area background The menu area background, which repeats along the x axis.

Menu item background The “dark box” that envelops each “current” or “hover” menu item

Menu pointer And the pointer.

As you can see, the dark box is as tall as a menu item plus the pointer and the pointer is the same height. They’re both transparent where they overlap and the pointer includes a bit of the green tube.

Overlapping background is a really powerful method of styling elements to make them look more complex than they actually are. It’s not *very* easy and you’ll notice the need for some IE 6 fixes and some weird height definitions that seem to make no sense. I know, I still can’t really explain some of it. But the truth is that the menu looks perfect in IE6, IE7 and Firefox for Windows as well as in Safari and Firefox for the Mac.

6 thoughts on “Horizontal CSS menu using a pointer and background images

  1. Whoa, nice technique. I liked specially the width in ems 😉 and the way you used the combination of bg images in the several elements. I would, however, do one thing differently. If I may…

    Instead of specifying the background-image only on :hover, I’d put it right there from the start, but have a bigger image, with a big transparent portion. I’d use background-position to place the transparent part viewable and then on :hover I’d adjust the background-position. All this to avoid that microsecond flicker. Kind of like CSS Sprites.

    Also, what if the text grows? If you use a taller image (with solid color beneath the current size) you could sustain a few size bumps (upwards).

    All this to say, thanks for sharing the technique. Even if there are easier ways to achieve this, the concept is pretty powerful. You can easily change the pointer between states without loading a whole new image. And the descriptive title is a must. Well done.

  2. Thanks 🙂

    I didn’t really notice flicker so the large image/background position idea didn’t even occur to me, but it is a cool technique.

    Also did not think about vertical growth at all, but I suppose having the color extend from underneath the image could work, as long as there’s no complexity in the image where it links with the solid color.

    1. Sorry, I don’t really understand what you mean. Did you find this elsewhere? I did write this little trick, it’s a few years old… I wouldn’t be surprised if someone used it somewhere else, it happens a lot on the web.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.