Simple tab controls using jQuery

Sep 18, 2012

Tabbed content using jQuery

Today we are going to create simple notebook tab controls using the jQuery library for Javascript plus a little HTML and CSS markup.

If you have never used jQuery, you can start learning about it here. For today’s purposes we are just using it to locate some DOM elements and manipulate their CSS classes and attributes.


To start with we need a small amount of appropriate HTML markup, like so.

<div id="tab-container">  
    <ul class="tab-menu">  
        <li id="html" class="active">HTML</li>  
        <li id="css">CSS</li>  
        <li id="javascript">Javascript</li>  
    <div class="clear"></div>  
    <div class="tab-top-border"></div>
    <div id="html-tab" class="tab-content active">
        <p>This links to <a href="">1995</a> when HTML made sense.</p>
    <div id="css-tab" class="tab-content">
        <p>You should check out the <a href="">Zen Garden</a>.</p>
    <div id="javascript-tab" class="tab-content">  
        <h1>Javascript ninjas</h1>
        <p>We will teach you how to be a web <a href="">ninja</a>. Your Javascript skills
        will actually be so extreme you will be forbidden from leaving
        the country due to arms export laws.</p>


Then we will add some CSS. Note that the demo CSS also actually includes CSS reset also.

    background: #fff;
    clear: both;
    height: 0;
    visibility: hidden;
    display: block;
a {
    text-decoration: none;
    font-family: Arial,  Verdana, Helvetica, sans-serif;
    font-size: 12px;
    margin: 3em auto;
    width: 500px;
    overflow: hidden;
#tab-container ul{
    list-style: none;
    list-style-position: outside;
    width: 100%;
#tab-container li{
    display: block;
    float: left;
    position: relative;
    font-weight: 700;
    padding: 5px 10px 5px 10px;
    background: #eee;
    border: 1px solid #ddc;
    border-bottom: none;
    border-width: 1px;
    color: #999;
    cursor: default;
    height: 14px;
    margin-bottom: -1px;
    margin-right: 5px;
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
    background: #fff;
    color: #0088CC;
    height: 15px;
    border-bottom: 0;
.tab-top-border {
    border-bottom: 1px solid #d0ccc9;
    margin: 0 auto;
    background: #efefef;
    background: #fff;
    border: 1px solid #ddc;
    border-top-style: none;
    text-align: left;
    padding: 10px;
    padding-bottom: 20px;
    font-size: 11px;
    display: none;
    height: 250px;
    display: block;
.tab-content h1{
    line-height: 1em;
    height: 28px;
    font-size: 22px;

The Javascript

And lastly we add the Javascript. Basically when someone clicks one of the tabs we hide the last one and fade in the new content using jQuery. You could play around with this and use a different jQuery animation technique (or none), but I like the understated effect of the fade in.

This code registers an event to be run on DOM ready, which means when the browser has finished loading the Document Object Model more or less. That event handler in turn registers a click handler for all list item elements beneath a tab-menu class element.

When the click handler runs it finds the id of the new active tab element, sets all others to no longer be “active”, hides all inactove tab bodies, and then adds the “active” class to the tab control which was clicked and runs the jQuery method fadeIn() on the tab body element corresponding to the tab which was clicked.

    var activeTabIndex = -1;
    var tabNames = ["html","javascript","css"];

    $(".tab-menu > li").click(function(e){
        for(var i=0;i<tabNames.length;i++) {
            if( == tabNames[i]) {
                activeTabIndex = i;
            } else {
                $("#"+tabNames[i]+"-tab").css("display", "none");
        return false;


Click here to see the tabs in action.