Dan's XHTML-CSS Tutorial : Walkthrough : Page 8

And on the Menu

[back to 'Adding Some Style']

Now for menus, everybody wants rollover effects. You're just not one of the cool kids, if you don't have rollover effects on your menu. (And if you don't know what rollover effects are already, I'm afraid you're not one of the cool kids. But stay tuned: salvation is at hand!) In the bad old days everyone would just stick in a bit of copied javascript and call it good enough, but today we're both simpler and more sophisticated: we do it all with CSS and our friendly little list.

The first step in turning a list into a menu is to get rid of those pesky bullets, which is accomplished with the following addition to your style.css:

#menu ul { list-style-type: none; }

And they're gone. This particular type of selecter is called descendent selector: it applies to all <ul>s contained within the 'menu' div (that way we can still use regular lists elsewhere on the site). Since this rule is fleshing out the menu, I put it right below the #menu selector.

Next, give the list some body:

#menu ul { list-style-type: none; width: 150px; margin: 5px 0 0 0; padding: 0; border: 1px solid black; } view result

(Again, the border is only temporary, to show us where the list ends.)

With the <ul> handled, we move on to the <li>s themselves, which are easy enough:

#menu li { margin: 0; padding: 0; border-bottom: 1px solid #999; } view result

While the <li>s now have bottom borders, however, the top one doesn't have a top border (besides the ugly black box). To fix this, just replace the border declaration of the <ul> with

border-top: 1px solid #999; view result

And then comes the fun part: turning the links into buttons. Watch:

#menu li a { display: block; background-color: #bbb; margin: 0; padding: 5px 10px; } view result

Aside from the colors, the change is subtle, but note that you can now click any part of the 'button' defined by the links' bottom borders (or mouse over any part for the hover effect).

Finally, we need to get some more interesting hover effects. We'll start by adding a few more declarations to '#menu li a', as follows:

font-weight: bold; color: #fff; text-decoration: none; text-align: right;

And then we define the hover state:

#menu li a:hover { background-color: #eee; border-left: 5px double #aaa; color: #333; } view result

And so we're done with the menu. Much more can be done to fancy up the buttons, including the addition of background images, but for the moment at least I'll leave those to you to figure out. Your complete #menu code should now look like this (I make one further change below, adding the :visited and :active states):

#menu { position: absolute; top: 100px; width: 150px; background-color: #ccc; margin: 0; padding: 0; } #menu ul { list-style-type: none; width: 150px; border-top: 1px solid #999; margin: 5px 0 0 0; padding: 0; } #menu li { margin: 0; padding: 0; border-bottom: 1px solid #999; } #menu li a, #menu li a:visited, #menu li a:active { display: block; background-color: #bbb; margin: 0; padding: 5px 10px; font-weight: bold; color: #fff; text-decoration: none; text-align: right; } #menu li a:hover { background-color: #eee; border-left: 5px double #aaa; color: #333; }

[ back to 'Adding Some Style']