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).
<div id="mainNav">
<ul>
<li id="item1Tab"><a href="#" id="current"><span class="noShow">Item 1</span></a></li>
<li id="item2Tab"><a href="#"><span class="noShow">Item 2</span></a></li>
<li id="item3Tab"><a href="#"><span class="noShow">Item 3</span></a></li>
<li id="item4Tab"><a href="#"><span class="noShow">Item 4</span></a></li>
<li id="item5Tab"><a href="#"><span class="noShow">Item 5</span></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>
#mainNav ul {
margin:0;
padding:0;
}
#mainNav ul li{
display: inline;
float: left;
margin:0;
padding:0;
}
.noShow {
margin-left: -999em;
}
The first style rule removes the indent. The next style rule turns the vertical list column into a horizontal row. The third rule hides the list text to make room for the graphics.
#mainNav #item1Tab a:link, #mainNav #item1Tab a:visited {
background-image: url(images/tab1.png);
background-repeat: no-repeat;
background-position: 0px 0px;
display:block;
margin:0;
padding:0;
width:75px;
height:25px;
}
#mainNav #item1Tab a:hover {
background-image: url(images/tab1.png);
background-repeat: no-repeat;
background-position: 0px -25px;
display:block;
margin:0;
padding:0;
width:75px;
height:25px;
}
#mainNav #item1Tab #current {
background-image: url(images/tab1.png);
background-repeat: no-repeat;
background-position: 0px -50px;
display:block;
margin:0;
padding:0;
width:75px;
height:25px;
}
All three styles are nearly identical. Each creates a 75px by 25px box around Item1 and then adds the tab1 graphic as a background. However, 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, put 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.