CSS: Tabbed Menu
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="tabsMenu"> <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>
Put this into the top of page B:
<ul id="pageB" class="tabsMenu"> <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>
Put this into the top of page C:
<ul id="pageC" class="tabsMenu"> <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>
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 is 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 is the complete CSS code for all the pages:
ul.tabsMenu { list-style:none; margin:1em; padding-bottom:5px; border-bottom:6px solid red; width: 70vw; } ul.tabsMenu li { display:inline; width:100px; height:20px; padding:9px; margin:3px; text-align:center; border-top-left-radius:.5rem; border-top-right-radius:.5rem; } ul.tabsMenu a:link {text-decoration:none; color:black} ul.tabsMenu a:visited {text-decoration:none; color:black} #pageA #a {background-color:red; padding-bottom: 9px; } #pageA #b {background-color:silver} #pageA #c {background-color:silver} #pageB #a {background-color:silver} #pageB #b {background-color:red; padding-bottom: 9px; } #pageB #c {background-color:silver} #pageC #a {background-color:silver} #pageC #b {background-color:silver} #pageC #c {background-color:red; padding-bottom: 9px; }
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 li
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 radius. 〔see CSS: Round Corners〕