CSS: Tabbed Menu

By Xah Lee. Date: . Last updated: .

This page shows you how to implement Tabbed Menu using CSS.

Click on the tab on top of this page. You can see that pages get switched and the corresponding tab gets highlighted. Here's how this is done in pure CSS.

Let's say you want a tab menu for 3 pages: A, B, C.

Put this into the top of page A:

<ul id="pageA" class="tabs">
<li id="a"><a href="a.html">Tab A</a></li>
<li id="b"><a href="b.html">Tab B</a></li>
<li id="c"><a href="c.html">Tab C</a></li>
</ul>

Also, put it into page B and C, but use id="pageB" and id="pageC".

Then, all you have to do is use the style sheet to control how your tab should look. Notice that in the above code there's id="pageA". So, in your style sheet, you specify a appearance for tag that match “pageA”. Like this:

#pageA #a {background-color:red}
#pageA #b {background-color:silver}
#pageA #c {background-color:silver}

#pageB #a {background-color:silver}
#pageB #b {background-color:red}
#pageB #c {background-color:silver}

#pageC #a {background-color:silver}
#pageC #b {background-color:silver}
#pageC #c {background-color:red}

Here's the complete CSS code for page A:

ul.tabs {
 list-style:none;
 margin:1em;
 padding-bottom:5px;
 border-bottom:4px solid red;
}

ul.tabs li {
 display:inline;
 width:100px;
 height:20px;
 padding:5px;
 margin:5px;
 text-align:center;
 border-top-left-radius:.5em;
 border-top-right-radius:.5em;
}

ul.tabs a:link {text-decoration:none; color:#000000}
ul.tabs a:visited {text-decoration:none; color:#000000}

#pageA #a {background-color:red}
#pageA #b {background-color:silver}
#pageA #c {background-color:silver}

#pageB #a {background-color:silver}
#pageB #b {background-color:red}
#pageB #c {background-color:silver}

#pageC #a {background-color:silver}
#pageC #b {background-color:silver}
#pageC #c {background-color:red}

Note that we also draw a red line under the tabs. This is done by border-bottom. This line should have the same color as the active tab.

We set the “ul” to not show its normal bullets for list items. This is done by list-style:none

We set “li” to have display:inline, so it flows from left to right. 〔►see CSS: Flowing List

We make each “li” to have background color so it looks like a solid tab.

Finally, we make the top corners of the tab round. This is done by border-top-left-radius:.5em and border-top-right-radius:.5em. 〔►see CSS: Round Corners: border-radius

Like what you read? Please tell friends, share link, or Buy JavaScript in Depth. Thanks.