Tabbed navigation is very popular—it's clean and easy to use. Tabs can be created a number of ways, from simple HTML + CSS methods to more complex graphics-controlled-by-javacript techniques. In this exercise I present another method that falls somewhere in between: it uses graphics with rollover effects, but it controls them entirely with CSS. When they're finished they will look something like this:
Tabbed navigation requires at least two, but usually three "states" per button: normal (how the tab usually appears), an "over" state (how the tab appears when it's rolled-over), and a "current" state (how the tab appears when you are on that particular page). Some methods require you to create a separate graphic for each state of each button, but with this method each graphic contains all three states (normal, over, and current).


Why .png files? They are better than .jpeg files for flat graphics and text and also allow for transparency (so I could leave the rounded corners transparent). And they's better than .gif files in terms of quality.
<div id="mainNav">
<ul>
<li id="item1Tab"><a href="#" id="current"><span></span>Item 1</a></li>
<li id="item2Tab"><a href="#"><span></span>Item 2</a></li>
<li id="item3Tab"><a href="#"><span></span>Item 3</a></li>
<li id="item4Tab"><a href="#"><span></span>Item 4</a></li>
<li id="item5Tab"><a href="#"><span></span>Item 5</a></li>
</ul>
</div>
Note the addition of id="current" to the first <a> tag; you can move it to any of the other <a> tags if you prefer (but only use one per tab menu). This id will be used to indicate whichever page you are currently on; so if your 're creating the page for "Item 4," you would move the id="current" to the <a> tag for tab 4.
<style type="text/css">
</style>
* {
margin:0;
padding:0;
}
#mainNav ul li{
float:left;
position:relative;
list-style-type:none;
width:75px;
height:25px;
}
The first style rule uses the universal selector to removes all default margins and padding. The next style rule turns the vertical list column into a horizontal row, removes the bullets, and gives each list item a relative position. The position has no effect of its appearance, but we need it to absolutely position the image on top which we will do next.
#mainNav #item1Tab a span{
position:absolute;
width:75px;
height:25px;
top:0;
let:0;
background-image: url(images/tab1.png);
background-repeat: no-repeat;
background-position: 0px 0px;
}
#mainNav #item1Tab a:hover span {
background-position: 0px -25px;
}
#mainNav #item1Tab a#current span {
background-position: 0px -50px;
}
The first rule positions the <span> absolutey, pulling it out of the markup and dropping it on top of the link. It then applies the tab image as a background (but since the <span> item is on op of the text link, the background cover it up).
The next two rules reposition the tab slightly—note the changes to the background-position line: the first or "normal" state shows only the top third of the graphic (the graphic is 75 pixels tall, but it's showing only 25 pixels of it); the second "hover" state shows the middle third; and the final style shows the bottom third of the graphic. Thus when one rolls-over the tab it changes, essentially 'sliding' up and down into the proper position.
Last update: 5/10/10