Simple Tabs w/ CSS & jQuery

Blog » CSS/XHTML » Simple Tabs w/ CSS & jQuery

Simple Tabs w/ CSS & jQuery

PrintJuly 1st, 2009

I know there are quite a few tutorials out there that demonstrate how to create tabs with CSS & jQuery, but I decided to create my own as well. I’m not sure if the techniques are the same, but hopefully this tutorial will be easy to understand even for a beginner.

For those who are not familiar with jQuery, check out their site for a general overview, and you can also follow up with the various tutorials out there.
Easy Tabs Tutorial with CSS & jQuery

Step1. Wireframe – HTML & CSS

Use an unordered list for your tabs, and follow up with the “tab_container” container right below it. Make note that each list item (tabs) has an attribute of “href” that matches the ID of the “.tab_content” div. This will be very important once we have jQuery pull off the actions. Also keep in mind that I used generic names like “tab1″ so its easier to understand. In reality, you should be using keywords so it can semantic and also benefit you in SEO.

HTML

<ul class="tabs">
    <li><a href="#tab1">Gallery</a></li>
    <li><a href="#tab2">Submit</a></li>
</ul>

<div class="tab_container">
    <div id="tab1" class="tab_content">
        <!--Content-->
    </div>
    <div id="tab2" class="tab_content">
       <!--Content-->
    </div>
</div>

If you have tried to create tabs before with CSS, you probably have experienced some frustration with getting the borders on the tabs correctly aligned. Below is a common problem that most people will run into.

Easy Tabs Tutorial with CSS & jQuery

Here is a solution I came up with that took care of this annoying issue. Check out the image below and also take a look at the CSS and its supporting comments for a better understanding.

Easy Tabs Tutorial with CSS & jQuery

Tabs CSS

ul.tabs {
	margin: 0;
	padding: 0;
	float: left;
	list-style: none;
	height: 32px; /*--Set height of tabs--*/
	border-bottom: 1px solid #999;
	border-left: 1px solid #999;
	width: 100%;
}
ul.tabs li {
	float: left;
	margin: 0;
	padding: 0;
	height: 31px; /*--Subtract 1px from the height of the unordered list--*/
	line-height: 31px; /*--Vertically aligns the text within the tab--*/
	border: 1px solid #999;
	border-left: none;
	margin-bottom: -1px; /*--Pull the list item down 1px--*/
	overflow: hidden;
	position: relative;
	background: #e0e0e0;
}
ul.tabs li a {
	text-decoration: none;
	color: #000;
	display: block;
	font-size: 1.2em;
	padding: 0 20px;
	border: 1px solid #fff; /*--Gives the bevel look with a 1px white border inside the list item--*/
	outline: none;
}
ul.tabs li a:hover {
	background: #ccc;
}
html ul.tabs li.active, html ul.tabs li.active a:hover  { /*--Makes sure that the active tab does not listen to the hover properties--*/
	background: #fff;
	border-bottom: 1px solid #fff; /*--Makes the active tab look like it's connected with its content--*/
}

Tab Content CSS

.tab_container {
	border: 1px solid #999;
	border-top: none;
	overflow: hidden;
	clear: both;
	float: left; width: 100%;
	background: #fff;
}
.tab_content {
	padding: 20px;
	font-size: 1.2em;
}

Step 2. Activate the Tabs – jQuery

For those who are not familiar with jQuery, check out their site for a general overview.
The following script contains comments explaining which jQuery actions are being performed.

$(document).ready(function() {

	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

});

Easy Tabs Tutorial with CSS & jQuery

Conclusion

So there you have it, a nice and simple tab function with CSS and jQuery. If you have any questions, comments, or suggestions, please feel free to let me know!

Did You Enjoy This Post?

subscribeSubscribe via RSS ,by email, or by twitter to get all upcoming
tutorials and articles delivered straight to you.

Bookmark or Share this Post!

  • Digg
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Twitter
  • E-mail this story to a friend!

Tags: , ,

This entry was posted on Wednesday, July 1st, 2009 at 12:18 am and is filed under CSS/XHTML. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

