Simple jQuery tabs

Posted on July 22, 2009 by Ashley

This tutorial looks into creating a really simple tabbed interface using just plain HTML and CSS with the functionality and interface manipulated using jQuery. Tabbed interfaces are great for maximizing the amount of space you have on your site.

Lets take a look at whats going on. We start with the document ready function. The first line of code hides all the tab containers, we then show the first one, and add the class ‘active’ to the first ‘li’ in the tab headers. Then whenever a tab is clicked it simply remove the class ‘active’ from all the tabs, then add it to the tab thats been clicked. We then take the href from the clicked tab and show it. Simple as that! You can get the complete file by grabbing the download and the demos at the bottom also.

The Code

$(document).ready(function(){
$('#tabs div').hide();
$('#tabs div:first').show();
$('#tabs ul li:first').addClass('active');

$('#tabs ul li a').click(function(){
$('#tabs ul li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div').hide();
$(currentTab).show();
return false;
});
});

The HTML

 <div id="tabs">
    <ul>
      <li><a href="#tab-1">This is Tab 1</a></li>
      <li><a href="#tab-2">Tab Two</a></li>
      <li><a href="#tab-3">Tab Three</a></li>
      <li><a href="#tab-4">Tab Four</a></li>
    </ul>
    <div id="tab-1">
      <h3>This is a really simple tabbed interface</h3>
      <p><img src="http://papermashup.com/demos/jquery-gallery/images/t1.png" width="120" height="120" class="thumbs"/> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur enim. Nullam id ligula in nisl tincidunt feugiat. Curabitur eu magna porttitor ligula bibendum rhoncus. Etiam dignissim. Duis lobortis porta risus. Quisque velit metus, dignissim in, rhoncus at, congue quis, mi. Praesent vel lorem. Suspendisse ut dolor at justo tristique dapibus. Morbi erat mi, rutrum a, aliquam nec, mattis semper, leo. Maecenas blandit risus vitae quam. Vivamus ut odio. Pellentesque mollis arcu nec metus. Nullam bibendum scelerisque turpis. Aliquam erat volutpat. <br/>
        <a href="http://feeds2.feedburner.com/AshleyFord-Papermashupcom">Subscribe to my feed here</a> </p>
    </div>
    <div id="tab-2">
      <h3>Tab 2</h3>
      <p><img src="http://papermashup.com/demos/jquery-gallery/images/t2.png" width="120" height="120" class="thumbs"/> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur enim. Nullam id ligula in nisl tincidunt feugiat. Curabitur eu magna porttitor ligula bibendum rhoncus. Etiam dignissim. Duis lobortis porta risus. Quisque velit metus, dignissim in, rhoncus at, congue quis, mi. Praesent vel lorem. Suspendisse ut dolor at justo tristique dapibus. Morbi erat mi, rutrum a, aliquam nec <br/>
        <a href="http://feeds2.feedburner.com/AshleyFord-Papermashupcom">Subscribe to my feed here</a></p>
    </div>
    <div id="tab-3">
      <h3>Tab 3</h3>
      <p><img src="http://papermashup.com/demos/jquery-gallery/images/t3.png" width="120" height="120" class="thumbs"/> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur enim. Nullam id ligula in nisl tincidunt feugiat. Curabitur eu magna porttitor ligula bibendum rhoncus. Etiam dignissim. Duis lobortis porta risus. Quisque velit metus, dignissim in, rhoncus at, congue quis, mi. Praesent vel lorem. Suspendisse ut dolor at justo tristique dapibus. Morbi erat mi, rutrum a, aliquam nec, mattis semper, leo. Maecenas blandit risus vitae quam. Vivamus ut odio.<br/>
        <a href="http://feeds2.feedburner.com/AshleyFord-Papermashupcom">Subscribe to my feed here</a></p>
    </div>
    <div id="tab-4">
      <h3>Tab 4</h3>
      <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur enim. Nullam id ligula in nisl tincidunt feugiat. Curabitur eu magna porttitor ligula bibendum rhoncus. Etiam dignissim. Duis lobortis porta risus. Quisque velit metus, dignissim in, rhoncus at, congue quis, mi. Praesent vel lorem. Suspendisse ut dolor at justo tristique dapibus. Morbi erat mi, rutrum a, aliquam nec, mattis semper, leo. Maecenas blandit risus vitae quam. Vivamus ut odio. Pellentesque mollis arcu nec metus.<br/>
        <a href="http://feeds2.feedburner.com/AshleyFord-Papermashupcom">Subscribe to my feed here</a></p>
    </div>
  </div>

demodownload