119 Responses to “Simple Tabs w/ CSS & jQuery”

  1. Smooth Criminal

    I have one issue thou: sidebar is causing layout to move horizontally, i is worth mention because of impact on user experience.

    Non the less i like the effect! Good work!

  2. Mark Gibbens

    Thanks very much for this. Very nice attention to detail with the borders.

    There is one slight downside, though I’m sure it’s easy to solve. If you’re not using JavaScript, you’re going to be stuck with an irrelevant navigation list. Perhaps the anchors could point to the tab content (#tab1, #tab2) so they break down to useful in-page navigation. Or perhaps the entire tab navigation could be added with jQuery so it doesn’t appear at all without JS?

  3. Puchki

    The tabs look extremely good but only problem is that the content area jumps when we swicth tabs

  4. Christian

    Hey, nice tutorial!

    I´ve just one thing to talk about… If JavaScript is off, the unordered list is viewed completely. This is nice and good for accessibility. But after clicking one tab, nothing happens. Might it be a opportunity to insert jump marks. So Users will also have an action by clicking on the tab and they do not need to scroll down by hand…

    What do you think about it?

    Greets Christian

  5. mynameisorman

    Nice post. Nice and simple. Going into my jQuery bookmarks along with some of your other posts.

    Thanks.

  6. Yasser

    thanks alot :)

    good tutorial , easy to implement …

  7. Kadir GÜNAY

    Hi,
    This is so nice work. So simple so useful. Thank you Soh :)

  8. mike

    Nice job on this one. I’ve got to know: Is that a Hillbilly lorem ipsum generator you’re using? I’d love to use it as well; much more entertaining than random Greek. :D

  9. Christoph

    And again,
    a very useful tutorial. Please keep on doing this work for us :)
    I already learned very much just because of your tutorials.

    Christoph

  10. Soh

    This is why I learn so much more from you guys and your feed back :-)

    Thanks! I just adjusted the script + html, it should anchor down when js is disabled :-)

    @mike, haha yea I usually go to http://www.duckisland.com/GreekMachine.asp for my lorem ipsum~

  11. Helder

    Nice work!

  12. Bob Hay

    Some people have complained about the content jumping. This will happen if the content area for a particular tab is taller than the page, which will cause the vertical scroll bar to appear. One solution to this is to float an empty div to the page that is 1px wide and 2000px tall. That way the scroll bar will be there regardless of the height of the content area.

  13. Soh

    Thanks Bob for noting that~

    My solution to take care of the vertical scroll would be:

    html {
    overflow-y: scroll;
    }

    this way, the area for the scroll will be filled whether or not there is a need for one~

  14. banny

    hii
    great tut
    help me plz with a problem
    i want to have a lil pics with names of the tabs andddddddd
    i want the name of tabs to be random like there should be different tab name n its assciated pic on first tab n selected n so on everytime theres a page visit or its refreshed
    thnxxx

  15. Alex

    Thanks for the great tutorial! It’s awesome =]

    Is there a way to put some text on top of the menu? right in front of contact for example, like: <- this is a tabbed menu…ya rly

    thanks again =]

  16. JustGage

    Does anyone know why when you use slideDown() it has this funky POP where it slows down like it’s going to stop then it just POPs all the way open?

  17. Utopie | Liverpool Websites

    Nice tips. I have an issue where if I have a div, etc, with a boarder on all 4 sides, the div is relative, and then I have an image inside which I make absolute and position is so that it break out of the top of the div, the top div boarder disapears in IE… do you know why that is?

  18. Soh

    @banny, I’m not sure if I’m understanding you correctly, can you please rephrase your question?

    @alex you can wrap your additional text in a div (nest it inside the list item) and assign it some absolute positioning to make it stick to where you want. That might be the easiest way~

    @JustGage do you have a demo I can look at?

    @Utopie If I’m understanding you correctly, it may be that your content div is layered on underneath your tabs (which hides the top border). But do keep in mind in this tutorial I mentioned the what seems to be the top border of the content div, is actually the bottom border of the tab ul. Hope I’m not confusing you hehe. Take a look at the supporting images to get a visual.

  19. bl@nkster

    nice tutorial.. let’s tray!

  20. John

    Hi and thx for a great script. Is there a solution to prevent the content from jumping every time the tabs are click´d ?. I want it to stay the same place and not “moving” up. Thx again.

  21. Frederik Heyninck

    Hi Soh

    The history doesn’t work, when i go to http://www.sohtanaka.com/web-design/examples/tabs/#tab2 in safari it doesn’t focus on the second tab.

  22. max

    hi soh hi guys ,
    i got a problem with this script:
    I want to use the tabs with different heights without.. oh.. sry..
    hi john :D

    ye a solution for this would be perfect..
    i try my best to solve it but if anyone of you get it faster it would be great

    pretty nice script and site anyway go on soh
    greets max from germany

  23. Soh

    @John, sorry I forgot to add one line of js here :

    return false; (updated on above code sample)

    this will prevent the browser from trying to jump to the anchor link~

    @Frederik can you tell me which version of safari you are on? I am checking in safari now and it seems to be working fine for me~

    @max I am assuming it was the same issue with John? Hope that fixes your issue :)

  24. mojaam

    This is incredible and creative. I’ve been wanted to have a lot more widgets on my blog’s sidebar but didn’t want it to be too cluttered and so I been thinking of some kind of collapsable solution and then this comes along. I tweaked a few things and voila, everything works even better than I imagined now. Thanks for sharing man!

  25. Soh

    @mojaam glad you found it useful!

  26. Mark

    Hi Soh,

    Great work! I’m testing your tabs and it works in FF but in IE7 the bottom border on the tabs is showing. I’ve tried changing the height on ul.tabs li to 32px then it works.

    And addition maybe you’ve already read about the FadeIn glitch of Jquery. In IE the content is not rendered in ClearType.

    maybe you can add this line in the fadeIn callback.
    if(jQuery.browser.msie) this.style.removeAttribute(’filter’);

    Cheers mate! Great piece.

  27. blank shot

    Excellent work!

    Is it possible adapt this code to make the tabs run vertical, like the jquery.ui plugin – http://jquery-ui.googlecode.com/svn/trunk/demos/tabs/vertical.html, and post it up here as an update/addendum??

    coooool!

  28. Soh

    Thanks Mark for the fadeIn callback :-)

    @blank shot, you can just switch up the css to make the tabs vertical and not horizontal: http://www.sohtanaka.com/web-design/side-navigation-tooltip-popup-bubble/ maybe you can take some of that tut’s css and modify it. Let me know how that goes~

  29. blank shot

    hows this??

    apologies now for the amount of code ;)

    Simple Tabs with CSS & jQuery

    body {
    background: #f0f0f0;
    margin: 0;
    padding: 0;
    font: 10px normal Verdana, Arial, Helvetica, sans-serif;
    color: #444;
    }
    h1 {font-size: 3em; margin: 20px 0;}
    .container {width: 700px; margin: 10px auto; float:left;}
    ul.tabs {
    margin: 45px 0px 0px 0px;
    padding: 0;
    float: left;
    }
    ul.tabs li {
    margin: 0;
    padding: 0;
    height: 31px;
    line-height: 31px;
    border: 1px solid #999;
    margin-right: -1px;
    margin-bottom: -1px;
    background: #e0e0e0;
    overflow: hidden;
    position: relative;
    }
    ul.tabs li a {
    text-decoration: none;
    color: #000;
    display: block;
    font-size: 1.2em;
    padding: 0 20px;
    border: 1px solid #fff;
    outline: none;
    }
    ul.tabs li a:hover {
    background: #ccc;
    }
    html ul.tabs li.active, html ul.tabs li.active a:hover {
    background: #fff;
    border-right: 1px solid #fff;
    }
    .tab_container {
    border: 1px solid #999;
    clear: both;
    float: left;
    width: 100%;
    background: #fff;
    -moz-border-radius-bottomright: 5px;
    -khtml-border-radius-bottomright: 5px;
    -webkit-border-bottom-right-radius: 5px;
    -moz-border-radius-bottomleft: 5px;
    -khtml-border-radius-bottomleft: 5px;
    -webkit-border-bottom-left-radius: 5px;
    -moz-border-radius-topleft: 5px;
    -khtml-border-radius-topleft: 5px;
    -webkit-border-top-left-radius: 5px;
    -moz-border-radius-topright: 5px;
    -khtml-border-radius-topright: 5px;
    -webkit-border-top-right-radius: 5px;
    }
    .tab_content {
    padding: 20px;
    font-size: 1.2em;
    }
    .tab_content h2 {
    font-weight: normal;
    padding-bottom: 10px;
    border-bottom: 1px dashed #ddd;
    font-size: 1.8em;
    }
    .tab_content h3 a{
    color: #254588;
    }
    .tab_content img {
    float: left;
    margin: 0 20px 20px 0;
    border: 1px solid #ddd;
    padding: 5px;
    }

    $(document).ready(function() {

    //Default Action
    $(”.tab_content”).hide(); //Hide all content
    $(”ul.tabs li:first”).addClass(”active”).show(); //Activate first tab
    $(”.tab_content:first”).show(); //Show first tab content

    //On Click Event
    $(”ul.tabs li”).click(function() {
    $(”ul.tabs li”).removeClass(”active”); //Remove any “active” class
    $(this).addClass(”active”); //Add “active” class to selected tab
    $(”.tab_content”).hide(); //Hide all tab content
    var activeTab = $(this).find(”a”).attr(”href”); //Find the rel attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active content
    return false;
    });

    });

    Gallery
    Submit
    Resources
    Contact

    <!–Simple Tabs w/ CSS & jQuery –>

    Gallery

    http://www.DesignBombs.com
    Saw polecat than took bankrupt good hillbilly stew, crazy, fancy and hillbilly heap rodeo, pappy. Thar range saw me him sherrif nothin’ shiney dirt, pigs sheep city-slickers everlastin’ shotgun driveway. Promenade catfight fart fiddle jiggly gonna tarnation, fence, what quarrel dirty, if. Pot grandma crop kinfolk jezebel diesel coonskin hoosegow wirey fixin’ shack good roped in. Reckon stew tax-collectors, grandpa tobaccee hayseed good wash tired caboodle burnin’ landlord.

    Smokin’ driveway wrestlin’ go darn truck moonshine wirey cow grandpa saw, coonskin bull, java, huntin’.

    Stinky yonder pigs in, rustle kinfolk gonna marshal sittin’ wagon, grandpa. Ya them firewood buffalo, tobaccee cabin.

    Submit

    http://www.DesignBombs.com
    Grandma been has bankrupt said hospitality fence everlastin’ wrestlin’ rodeo redblooded chitlins marshal. Boobtube soap her hootch lordy cow, rattler.

    Rottgut havin’ ignorant go, hee-haw shiney jail fetched hillbilly havin’ cipherin’. Bacon no cowpoke tobaccee horse water rightly trailer tools git hillbilly.

    Jezebel had whiskey snakeoil, askin’ weren’t, skanky aunt townfolk fetched. Fit tractor, them broke askin’, them havin’ rattler fell heffer, been tax-collectors buffalo. Quarrel confounded fence wagon trailer, moonshine wuz, city-slickers fixin’ cow.

    Resources

    http://www.DesignBombs.com
    Dirt tools thar, pot buffalo put jehosephat rent, ya pot promenade. Come pickled far greasy fightin’, wirey, it poor yer, drive jig landlord. Rustle is been moonshine whomp hogtied. Stew, wirey stew cold uncle ails. Slap hoosegow road cooked, where gal pot, commencin’ country. Weren’t dogs backwoods, city-slickers me afford boxcar fat, dumb sittin’ sittin’ drive rustle slap, tornado. Fuss stinky knickers whomp ain’t, city-slickers sherrif darn ignorant tobaccee round-up old buckshot that.

    Deep-fried over shootin’ a wagon cheatin’ work cowpoke poor, wuz, whiskey got wirey that. Shot beer, broke kickin’ havin’ buckshot gritts. Drunk, em moonshine his commencin’ country drunk chitlins stole. Fer tonic boxcar liar ass jug cousin simple, wuz showed yonder hee-haw drive is me. Horse country inbred wirey, skanky kinfolk. Rattler, sittin’ darn skanky fence, shot huntin’.

    Contact

    http://www.DesignBombs.com
    Grandma been has bankrupt said hospitality fence everlastin’ wrestlin’ rodeo redblooded chitlins marshal. Boobtube soap her hootch lordy cow, rattler.

    Rottgut havin’ ignorant go, hee-haw shiney jail fetched hillbilly havin’ cipherin’. Bacon no cowpoke tobaccee horse water rightly trailer tools git hillbilly.

    Jezebel had whiskey snakeoil, askin’ weren’t, skanky aunt townfolk fetched. Fit tractor, them broke askin’, them havin’ rattler fell heffer, been tax-collectors buffalo. Quarrel confounded fence wagon trailer, moonshine wuz, city-slickers fixin’ cow.

  30. blank shot

    Ive done something but your comments box wont allow me to post the source code directly in.. unless theres some tags i can use??

    [quote]Quote tags test[/quote]

    [code]Code tags test[/code]

  31. fritzek

    thx, soh, for this job.
    here is a slight modification if someone needs to jump into a certain tab from outside:
    $(document).ready(function() {

    //Default Action
    $(”.tab_content”).hide(); //Hide all content
    if(location.hash != “”) {
    var target = location.hash.split(”#”)[1]
    $(location.hash).show(); //Show first tab content
    $(”ul.tabs li:has(a[rel="+target+"])”).addClass(”active”).show();;
    } else {
    $(”ul.tabs li:first”).addClass(”active”).show(); //Activate first tab
    $(”.tab_content:first”).show(); //Show first tab content
    }

    //On Click Event
    $(”ul.tabs li”).click(function() {
    $(”ul.tabs li”).removeClass(”active”); //Remove any “active” class
    $(this).addClass(”active”); //Add “active” class to selected tab
    $(”.tab_content”).hide(); //Hide all tab content
    var activeTab = $(this).find(”a”).attr(”href”); //Find the rel attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active content

    });

    });

    it’s a bit hackish but it works

  32. feha

    Great, how easy is to make it with cookies that remembers the state ?
    I could make it with ui library, but i like more your option …

  33. Soh

    @blank shot, try this link: http://www.elliotswan.com/postable/ it will convert code for you~

    @fritzek thanks for that! I’m sure people will find that useful :-)

    @feha I would take a look at something like this: http://plugins.jquery.com/project/Cookie

  34. feha

    I have used that with JQuery UI tabs, but don’t know how to implement it in your code …
    I’m not that good with JQuery :-)

  35. feha

    This is what i use with UI tabs
    // Tabs
    $('#tabs').tabs({ cookie: { expires: 30 } });

    can i use same with your code ?

  36. feha

    I really would like to get it work with cookies both this tabs
    and nice
    http://www.sohtanaka.com/web-design/easy-toggle-jquery-tutorial/

    thank you
    :-)

  37. blank shot

    ha, how odd, that first post didnt come up at first.. hence the second post..

    oh well, at least its up now ;)

  38. banny

    hi here i am putting my qs again
    i was saying that
    i wanted to have little pictures associated with the tabs names how to do it? and i wanted the tabs names to be appeared on random every time a user refreshes the page or there is a new visitor
    the tabs should be selected at random for example when u go to example page
    the gallery is selected what i want to do is that every time a person visits the site there shud be different tab in location of gallery for example in ur demo its gallery but how cld we modify it that next time when i visit the demo its submit at the first place selected and the gallery is at last place or some where else
    and next time its resources selected and other tabs are on other places i hope u get what i mean :D and then there should be little pictures associated with tab names for example there could be a flower pic within gallery tab and a lettter tab in submit and so on so plz help me with it thanx a lot

  39. rbo

    Thanks for the simple tutorial, this is exactly what I was looking for :)

  40. swenflea

    how can you add more tabs dynamically by pushing a button on your website?

  41. ScriptPlazza

    nice and simple tutorial good work

  42. Dale Novak

    In reference to the issue with ie and fonts, I suggest the following fix:

    replace

    $(activeTab).fadeIn();

    with

    if $.browser.msie)
    {$(activeTab).show();}
    else
    {$(activeTab).fadeIn();}
    return false;

    You will loose the fade effect in ie but the tab navigation will continue to work without loosing cleartype on the fonts.

  43. Dale Novak

    Sorry
    Corrected comment

    In reference to the issue with ie and fonts, I suggest the following fix:

    replace

    $(activeTab).fadeIn();

    with

    if ($.browser.msie)
    {$(activeTab).show();}
    else
    {$(activeTab).fadeIn();}

    You will loose the fade effect in ie but the tab navigation will continue to work without loosing cleartype on the fonts.

  44. John

    Thx mate, ur the man =). Cheers – John.

  45. banny

    no reply ??? any one?

  46. blank shot

    New question: Is there any way you can target multiple containers/content areas to change with just one tab click? ..the same way you can with naming an iFrame??

    $(document).ready(function() {

    //When page loads…
    $(”.tab_content”).hide(); //Hide all content
    $(”.tab_content2″).hide(); //Hide all content
    $(”ul.tabs li:first”).addClass(”current”).show(); //Activate first tab
    $(”.tab_content:first”).show(); //Show first tab content
    $(”.tab_content2:first”).show(); //Show first tab content2

    //On Click Event
    $(”ul.tabs li”).click(function() {

    $(”ul.tabs li”).removeClass(”current”); //Remove any “active” class
    $(this).addClass(”current”); //Add “active” class to selected tab
    $(”.tab_content”).hide(); //Hide all tab content
    $(”.tab_content2″).hide(); //Hide all tab content2

    var activeTab = $(this).find(”a”).attr(”href”); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
    });

    });

    ??????????

  47. jan Schrieber

    I’m sure this is a simple question, how do I change the font color for the active tab? I can’t seem to get it to work.
    thanks

  48. banny

    hi here i am putting my qs again
    i was saying that
    i wanted to have little pictures associated with the tabs names how to do it? and i wanted the tabs names to be appeared on random every time a user refreshes the page or there is a new visitor
    the tabs should be selected at random for example when u go to example page
    the gallery is selected what i want to do is that every time a person visits the site there shud be different tab in location of gallery for example in ur demo its gallery but how cld we modify it that next time when i visit the demo its submit at the first place selected and the gallery is at last place or some where else
    and next time its resources selected and other tabs are on other places i hope u get what i mean :D and then there should be little pictures associated with tab names for example there could be a flower pic within gallery tab and a lettter tab in submit and so on so plz help me with it thanx a lot

  49. naynesh

    I was wondering if anyone would know how to add a ajaxloader.gif image whilst tabs are still loading.
    But its a great,neat little script.

  50. Steph

    Is there a way to do this with images for the tabs?

    This is my first crack at jQuery and you made it so easy, thanks!!!!

  51. naynesh

    Yes there is Steph.
    Look at this page,ive used background images for all of them using css.
    http://www.ultimatewebnet.com

    For different images for each one you would have to edit each tab ()

  52. Mahbub

    Although i’m a fan of jQuery UI tabs and know all about them, this one is a great start for custom and minimal tab interfaces

  53. banny

    hey i was saying that i wanted to have little pictures associated with the tabs names how to do it? and i wanted the tabs names to be appeared on random every time a user refreshes the page or there is a new visitor
    the tabs should be selected at random for example when u go to example page
    the gallery is selected what i want to do is that every time a person visits the site there shud be different tab in location of gallery for example in ur demo its gallery but how cld we modify it that next time when i visit the demo its submit at the first place selected and the gallery is at last place or some where else
    and next time its resources selected and other tabs are on other places i hope u get what i mean :D and then there should be little pictures associated with tab names for example there could be a flower pic within gallery tab and a lettter tab in submit and so on

  54. Sumit

    hi there

    is it possible to use more of these tabs on one single site without copy & paste the code over and over again and include the same .js with a different name?

    Like
    ———-
    Tab1 Tab2 Tab3
    TABcontent1
    ———

    Tab3 Tab4 Tab5
    Tabcontent2
    ———

    and so on

    possible?

  55. Soh

    @blankshot if you want to have multiple tabs open at the same time, instead of trying to match the tab anchor link to the content ID, if you switch that to match the content class instead, it may work. But I’m not sure how usable that feature would be…

    @Dale thanks for that tip! Noted :-)

    @jan, you can specify text color like this:
    ul.tabs li a {
    text-decoration: none;
    color: red;
    display: block;
    font-size: 1.2em;
    padding: 0 20px;
    border: 1px solid #fff;
    outline: none;
    }

    @naynesh you can try something like this: http://net.tutsplus.com/javascript-ajax/how-to-load-in-and-animate-content-with-jquery/
    James, broke down a section of the tut on how to implement that~

    @steph, yes you can just use css sprites: http://css-tricks.com/css-sprites/

    @sumit, you can have multiple sets of tabs on one page with just calling one jquery/script file. But the logic you showed as your example doesn’t look right to me. Tab1,2,3 would show tab content 1? Maybe I am misunderstanding?

    @banny, it looks like your looking to randomize your tabs. I believe there are plugins out there that pulls this off, and you will have to implement that with this tutorial~

  56. Sumit

    Thanks for you reply
    Okay maybe i wrote it a little bit confusing.
    Hard to explain only with Numbers huh?

    Ok letz say we have one TabSet:
    _______________________
    Tab1 | Tab2 | Tab3
    The tab Content(1,2,3) related to the tabs
    _______________________

    Thats one TabBox with 3 diffrent Tabs and each one of it loads it’s own content. Like every Tab-Script :-)

    Now i want to have that TabBox multiple times on a single site with different contents of course.
    I tried it with:
    http://www.dynamicdrive.com/dynamicindex17/tabcontent.htm

    but when i use that and add the tabs more than once, they won’t work. I have to inlucde the js-File over and over again and have to change the function in it in every file. So that they don’t fight each other (^^,)

    Easy Works:
    1 Tab-Script
    X independent TabBoxes

  57. Sumit

    PS.: check my website sidebar to see what i mean.

  58. Soh

    Oh ok, yea then this should be no problem.

    Just make sure your tab links match with the container ID’s~

  59. banny

    thnx soh
    plz tell me the plug ins i have searched but didnt get any
    tell me the names if u know
    yes ur right i want to randomize the tabs every time the page refresh or loads on a new visit :D

  60. Masey

    Just curious if there’s a way to target specific tabs from links on other pages.

    For example, I click on a “Honda Specs” link on a particular page and the link will open the “Honda Specs” tab on the “Road Bikes” page, for example?

  61. banny

    hey soh and guys
    this is wat i wanna achieve and this is wat i meant by rotation of tabs
    for eg

    tab 1 tab 2 tab 3 tab 4 <— this is on first page load

    tab 2 tab 3 tab 4 tab 1 <—- this is on 2nd page load or refresh

    tab 3 tab 4 tab 1 tab 2 <—– this is on 3rd page load and so on

    or i wont mind a total tab rotation regardless of which tab comes first
    like tab 4 tab 2 tab 1 tab 3 etc i hope u got it wat i meant by rotation
    so plz help me and hint me regarding this thanx

  62. Tom

    I apologize, I don’t know any jQuery, but I feel this can be done simply.

    Is it possible to target the “previous” and “next” tabs with a couple of links (say, a “prev” link & “next” link) outside of the tabbed interface?

  63. Sumit

    Hi there

    Thanks again for you help, i tried and it works better than the other script, but still not perfect.

    E.g.: tab1 of tabset1 is activated on site-load.
    If i click at e.g. tab2 of tabset3, ALL other tab(-sets) are closing. Even tabset1 and tabset2.
    But i want that each tabset has one active tab.

    See the problem in action at the searchresults of my site
    http://www.sumit-online.de/?s=google

  64. John

    @Jan To change the font color of just the active tab, add this

    ul.tabs li.active a
    {
    color: #red;
    }

  65. Nintensity

    Hey, Soh! Nintensity here.
    I’m running into a little problem, utilizing nested LI elements within the Simple Tabs. (concept: Sub-Nav)

    Now, the problem lies within the jQuery code. If I click on a Sub-nav list, all the LI elements, including the Sub-nav’s parent LI element, becomes hidden.

    What I’m trying to acheive is to click on a Sub-nav list, and that not only itself is assigned the “active” class, but it’s parent LI element so it can be displayed. Does this make sense?

  66. Soh

    @Masey I would maybe add a query string to the URL and try to do an if then else/switch statement to target/match your active tab with the query string. Here is an example of how you can choose which tab you want active: http://www.sohtanaka.com/web-design/examples/tabs/index2.htm (resources will be open by default in this example)

    @Tom, I can try to add that on a future tutorial if you would like. That would work more like a carousel mixed in with the tab feature.

    @Sumit, ok I see your issue now, try something like this: http://www.sohtanaka.com/web-design/examples/tabs/index3.htm

    @Nintensity I may be misunderstanding your request, do you mean you want a subnav within the tab? If so it may be bad usability since most tabs behave as tabs and not as primary drop down style navigation~ Let me know~

  67. banny

    well what abt my query?:)

  68. Soh

    Banny, I did a quick search and found similar that may lead you to the right path:

    http://www.designateonline.com/discussions/comments.php?DiscussionID=4258&page=1

    http://stackoverflow.com/questions/1012264/jquery-randomly-fadein-images

    http://snipplr.com/view/13725/random-bg-image/

    If you would like, I can come up with a tutorial for something similar in the future. I just can’t do this now since it would be re-tweaking the original intent of this tutorial~

  69. banny

    thnx soh ur so sweet
    i would love a tut from u
    may be later then:)

  70. danna

    anyone had any issues with the z-index of the tabs class, it is being stacked on top of a menu with a z-index:10 ??? thanks

  71. Nintensity

    ahhh, sorry, let me make it clear,
    basically incorporating tabs inside tabs. is this possible with your jquery code? i’ve always wondered how to implement this.

    -n

  72. Bob Who

    Thanks for code, it is very nice and easy, no idea that it is fun

  73. Ce.

    Thanks for this. I ended up pulling the fade for the project I’m working on, but either way it’s great!

  74. florian

    How can I add the “active” class to the “a” href element in the selected tab? I know the following line of code adds it to the selected tab, but how can I also add to the a element inside the tab?

    $(this).addClass(”active”); //Add “active” class to selected tab

  75. Soh

    @florian

    to target the href inside the list-item/$(this), you can do:

    $(this).find(”a”).addClass(”active”); //Add “active” class to href inside selected tab

    logic:

    $(this) = ul.tabs li
    .find(”a”) = find “a” within ul.tabs li
    .addClass = add class

  76. Pete

    Nice tabs!

    Could you make this rotate thru the tabs? I tried to build off your code but cannot get it to work. Could you also show me how to resume rotating after they hover away from a tab?

    How would I also make it change tabs on hover? when user hovers over a tab it changes to that tab. THANKS

  77. Niko Moustoukas

    Hey, great script – im using this for an a-z jargon – is there any way i can disable a few of the tabs? (tried removing the a link and its still clickable!)

    thanks

  78. banny

    hi soh :)
    well i tried but didnt get it done yet the thing which i asked u before
    i want the tabs to rotate as well but i wanted the tabs to rotate at evry page load as well or page refresh for eg
    tab 1 tab 2 tab 3
    on next page load it shud be like this
    tab 2 tab 3 tab 1 and so on
    how to do it? plz tell us
    thnx

  79. Pete

    Hello Soh

    I figured how to get it to change tabs on hover. Just change the click to hover. Here it is.

    $(”ul.tabs li”).hover(function() {
    $(”ul.tabs li”).removeClass(”active”); //Remove any “active” class
    $(this).addClass(”active”); //Add “active” class to selected tab
    $(”.tab_content”).hide(); //Hide all tab content

    var activeTab = $(this).find(”a”).attr(”href”); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
    });

  80. florian

    Hi Soh.

    Thank you for the line to add active class to the a element inside the ul.tabs li. I tried to include it into my code but the code doesnt seem to work. May I paste my existing code below for you to take a look.

    $(document).ready(function() {

    //When page loads…
    $(”.tab_content”).hide(); //Hide all content
    $(”ul.table-tabs li:first”).addClass(”active”).show(); //Activate first tab
    $(”ul.table-tabs a:first”).addClass(”active”).show(); //Activate first a
    $(”.tab_content:first”).show(); //Show first tab content

    //On Click Event
    $(”ul.table-tabs li”).click(function() {

    $(”ul.table-tabs li”).removeClass(”active”); //Remove any “active” class
    $(”ul.table-tabs a”).removeClass(”active”); //Remove any “active” class
    $(this).addClass(”active”); //Add “active” class to selected tab
    $(”.tab_content”).hide(); //Hide all tab content

    var activeTab = $(this).find(”a”).attr(”href”); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
    });

  81. feha

    Still no cookie version? :-(

  82. Ramil

    Yea, I am patiently waiting for this tab (multiple tab set in one page) and the toggle tutorial (http://www.sohtanaka.com/web-design/easy-toggle-jquery-tutorial/) to have a cookie version. Hope Soh can provide us with the cookie code.

  83. bella

    Hi Soh

    Please help, the tabs in my portfolio site jumps in IE6, it works fine in firefox so I have no clue what to do. I’m not a programmer so you going to have to show me step by step what I can do. Please help :-(

  84. feha

    Hi Soh

    please how to prevent jumping to the top if tab is clicked ?
    it happens when tab is located below/under (long content) or
    browser scroll bars visible …

  85. Jess

    Problem with bottom border = Pain in the ass

    Hilarious Soh. =)

    Thanks for another great tutorial.

    I think I’m going to have to start giving inspiration credit to you with all that your teaching me. Thanks dude.

  86. banny

    soh ur students and fans are waiting for ur response :) plz help us thnk u

  87. banny

    soh i was thinking of the tab 2.0 version with cookies and tab chnges on very refresh like first tab is on last place etc for eg
    if tabs are like this

    tab name 1 tab name 2 tab name 3
    then on next page load or refresh its like
    tab name 2 tab name 3 and tab name 1 and so on
    thnx

    p.s i tried to do it but didnt went well :(

  88. Soh

    Sorry guys, I have been extremely crammed with work recently, and to be honest I won’t be able to update this tut with any extra features for a little while longer~ For those with extra feature requests that do not relate to the original tutorial itself, I apologize for the inconvenience.

    Just as a hint though, I would be using this plugin for jQuery to get this going. http://plugins.jquery.com/project/Cookie

    Also, for those having “jumping” issues, this is due to your content being inconsistent in size with the other tab contents. This is not a jquery issue or a bug, but simply just like the vertical scroll jump (some thought it was a glitch, but it was simply the vertical scroll showing/hiding according to how much content was on the page), its just a matter of your content getting shorter/longer with various amounts of content making it seem like its glitching.

    @Niko, we are targeting the list item in our script to trigger the events, so if you would like to disable that, you would do something like this

    $(”ul.tabs li a”).click(function() {

    $(”ul.tabs li”).removeClass(”active”); //Remove any “active” class
    $(this).parent().addClass(”active”); //Add “active” class to selected tab
    $(”.tab_content”).hide(); //Hide all tab content

    var activeTab = $(this).attr(”href”); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
    });

    Thanks~

  89. banny

    thnx

  90. Chris

    Hello great solution looks good! I’m trying to get this to work inside a facebox link. You can see it working at http://www.acowancreative.com/faceboxtest2.html

    at

    http://www.acowancreative.com/kingmanindex3.html (link in the bottom left)

    No workie :( It will display the first tab then none of the others and won’t return to displaying the first tab

    Any ideas

  91. inderpreet singh

    please help how can i add these cute tabs in blogger blogs plz reply :(

  92. cracks

    a very helpful tute & demo – thanks for making it available Soh.

    i noticed on the multi tab box demo (http://www.sohtanaka.com/web-design/examples/tabs/index3.htm) that the fade effect is lost. anyway to re-implement it when using multi tabbox instances..?

  93. cracks

    ummmm …. cancel last order – .fadeIn fast fixes that.

    while i’m here though …… is it possible / how would you invoke that same fade effect on standard (non-Simple Tabs) anchor points on the same page. i have some long pages, with anchor points in the normal page body (NOT in the Simple Tabs sections) and the fade effect sure is effective.

    Anyone ..?

  94. Satya A

    How if Content for each tab like iframe or ajax, that’s mean if I click something stil in that active tab.

    Thanks

  95. elandy2009

    Thanks for the code! But I needed to tweak the CSS

    So for the curious people, here’s the tweaked CSS:

  96. elandy2009

    Here’s the CSS code:

    ul.tabs {
    margin: 0;
    padding: 0;
    float: left;
    list-style: none;
    height: 32px; /* Set height of tabs */
    border-bottom: 1px solid #999;
    border-left: 1px solid #999;
    width: 100%;
    }
    ul.tabs li {
    float: left;
    margin: 0;
    padding: 0;
    height: 31px; /* Subtract 1px from the height of the unordered list */
    line-height: 31px; /* Vertically aligns the text within the tab */
    border: 1px solid #999;
    border-left: none;
    margin-bottom: -1px; /* Pull the list item down 1px */
    overflow: hidden;
    position: relative;
    background: #e0e0e0;
    }
    ul.tabs li a {
    text-decoration: none;
    color: #000;
    display: block;
    padding: 0 10px;
    border: 1px solid #fff; /* Gives the bevel look with a 1px white border inside the list item */
    outline: none;
    }
    ul.tabs li a:hover {
    background: #ccc;
    }
    html ul.tabs li.active, html ul.tabs li.active a:hover { /* Makes sure that the active tab does not listen to the hover properties */
    background: #fff;
    border-bottom: 1px solid #fff; /* Makes the active tab look like it's connected with its content */
    }
    .tab_container {
    border: 1px solid #999;
    border-top: none;
    clear: both;
    overflow: auto;
    width: 100%;
    background: #fff;
    }
    .tab_content {
    padding: 10px;
    }

  97. elandy2009

    So here’s the tweaked CSS:

    <style type="text/css">
    <!–
    ul.tabs {
    margin: 0;
    padding: 0;
    float: left;
    list-style: none;
    height: 32px; /* Set height of tabs */
    border-bottom: 1px solid #999;
    border-left: 1px solid #999;
    width: 100%;
    }
    ul.tabs li {
    float: left;
    margin: 0;
    padding: 0;
    height: 31px; /* Subtract 1px from the height of the unordered list */
    line-height: 31px; /* Vertically aligns the text within the tab */
    border: 1px solid #999;
    border-left: none;
    margin-bottom: -1px; /* Pull the list item down 1px */
    overflow: hidden;
    position: relative;
    background: #e0e0e0;
    }
    ul.tabs li a {
    text-decoration: none;
    color: #000;
    display: block;
    padding: 0 10px;
    border: 1px solid #fff; /* Gives the bevel look with a 1px white border inside the list item */
    outline: none;
    }
    ul.tabs li a:hover {
    background: #ccc;
    }
    html ul.tabs li.active, html ul.tabs li.active a:hover { /* Makes sure that the active tab does not listen to the hover properties */
    background: #fff;
    border-bottom: 1px solid #fff; /* Makes the active tab look like it's connected with its content */
    }
    .tab_container {
    border: 1px solid #999;
    border-top: none;
    clear: both;
    overflow: auto;
    width: 100%;
    background: #fff;
    }
    .tab_content {
    padding: 10px;
    }
    //–>
    </style>

  98. banny

    where is the css elandy2009

  99. elandy2009

    banny, i’ve tweaked the CSS to make the code better

  100. banny

    thnx e landy
    any running demo mate??

  101. bob

    how would I get a href link on the same page as the tabs to change the tab? Using fritzek example, i grab the hash and use that as the target to show but its not working. Thanks for the help!

    $(”a.pointer”).click(function() {

    var target = $(”a.pointer”).hash.split(”#”)[1]
    //$(location.hash).show(); //Show first tab content
    $(”ul.tabs li:has(a[rel="+target+"])”).addClass(”active”).show();

    });

  102. Eric

    Thanks a lot for the tut bro. Simple and to the point! Do you know of a way to slow down the fade effect (both in and out)? Thanks for your time!

  103. cracks

    i’m with Bob (2 comments up) – how would you get a link that’s inside the tab container content to trigger a tab change ..?

  104. Paula

    Hey, I just wanted to thank you for the great tutorials. Simple or advanced, keep them coming!!

  105. Masey

    @Bob & @Cracks – I think this is the one feature that everyone is really wanting to know how to implement. Having such links would enable you to link directly to a tab from external websites, emails etc and would be a welcomed addition.

    Let’s hope Soh can come up with an add-on solution for us and present it in his usual clear and awesome way. ;)

  106. Nate Woods

    Just a quick question, I have used background images for the tabs, I am using the 3 states norm, hover and active, everything works great except the active state will not show with background-position: left bottom; In fact it will not show no matter what I try. Here is the weird thing: the active a:hover does work.

    Here is my css:
    html ul.tabs li.active, html ul.tabs li.active a:hover
    {background-position: left bottom;}

    And the jQuery is unaltered from this tutorial

    Any thoughts?

    Thanks in adavnce

  107. Soh

    Nate do you have a sample link I can look at?

  108. Nate Woods

    Soh,
    I figured it out, I had the normal state relating to the li a, i took off the a and its all good.
    This site is awesome, thanks so much.

  109. Shaked

    Hey, I want to delegate the “submit” , “reset” and other “form” inputs to my tabs from another page.

    I`v two pages:
    1. tabs.aspx -> the page with the tabs…
    2. all.aspx -> the page with the form (all.aspx.cs has the server side of course…)

    so I entered this code:
    $(’#tabs’).tabs({
    load: function(event, ui) {
    $(’submit’).live(”click”, function() {
    $(ui.panel).load(this.href);
    return false;
    });
    }
    });
    into tabs.aspx and load into my tab the other page -> all.aspx
    —– but when I click on “submit” or “reset” the page redirect me to all.aspx (with the post of course..)

    what can I do?

    thank you for the help & time.
    Shaked.

  110. feha

    Hi
    Thank you
    After studying a cookie plugin …
    I have modified the script to support cookies :-)

    Will be soon publishing a link.

  111. Aseem Gautam

    Hi,
    Thank you. A very nice solution indeed. I extended your solution to multi line with state management in ASP.NET. No cookies!!
    http://www.aseemgautam.com/blog/index.php/2009/10/27/jquery-multiline-tabs-with-state-management/

  112. Jon

    Brilliantly simple!

  113. flower

    Hey!
    How can i make it work from an external menu??

    i.e. I have a common menu with Home, Downloads, Contact, …etc.. and inside one of those common items there are submenus that take the user INSIDE a particular TAB, INSIDE the page where the jQuery Tabs are.
    Am i making sense?….

    THANKS!

  114. Greg

    Has anybody created code for having multiple tabbed areas on one page?

    Shouldn’t be too hard in theory– instead of applying the default action and onclick events to the selectors in the entire page, you’d look for them only inside specified divs.

    But that’s theory, and I’m not the best at execution. If anyone has already done this, I’d appreciate the code!
    Greg

  115. Soh

    Hey Greg, yea some mentioned that and I had a fix for them in the comments above~

    Open specified tab
    http://www.sohtanaka.com/web-design/examples/tabs/index2.htm

    multiple tabs on one page
    http://www.sohtanaka.com/web-design/examples/tabs/index3.htm

  116. Eric

    Hello. First, great tutorial. Simple and to the point without any unnecessary fluff. I’m running into a small problem configuring it for my sites current color scheme. I can’t seem to get the active tabs text color to change. I thought it would just be a matter of applying “color:…” to the “html ul.tabs li.active,…” definition since that’s where I changed the background color but it won’t stick. Basically, I just want to change the active tabs text color (when not hovered) to black. Any ideas?

    http://um3d.dc.umich.edu/Temp/newsite/tab_test.html

    Thanks

  117. SF

    Thanks very much for this tutorial and code. With some good modications (put the tabs on the side, modified div structure, etc) I am using it to display info/screen shots for my upcoming uISV product.

    It is working fine except for one small issue; if the browser is scrolled, it jumps to the top of the window

    Now the most bizarre part is; if I replace the .fadeIn() with a .show() then it no longer jumps; it works great … but it would be nice to have .fadeIn()

    Any suggestions? (off to the jquery site to check for bugs/info).

  118. Hazem Farra

    Very nice samples and tutorials….I am learning a lot from this blog/site. I am on the development side of the web (ASP.NET + little PHP) but in these bad economy times I am learning to to all!

  119. free style

    cool tabs!

Speak Your Mind…

Need to post HTML code?
Use Postable for your convenience.

Don't have an Avatar?
Set up a Gravatar image now!

Blog Blog Categories

Popular Comments Popular Posts

Recent Comments Recent Comments