This entry was posted in CSS, Design, Downloads, Javascript, Tutorials, jQuery, popular and tagged , , , , . Bookmark the permalink.
Comments
35 discussions around Simple jQuery tabs
  1. Simple is definitely the key word here, fantastic tutorial and very easy to follow.

  2. Ben says:

    Cool, I was looking to do something like this a few days ago. Ended up using the jQuery UI though

  3. mahesh says:

    Nice tutorial,
    Nice demo,
    Can you explain how to implement browser history management for tabs with cookie,
    i am awaiting to your post

  4. Pingback: Simple jQuery tabs | Papermashup.com « Netcrema - creme de la social news via digg + delicious + stumpleupon + reddit

  5. Pingback: 22 fresh design and development related links for you ;) « Adrian Zyzik’s Weblog

  6. Cool! Thanks for the tut :)

  7. Jason Warner says:

    This one seems more intuitive and clear:

    http://www.sohtanaka.com/web-design/simple-tabs-w-css-jquery/

  8. Ashley says:

    @jason I think you’ll find the code is pretty much exactly the same. but thanks for the link I’m sure people will find it useful :)

  9. Maybe adding the following:
    a { outline:none; }

    Will help with the tabs showing the outline.

  10. Great simple tab list, will re-work for a site i’m currently working on!

  11. Whack Design says:

    Great post! Thanks for taking the time to write it. I have one question for you, I want some of the ‘s in the to actually link to outside links how can I do that?

    Thanks in advance

  12. Ashley says:

    @whackdesign thanks for the comment, didn’t get the question as the code was removed automatically though :(

  13. One Suggestion:
    add the immediate-child > selector to the rules

    $(‘#tabs > div’).hide();
    instead of
    $(‘#tabs div’).hide();

    In case the user wants to put another element inside of one of the tabs.
    Otherwise – perfect, simple, thanks!

  14. That was, another <div> element ;-)

  15. Marc Mcleod says:

    Is it possible to link directly to one of the tabs? E.g. If I wanted to link directly to tab #3 from another page, how could this be achieved?

  16. Ashley says:

    @Marc yeah it would be possible i’d need to dig out an example of that online

  17. Vicky says:

    NICE Graphics..Thanks for the TUT.

  18. Pingback: Dubo.cl » Blog Archive » links for 2009-09-22

  19. slacey says:

    Thanks for this! It’s exactly what I needed and works very well. You said you could dig out an example of linking directly to a certain tab from an outside page. Could you please do that? I really need to do this and can’t find the code for this specific tab system.

    Please and thank you!

  20. Ashley says:

    @slacey here’s an example of jQuery tabs where you can link directly to a specific tab:

    Demo: http://blemble.com/creative/jquery-tabbed-interface/
    Tutorial: http://blemble.com/2009/01/comprehensive-introduction-to-jquery/

    Hope that helps.

    Ash

  21. slacey says:

    Thanks Ashley! I think this will work, although I’m bummed I have to change all my code. Your tabs were so nice and clean!

    But thank you for helping me find this one.

  22. Ashley says:

    @slacey no problem :) depending on your JavaScript knowledge you might be able to edit my tabs to work with the demo i posted.

    at a glance you would need to add the below code:

    // Determine which tab should show first based on the URL hash
    var panelLocation = location.hash.slice(1);
    if(panelLocation){
    var panelNum = panelLocation;
    }else{
    var panelNum = ’1′;
    }

  23. joseph says:

    thanks for your great example
    I don’t know to to call it in your code? so, i need to access tab-3 from another page ? what can i do to make the page open in this tab?

  24. Jacob Miller says:

    I would like to know if its possible to have scroll bars in jQuery tabs?

  25. Ashley says:

    @Jacob you could just set the contents of the container div to overflow:scroll; with a bit of CSS make sure you set a height on the div and you’ll see the scroll bar on the right.

  26. uppercanuck says:

    Nice work! Has anyone modified the script to fade out a tab on mouseout?

  27. wpfeed says:

    the best jQuery tabs to use!!!

  28. audrey says:

    hello,

    thanks for the tutorial !
    i’m trying to put some columns of text inside the tab-1 but when I use a div inside the tab div it does not appear.

    someone in the forum suggested to write $(’#tabs > div’).hide();
    instead of
    $(’#tabs div’).hide();

    I did it but I still don’t manage to put another div element inside one of the tabs…

    what should I do ?

    thank you for your help,

    audrey

  29. Ashley says:

    @Audrey i cant understand why you’re having problems. If you like you can email me your code to ashley at dotdashcreate dot com and i can take a look for you.

  30. Earl P says:

    @Ashley, nice code! I’m going to replace some spry ish I have right now. I have nested tabs that I need to link to from another page, this should work. I hoping to be able to pass something like: blabla.html?tab=1#TabbedPanels1&#4

    I’ll let you know if it works….
    thanks.
    E.P.

  31. Earl P says:

    @Ashley, It works, but a bit different then I thought.

    the string is blabla.html?tab=1&#4

    One weird thing is that once you pass the string to tab blabla.html?tab=1&#4, I can’t seem to pass to another tab from the url:

    example: if I type in blabla.html?tab=1&#2 from the blabla.html?tab=1&#4 page… it won’t pass. I’m not sure I’ll need to, but something I noticed when playing around with it. nice tutorial.

    -E.P.

  32. Pingback: Twitted by delicious50

  33. Pingback: jQuery: збірка плагінів та туторіалів |Створення сайтів, Seo| Мегаблог

  34. blogregator says:

    Great tutorial! Thanks!

  35. Aman says:

    hi
    your tutorial is really helpful. I have also 15+ eyes Caching Jquery tabs please see here:-

    http://jquery13.blogspot.com/2010/08/15-eyes-catching-jquery-tabs.html

    Thanks
    Aman

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribers
1,250
Twitter
510
Comments
1,207
Posts
125