Tutorials

Latest Word » Tutorials » Simple Tabs w/ CSS & jQuery
Simple Tabs w/ CSS & jQuery

Simple Tabs w/ CSS & jQuery

Tags: ,

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!

Related Posts

Author Bio

My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!

You can learn more about me or Twitter  Follow me on twitter for more updates and resources!

Did You Enjoy This Post?

subscribe  Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.

+ Add Comment294 Peeps Have Spoken Their Minds...

  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!

  120. Danny

    I was able to get it to fade by adding this code to add right before your tag.

    $(function() {
    $(“.tabs”).tabs(“.tab_container> div”, {effect: ‘fade’, fadeOutSpeed: 400});
    });

    It worked perfect for me, of course I made some modification to the css here is what I changes.

    .tab_container {
    position:relative;
    height:132px;
    width: 880px;
    }

    .tab_container div {
    display:none;
    position:absolute;
    font-size:14px;
    color:#444;
    width:881px;
    }

  121. FoO Iskandar

    Hello SOH!

    Thank’s for your tutorial and this project and so thank to this sample http://www.sohtanaka.com/web-design/examples/tabs/index3.htm it’s very helped :D

    Check out http://foo.iskandar.web.id it’s work with effect :D

  122. Gajus

    /**
    * Cookie + tabs
    */
    $(function(){
    $(“div.tabs div.frame”).hide();

    if($.cookie(‘active_bar’) == null)
    {
    $(“div.tabs div.frame:first”).show();
    $(“ul.tabs li:first”).addClass(“active”).show();
    }
    else
    {
    $($.cookie(‘active_bar’)).show();
    $(‘ul.tabs a[href=' + $.cookie('active_bar') + ']‘).parent().addClass(“active”).show();
    }

    $(“ul.tabs li”).click(function() {
    $(“ul.tabs li”).removeClass(“active”);
    $(this).addClass(“active”);
    $(“div.tabs div.frame”).hide();
    $($(this).find(“a”).attr(“href”)).fadeIn();
    $.cookie(‘active_bar’, $(this).find(“a”).attr(“href”), { path: ‘/’, expires: 10 });
    return false;
    });
    });

  123. Raja

    Thank you …very useful post also your blog design is great..

  124. Gabriel

    Sorry about my English, I have a problem loading google maps correctly inside the tab, and I try several solutions for jQuery UI Tabs but not work for this tabs, any solution?

  125. Asanka

    Nice & Simple. Thank you.

  126. iwans

    it’s Roooock! thannks..

  127. Rakesh

    Thank you so much.. Its flawless, Works charm!! Appreciate your valuable time and work. cheers!! ;)

  128. Web Designer Hyderabad

    Excellent post thanks. I recently started redesigning my site and was looking for
    tabs with CSS

    Here i found it, Thanks for sharing very useful.

  129. Lawrence

    Hi Soh!!!!

    Simple and clear jquery tabs! Thanks for this.

    However I have a question *(~.~)*

    Is it possible to add a code that will automatically and continuosly go to the next tab even without clicking on the tabs?

    How do I implement it?

    Many Thanks!

  130. Fred

    GM,

    How can we insert more than one on the same page, it seems only one works.

    Thank you

  131. E

    I have 2 tabs. My page is showing the second tabs content for a second during page load, then makes it invisible like it should be. How would I fix this?

  132. E

    Never mind, figured it out.

    It would be nice to be able to link to a specific tab with a url string.

    Nice work though, thanks.

  133. indialike.com

    This is really very very good Site… Thanks

  134. Gayantha

    wOw! these tutorials are Verrrryyy Useful.. Love them.. Thanx a Lott.. :)

  135. hman

    is there a way to have all links from tab contentes to open in the very same tab? thanks in advance and great tut keep it up

  136. Mike

    VEEEEEEEERY COOOOOOOOOOL!!!!!! I like it lots!!!

  137. يوتيوب اسلامي

    This is really very very good Site… Thanks

  138. Boyd Dames

    Very logical coding out there with JQuery! very nice! like: if the page has loaded, hide everything, then pick the first tab give it a class, show the first content page!

    Very logical!

  139. Rich

    One thing confuses me.. how do I set up jquery with wordpress? I cant get it to work!

  140. duplets

    Nice and simple. I like it!
    You can preview it on one of my sites: http://heal.lv/services/sms_acc
    I did a little css modification.

  141. Brian

    First of all thanks for the great articles/tutorials that you put out, they are very helpful. Now, what I’m trying to do is combine two of your articles into one solutions (“Active” State in CSS Navigations & This one), converting your horizontal tabs into a vertical menu system.

    You can see what I’ve got going on here: anytag.com/jQ-tabs.html

    Everything seems to be working great until you take a look at it in IE6… oh I hate IE;-) What is happening in IE is that the LAST css tag “Working” for the active state is being swapped for each of the navigation point instead of the active states for each of the different nav elements(please see the source).

    Does anyone have an idea of how I can fix this, I’ve been pulling my hair out trying to fix this bug. Thanks in advance for any help you can provide.

    Brian

  142. sunny

    anyway to fox pagination within tabs ?

    http://www.accessdunia.com.my/main/our-products

    once user clicks page 2 of pagination the other tabs become empty…

  143. Clarke

    Very good attention to the details. I see this as a stand-alone page with no other content. It will work great for me.

  144. AzeriFire

    But it still “jumps” on click tabs.I did it work: Just add fixed height to .tab_container class. For sample:

    .tab_container {
    border: 1px solid #999;
    border-top: none;
    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;
    height: 550px;
    }

  145. tim

    Great tutorial! Quick question – Once a tab is clicked, I’m trying to get the main content to change, along with some content in the sidebar (which is a separate div).

    i.e. – someone clicks tab1 and the tab1/tab_content is shown, at the same time, a different div in the sidebar would be shown at the same time……..I can’t find a tutorial or even a forum discussion on how to do this….if its too complicated for this thread, no worries…thought id ask though….thanks!

    tim.

  146. Bill

    Hi Soh,

    Fantastic Tabs!

    Have you investigated a way to get your tabs in a vertical layout?

  147. sajeer mohammed

    Hai dear…

    This is very useful tuto. thanks.
    I have one issue;ie; I want to create one more Jquery tab in the same page. If i take one more copy of existing tab, it does not work. I think one or more additional script to be used. can you please explain or give me a solution .

  148. ravi kumar tamada

    Very nice … i like this very much, easy to learn and implement.

  149. ravi kumar tamada

    It looks good but only problem is when we change the method show() to slideDown()- when we clicks quickly on another tab before the first tab appear the two contents are displaying in same tab, one after another . I will be happy if you reply me.
    Thank You

  150. P-L Gendreau

    You can wrap the .click function with
    if (!jQuery(this).hasClass(“active”)) {
    This way the active tab doesn’t fade out when you click and it’s already active :)

  151. nesho

    hi soh, great tutorial symple and easy but ;-) i have the same question like masey, bob, cracks … directly link to a specific tab in the same page from external websites. for example,
    product page = page1.php with 3 tabs, some external pages has a deeplink to
    http://www.xy.com/page1.php#tab1 <- (google adwords)
    http://www.xy.com/page1.php#tab2 <- (banner)
    http://www.xy.com/page1.php#tab3 <- (microsite)
    tutorial from fritzek unfortunately not working
    thanx a lott and for your time!

  152. P-L Gendreau

    What fritzek posted works, you just need to format your tab links like this: some tab

    Then, $(”ul.tabs li:has(a[rel="+target+"])”) gets the rel attribute and it just works :)

  153. P-L Gendreau

    That obviously didn’t work out as intended lol

    * Your link should look like: a href=”#tab5″ rel=”tab5″

  154. Saleem

    Hi this is a nice tutorial.. where easy to understand and better to implement.. its going to implement in my webite

  155. Ian Devlin

    Very useful tutorial and the code is well commented, so well done to you for that.

  156. nesho

    hi p-l gendreau (and fritzek)
    thanks for your tip -> link rel=”tab″, now it works !

  157. Colby

    Hi I am still having trouble getting a link from a different page to anchor to a certain tab on another page can someone please explain how I would do this? Thanks

  158. Alex Aitken

    Except for the transitional doctype, I love this. Will be using a variation in an upcoming project.

  159. Sanda

    Thank UUUUUUU soooo much….. I’m new web developing & since it’s a bit hard to attend classes I learn by going through the tutorials….

    :D

    Thanks to U guys I learn alot….. I still haven’t gone for a class to learn web developing…….

    U guys help alot out there…… Keep it up…… :)

    Gud Luck……

  160. Kuldeep Daftary

    how to create Vertical tabs ??

  161. pffmihai

    Great tutorial!

    I was searching for a nice jquery based solution for tabbed content that would also degrade nicely if the javascript is disabled in visitors’ browser.

    Thank you sooo much.

  162. codebloo

    To get this to auto rotate use the following code! (Note, I changed the name of the main tab listing to in my html and thus my code to keep the word TAB to a minimum :)

    var rotateDelay = 5000; //milliseconds
    var rotateTabs=true;
    var rotateTimeout = “”;

    function switchTabs(obj){
    $(“ul#swap li.active”).removeClass(“active”); //Remove any “active” class
    $(obj).addClass(“active”); //Add “active” class to selected tab
    $(“.tab_content”).hide(); //Hide all tab content

    var activeTab = $(obj).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;

    var nextTab = $(obj).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;
    }

    function doRotateTabs(){ //recursive function
    if (rotateTabs) {
    var rotateTimeout = setTimeout(function(){
    if (!rotateTabs){
    clearTimeout(rotateTimeout);
    return;
    } else {
    var currentTab = $(‘ul#swap li.active’);

    if(currentTab.length == 0 || currentTab.attr(‘id’) == $(‘ul#swap li:last’).attr(‘id’)){ //if there are no active tabs, or we’re on the last, show the first
    switchTabs($(‘ul#swap li:first’));
    } else {
    switchTabs($(‘ul#swap li.active’).next()); //pass the next element to show onto the function
    }

    doRotateTabs();
    }
    }, rotateDelay);
    }
    }

    $(document).ready(function() {

    //When page loads…
    $(“.tab_content”).hide(); //Hide all content
    $(“ul#swap li:first”).addClass(“active”).show(); //Activate first tab
    $(“.tab_content:first”).show(); //Show first tab content

    //start rotation
    doRotateTabs();

    //On Click Event
    $(“ul#swap li”).click(function() {
    switchTabs($(this));
    rotateTabs=false;
    return false;
    });

  163. Zach S

    Hi, great script. Is there anyway to add a close option on the links? So you would open them like you normally do but also have an option to close them after they’ve been opened. Any help would be appreciated. Thanks.

  164. Andy W

    Hi there.. firtly great piece of code and tutorial!!

    Is there a way to make the tabs automatically fill the width of the container? for instance if there were only two tabs they would stretch the entire length without leaving the gap at the end.

    many thanks
    Andy.

  165. Magora

    I’ve tried everything, still having the issue with the content “jumping” when loading the site. It doesn’t do it when switching tabs, just when you refresh or first load the site. It is a bit annoying. I understand the cause of the issue since I read every comment above. I just wish there was a way around it, but again I have tried all the above workarounds, nothing seems to fix the problem.

    Anyone have any more ideas toward fixing the whole jumping issue?

    Thanks, I love the tabs by the way, very simple to use.

  166. Magora

    Sorry, I should have added to the above ^ – that this issue seems to happen more in Firefox/IE. I’ve tested it in Chrome and Opera too, they don’t seem to generate the problem. Not sure about Safari.

  167. Soh

    @Magora, that is due to the lagg of your javascript loading. Basically the HTML is loading first (showing content) then js kicks in, hiding the content.

    If you want this to be resolved, you can just add a display: none to your content, and show it when js is loaded. Only down side is that the content will not be accessible if the user does not have js enabled.

    Hope that helps~

  168. Steve

    Really like the look of this tab widget. However, it doesn’t scale well when there are many tabs. I’ve tried to convert the CSS to support a vertically-tabbed version but can’t get the content to show appropriately to the right of the tabs. Any assistance would be much appreciated!

    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: 0px 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;
    border-left:none;
    clear: both;
    float: left;
    width: 100%;
    background: #fff;
    }
    .tab_content {
    padding: 10px;
    font-size: 1.2em;
    }

  169. Brad Ney

    Thanks for the code – Am going to implement this on my blog :)

  170. bruceh63

    Hello Soh

    I’m new to jquery so was very glad to find your tutorial. However, I’m having a problem getting the same results between browsers. I’m using a 3 state image as the tab background and then overlapping them. Using your code to switch the active state of the tab so that, hopefully, the active tab will be above the other tab or tabs (higher z-index). This code works great with IE 7 but when tested in any W3C compliant browser like Firefox 3.x or even IE 8 the effect goes away. (I can see class=”active” changing for the tabs using Firebug)

    I hope this is clear. Any ideas.

    Thanks
    Bruce

    Simple Tabs with CSS & jQuery

    /* get rid of those system borders being generated for A tags */
    a:active {outline:none;}
    :focus {-moz-outline-style:none;}
    /* a { position:relative }*/ /* Gets links displaying over a PNG background */
    a { outline:none; /* Gets rid of Firefox’s dotted borders */ text-decoration:none }
    a img { border:none } /* Gets rid of IE’s blue borders */

    body {
    background: #f0f0f0;
    margin: 0;
    padding: 0;
    font: 10px normal Verdana, Arial, Helvetica, sans-serif;
    color: #444;
    }

    /*– overlapping tab –*/

    /* root element for tabs */
    ul.tabs {
    list-style-type:none;
    margin:0 !important;
    padding:0;
    height:31px;
    }

    /* single tab */
    ul.tabs li {
    float:left;
    position:relative;

    }

    /* link inside the tab. uses a background image */
    ul.tabs li a {
    display: block;
    width: 132px;
    line-height: 31px;
    text-align: center;
    color: #000;
    background: transparent url(‘images/overlapping_tap_bg.png’)
    bottom left no-repeat;
    }

    ul.tabs li.active a{
    outline:none;
    color: #fff;
    background: transparent url(‘images/overlapping_tap_bg.png’)
    0 0 no-repeat;
    }

    /* when mouse enters the tab moves the background image */
    ul.tabs li a:hover {
    text-decoration: none;
    background: transparent url(‘images/overlapping_tap_bg.png’)
    0px -31px no-repeat;
    }

    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–*/

    ul.tabs li.active { z-index: 2; }

    #sponsor { z-index: 1; }
    #donation { left: -12px; z-index: 0; }

    /*– content –*/
    .container {
    width: 265px;
    margin: 10px auto;
    }

    .tab_container {
    border: 1px solid #999;
    border-top: none;
    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;
    }
    .tab_content {
    padding:20px;
    font-size: 1.2em;
    color:white;
    background-color:#1F1A17;
    }

    $(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”).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;
    });
    });

    Our Sponsor’s
    Our Donor’s

    Tab 1 content goes here

    Tab 2 content goes here



  171. KennethB

    You mentioned, as a reply to Tom (http://www.sohtanaka.com/web-design/simple-tabs-w-css-jquery/#comment-3412) that you might make a carousel version, with “next” and “previous”. I have searched the web for a solution to integrate with your code, but I can’t make it work… Can you help us?? :-)

    Very slick code! I am a frequent, happy, returning visitor!

  172. bruceh63

    Opps – looks like the html for the above did not appear correctly – here it is
    sorry
    Bruce

    Our Sponsor’s
    Our Donor’s

    Tab 1 content goes here

    Tab 2 content goes here



  173. ocean

    thx soh

    ocean

  174. Yves

    thanks Soh this is great.

    Unfortunately, despite the return false, since my tab content area is below the fold, the page is still jumping up to the anchor on every click.

    Any ideas?

  175. Steve

    I am looking for concrete example(s) of this widget implemented for vertical tabs where each tab container doesn’t have to have a fixed width. This is important so each tab’s content can support items that are float-right, tables with width=”100%”, etc. (the example linked previously is fixed-width and I can’t get these two scenarios to render properly.

  176. Yves

    I should add that as was mentioned above, this stops being a problem when changing fadeIn() to show(). Strange?

  177. dan

    i know that you can have the tabs multiple of times on one page but what if you wanted one main page tab(s) then inside one of the tabs u had another row of tabs different style (smaller) is that possible ???
    basically duplication the css and renaming it for the different style…
    but that does not work ! ha

    any one have an idea ??

  178. dan

    D’oh! i think its time to west.. iv figured it out!
    great tutorial by the way !

  179. Drew

    Any thoughts on how to get multiple copies of this working on a single page? It seems to be limited to one; unless I’m just doing it wrong!

  180. ante

    ca i downloa tha demo at all ?
    so its make life easer!

  181. P-L Gendreau

    @ante – There’s a demo you can down copy and paste, but the whole idea behind the tutorial is actually to learn!

    http://www.sohtanaka.com/web-design/do-you-eat-and-run/

  182. droope

    Hi there, dear blog owner :)

    I have made a fix for your code, that enables it to work when, for some reason, one can’t make a link to #tab2, and needs to make a link such as currentpage.php#tab2.

    Believe me, that need exists in many CMS, and several situations.

    Anyway, here it is:

    <script>
    $(document).ready(function() {

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

    //On Click Event
    $(&quot;ul.tabs li&quot;).click(function() {
    $(&quot;ul.tabs li&quot;).removeClass(&quot;active&quot;); //Remove any &quot;active&quot; class
    $(this).addClass(&quot;active&quot;); //Add &quot;active&quot; class to selected tab
    $(&quot;.tab_content&quot;).hide(); //Hide all tab content

    var activeTab = $(this).find(&quot;a&quot;).attr(&quot;href&quot;); //Find the href attribute value to identify the active tab content
    activeTab = activeTab.split(&quot;#&quot;); //bugfixed by droope :)
    $(&quot;#&quot; activeTab[activeTab.length-1]).fadeIn(); //Fade in the active ID content
    return false;
    });

    });
    </script>

    Thank you for your script!

    It was very useful for me,

    also, your site is very beautiful.

    Cheers,
    Droope

  183. droope

    Unescaped code:
    $(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 = activeTab.split(“#”); //bugfixed by droope :)
    $(“#”+activeTab[activeTab.length-1]).fadeIn(); //Fade in the active ID content
    return false;
    });

    });

  184. Imtiaz

    Hiya, am new in jquery and i wanna know howw can i add more than one tabbed box in same page. Will be glad if you let me know, btw i already used one from here and it was cooool!!!

  185. Alawi

    How i can show any tab as default.

    so for example i want to show second tab or third tab.

    And i want way to make any tab i want disable.

  186. Richie Sun

    So simple and so nice!!!!!!!!

  187. tulio

    How to link to tabs via an a text link on the same page?

    I’ve tried the codes posted here (and pretty much everywhere…)
    without sucess. It will only link to a tab if it’s already open, it doesn’t switch between tabs.

    Any clues?

  188. Victor

    I tried the code, it works perfect for me. Thank you for sharing, Soh!

    A tiny issue commented above is “back to top”. If you use “fadein” in jquery, it will reset the page back to the top, no matter you use “return false” or not. If you use “show()” instead, the problem is gone.

  189. Himel Khan

    awesome…. i like it.
    more jquery contents visit:
    http://www.coolajax.net/category/listing/jquery.html

  190. Nabin Bikram Singh

    Simple and nice post. I found a lot of results while I google for jQuery tab but this one was the most simple one and perfect to my requirement.
    Thanks a lot.

  191. Mick

    Is it at all possible to add the ability to open the tab from an external link rather than the default tab?

    For example if I wanted to link someone to tab3 I’d give them the URL index.html#tab3

  192. Mukul

    Thanks for the code its simple and easy to implement.

  193. Mike

    First of all, great tutorial and great looking site!

    Secondly, to all of those having issues with the page jumping when a tab is clicked: the “return: false;” didn’t work for me either, so I simply changed the tab li’s from…

    Gallery

    …to…

    Gallery

    Then, in the javascript…

    var activeTab = $(this).find(“a”).attr(“href”);

    …becomes…

    var activeTab = $(this).find(“a”).attr(“name”);

    It works for me in FF and Safari, not sure if it’s good for IE but I don’t see why not.

  194. Mike

    Damn, of course my HTML didn’t post…

    TO THE MODERATOR: since I can’t edit my post, please change the “Gallery” links to display the HTML instead, then delete this message please :)

    Thanks.

  195. Nick

    Thanks for the code!

    I noticed one issue I was having.

    When my page loaded in IE 7 my content was displaying spaces between the three divs, but then when you clicked on the tab the spaces were removed.

    I couldn’t have the spaces and in order to resolve the issues I added to the code;

    $(“.tab_content:first”).show(“active”); //Show first tab content

    Maybe you have a better way of resolving this issue when the page loads?

    Thank you for your great work.

  196. Nick

    I also added this to help with displaying Google calendar in a tab.

    Hope it helps.

    $(this).addClass(“active”).show();

  197. Webopolis

    Please do a vertical version of this. Or, show me a resource with an example.

    Thanks…

  198. Lindsay

    Completely, totally, and utterly psyched. THANK YOU SO MUCH!!

    I used it here:
    http://96.9.157.245/~rrochlin/our-practice-areas/
    I added the ability to make tags also a selector for the hidden divs. LOVE IT THANK YOU!

  199. Lindsay

    Got one little question.. its been asked in these comments but hasn’t really been addressed. Can you make a tab open from an external link. So for instance, I have my default tab that is always open so long as people come to the page from the basic links throughout my site or the web.

    HOWEVER, if i want people to go directly to the content of one particular tab instead telling them to “click here, then click on the XXX tab” how do I make that happen?

    site can be seen here with your stuff
    http://96.9.157.245/~rrochlin/our-practice-areas/

    So i’d like to make the “Corporate Control Disputes” tab to be open if I create a link where I want people to see that content on click

  200. Nathan

    Nice one, easy, simple and does a great job!
    Thanks for this tut.

  201. ali

    Hi All
    i want right to left this tabs
    How?

  202. Nick

    Lindsay, sorry I can’t help with your question, but I’ve got a question for you…

    As many people here have mentioned, there is a problem with the page jumping back up to the top when a tab is clicked. One person has replied that it can be fixed by removing the fadein function. However, you seem to have managed to make it work on your site. Could you enlighten us on how you fixed the problem?

  203. Daniel Hunninghake

    I have to say, I am so impressed with the simplicity of this jQuery technique! Also this site is amazing… a great place to get lost in and learn fresh coding.

  204. Dave

    Love the tabs, nice and simple. I wanted to add tab shortcuts so I included the shortcut JS library here:
    http://www.openjs.com/scripts/events/keyboard_shortcuts/

    and then added two shortcuts for moving between tabs using CTRL+LeftArrow and CTRL+RightArrow like this (in the document.ready() function):

    /**
    * Contrl+Right Arrow = move to next tab.
    */
    shortcut.add (“Ctrl+right”, function () {
    $(“ul.tabs li.active”).next ().find (“a”).click ();
    });

    /**
    * Contrl+Left Arrow = move to previous tab.
    */
    shortcut.add (“Ctrl+left”, function () {
    $(“ul.tabs li.active”).prev ().find (“a”).click ();
    });

  205. rory

    Awesome. Tabs work great. I have integrated them with .NET. I have a form on each tab, and I have got it working to keep the same tab on PostBack. If anyone wants the code just email me.

    rory584@hotmail.com

  206. Matthew

    Great tuts Soh!,
    let’s see. i want to this tabs add an Autoplay and switch for 4 o 5 seconds to the next tab… do u know how to change that?

    thx soh.

  207. Tg

    Great Tutorial :) How can i disable or enable a tab in you js file? Haven’t found solution yet.

    Thanks.

  208. dnnsldr

    @droope thanks for the code. I needed this since I was bringing in content via ajax. It worked in Firefox and IE8 the original way, but IE7 would not show content until I implemented your code. Thanks,

  209. Phil H

    Thanks for a great program. Just one point, I have a google map on one tab and have set the container width to 80% of the screen … Only one map tile gets shown but all the pointers. Is there a way to get all the map tiles to show correctly

    Thanks again
    Phil

  210. Karl

    OK! Amazing, but if i want to call certain div from another page, lets say that i want the tab3 to show first, how do i link that element?

  211. milo

    what if you have a php page in another tab with pagination how would you reference it without the tab jumping? since you cannot reference it with #tab2 etc.

    btw thanks nice tutorial

  212. Colin

    This is a fantastic, but the biggest outstanding query is how to get the page to load to a specified tag. Quite a few ask for it, there are a couple of suggested solutions which don’t seem to work. Fix this and you’ve get a perfect solution for good pages. Can it be done – I’ve just spent three days trying to figure it out. The key seems to be to identify the bookmark reference (#tabx) and so activate that tab. I can get the page to load to the correct content, but not activate the correct tab.
    Solution so far:

    //Default Action
    $(“.tab_content”).hide(); //Hide all content
    if (location.hash !=”")
    {
    $(location.hash).show(); //Show bookmarked tab content – THIS BIT WORKS
    var activeTab = location.hash
    $(activeTab).addclass(“active”).show(); //Activate tab – THIS BIT DOESN’T
    }
    else
    {
    $(“.tab_content:first”).show(); //Show first tab content
    $(“ul.tabs li:first”).addClass(“active”).show(); //Activate first tab
    }

  213. Nepal Kathmandu

    You are absolutely genius. Never seen such wonderful tutorials for free. Thank you.
    God bless you. You are the best web design guru.

  214. Joe

    Does anyone know how to keep the same tab to shwo after a postback?

    Thanks Joe

  215. Leslie

    Hi,
    I was wondering how to make the color of the active tab’s font change color. Right now only the background’s color is changed. Please help. thanks

  216. Shelliez

    I’m using this and it’s great, but I only have three tabs and I want to change the tab width so that they fit all the way across in width. Having a lot of trouble doing so. How can I change the tab width??

  217. Andrew

    Hi, thanks for the script, it’s really useful.

    Is it possible to add a link elsewhere in the page that closes all tabs again?

    Thanks

  218. Hank

    Has anyone noticed the first tab no longer shows in Chrome? Please help! Works great in FF and IE, and did in Chrome, but something’s changed!

  219. Your REAL Name...

    I loved this tutorial, I am only just learning javascript. I am not sure If I did something wrong, but when I look at this in IE, all of the tabs are visible. Not sure how to fix this, any suggestions would be great. Thanks!

  220. yow

    nice tutorial, thanks

  221. James

    Terrific tutorial! Just wondering how to link to a specific tab?

  222. Airos

    Thanks for your share this Tech, i’m ing use

  223. Welly Chow

    COOOOOL…. THX MAN

  224. dukiejc

    Hi,
    Great tutorial and it works wonderfully!
    I know this has been asked before but didn’t see an answer. Would anyone know how I can add a link within each content tab to go to the next tab? (sort of like a prev and next link to navigate through the tabs)

    Thanks for any help.

  225. Melissa Slemin

    Hi ya–regarding your answer to Masey’s question from July 09, could you elaborate a little? I need to know exactly how to achieve this.
    Thanks.

  226. Victor

    Thanks creating these tabs. Unfortunately, I have it setup on a page but it didn’t work. So I went ahead and set it up on it’s own page, just to make sure no other script was interfering but it still work. :(

    I’m installing your script exactly as you have it on your tutorial. I mean, exactly! But it still doesn’t work. When it say it doesn’t work, I mean you can view the fake content I entered for Graphic and Submit without having to click on either tab rather than hiding the content for the inactive tab. Also when I click on the tabs it jumps to the div as an “anchor”.

    I read the comments and it seems to work for everyone else :(. Any ideas??

  227. Drive Net Consulting

    This is the best jQuery tabs plugin I have found to date. The major thing that drew me in to this is that it is sooooo lightweight. Thanks for the great work!

  228. datshay

    very helpful
    ……. Thanks

  229. Jerome

    First thanks for the tutorial, great work!
    And here the fix for the tab to mark it active:

    $(document).ready(function() {

    $(".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[href=" 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 href attribute value to identify the active tab content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
    });

  230. Kim

    Hello!

    Please excuse me if this seems like a silly question. Everything seems to be showing up properly, however, I can’t seem to activate the jquery. Where do I put the following code in my html file? Thank you in advance for your help!

    $(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;
    });

    });

  231. Kim

    Actually, I got it to work. Thank you for this great demo! :)

  232. cesar

    thanks for the script,
    i need to use it in a very particular way, need to put many tab boxes inside the same page, you can see here http://www.productosdecolombia.com/new/artesanias.asp , but the script only works for the first one, the rest of the tab boxes got hidden, is there a way i can make it work?

  233. Christian

    good one, I used it for my recent project

  234. Colin

    OK, now we’re getting somewhere. The tabs work, and with Jeromes modification (with the typos corrected – see next post), you can go to a specified tab on a DIFFERENT page. Brilliant!

    Final point of detail, how do you specify a different tab ON THE SAME PAGE but from the body text of the page. Currently I can’t get this to work as the browswer recognises its already on the page so won’t reload and seems to ignore the bookmark (#tabx).

    Any ideas, anyone?

  235. colin

    Heres the corrected code from Jeromes post (and the variation to allow fade for non IE browsers):

    $(document).ready(function() {
    $(“.div_reveal”).hide(); //Hide all content
    $(“.tab_content”).hide(); //Hide all content
    if(location.hash != “”) {
    var target = “#”+location.hash.split(“#”)[1]; // need semicolon at end of line
    $(location.hash).show(); //Show first tab content
    $(“ul.tabs li:has(a[href="+target+"])”).addClass(“active”).show(); //need ‘+’ either side of ‘target’
    } 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 href attribute value to identify the active tab content
    if ($.browser.msie)
    {$(activeTab).show();}
    else
    {$(activeTab).fadeIn();}
    return false;
    });
    }); // need this to close script

  236. Moderns

    Thanks a lot! Do you know how to activate the second tab at the page load? I added word “last” instead of “first” and was able activate last tab. Please advise. Thanks.

  237. Jerome

    Ok, I will go thru again and test in all Browser (just tested in Safari before). Will post the fix soon.

  238. Nick H

    @all

    I needed to load tab3 when the page loaded so…

    $(“ul.tabs li:first”).addClass(“active”).show(); //Activate first tab
    $(“.tab_content:first”).show(); //Show first tab content

    changed to:

    $(“ul.tabs li:#tab3″).addClass(“active”).show(); //Activate first tab
    $(“.tab_content:#tab3″).show(); //Show first tab content

    This gives me the correct tab when the page loads, but there are visual issues with the tab and the tab-content.

    Any ideas on how to fix this?

  239. Kim

    hi, how to change click(function) to mouseover and show content..

  240. Orijit

    Thanks Nick. I had a similar problem of loading a particular tab depending on the page. So I made a small change.

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

    to

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

    You can specify the index number as the eq param to change the tab.

  241. colin

    Orijit and Kim,
    The solution to loading ANY tab is contained within the code published by Jerome and updated by me in my post of 05/31. To load the 2nd tab by default simply use pagename.ext#tab2 for the second, pagename.ext#tab5 for the fifth and so on in the hyperlink. If there is no bookmark (#), it will simply load the first tab. For example a hyperlink like this: thispage.asp#tab3 loads the third tab, whereas thispage.asp loads the first tab.
    If you want to specify a default for a page you can use the code that Nick provides but not if you want to use reusable code within master pages or templates.

  242. Nick H

    @ ORIJIT

    ORIJIT that is a way better way of doing it thanks!

    I still have one issue when loading and other tab then the first:

    @ ORIJIT Your code
    $(“ul.tabs li:eq(2)”).addClass(“active”).show(); //Activate first tab
    $(“.tab_content:eq(2)”).show(); //Show first tab content

    When using this way the first tab’s content now isn’t being completely loaded and is messing up.

    Any ideas for a fix?

    @ COLIN

    Your way allows me to link to the page from an external page and only that.
    If I have loaded the page from that link it will not allow me to change it again unless the url actually changes and not the tab. Maybe I‘m doing something wrong and you need to enlighten me?

    What I need is to be able to set the default tab to any tab that I specify when index.html is requested and make sure that all the content is loaded from all the tabs even when #tab3 is default.

    I hope you understand all this. :)

  243. Nick H

    @ all

    Is there a way to add an auto rotate to the script so when the page loads it rotates to the directed tab?

    This would allow me to load the first tab and get all the content loaded and then switch to the third tab.

  244. Nick H

    @ codebloo +1

    Thanks to your added rotate function I was able to get it to work!

    fixed my first tab not being loaded and provided me an auto switch that acts as i chose the default tab on load.

    /***********************************************
    * Description : Activate the Tabs – jQuery
    * Author: Soh Tanaka
    * URL: http://www.sohtanaka.com/
    ***********************************************/
    var rotateDelay = 1000; //milliseconds
    var rotateTabs= true;
    var rotateTimeout = “”;

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

    var activeTab = $(obj).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;

    var nextTab = $(obj).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;
    }

    function doRotateTabs(){ //recursive function
    if (rotateTabs) {

    var rotateTimeout = setTimeout(function(){

    if (!rotateTabs){
    clearTimeout(rotateTimeout);
    return;

    } else {

    var currentTab = $(“ul.tabs li.active”);

    if(currentTab.length == 0 || currentTab.attr(‘id’) == $(‘ul.tabs li:last’).attr(‘id’)){ //if there are no active tabs
    switchTabs($(“ul.tabs li:eq(2)”));
    rotateTabs=false;
    } else {
    switchTabs($(“ul.tabs li:eq(2)”)); //if there are active tabs
    rotateTabs=false;
    } doRotateTabs();}
    }, rotateDelay);
    }
    }

    $(document).ready(function() {

    //When page loads…
    $(“.tab_content”).hide(); //Hide all content

    //start rotation
    doRotateTabs();

    if(location.hash != “”) {
    var target = “#”+location.hash.split(“#”)[1]; // need semicolon at end of line
    $(location.hash).show(); //Show first tab content
    $(“ul.tabs li:has(a[href="+target+"])”).addClass(“active”).show();
    } else {
    $(“ul.tabs li:eq(0)”).addClass(“active”).show(); //Activate first tab
    $(“.tab_content:eq(0)”).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”).show(); //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
    if ($.browser.msie)
    {$(activeTab).show();}
    else
    {$(activeTab).fadeIn();}
    return false;
    });

    });

  245. Nick H

    Here is a slight change to the “//When page loads…” section for my previouse post to keep external linking intact. So if the page is called with a #tab2 link it will handle it and not run the auto switch function.

    $(document).ready(function() {

    //When page loads…
    $(“.tab_content”).hide(); //Hide all content

    if(location.hash != “”) {
    var target = “#”+location.hash.split(“#”)[1]; // need semicolon at end of line
    $(location.hash).show(); //Show first tab content
    $(“ul.tabs li:has(a[href="+target+"])”).addClass(“active”).show();
    rotateTabs=false;
    } else {
    $(“ul.tabs li:eq(0)”).addClass(“active”).show(); //Activate first tab
    $(“.tab_content:eq(0)”).show(); //Show first tab content
    }

    //start rotation
    doRotateTabs();

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

    $(“ul.tabs li”).removeClass(“active”); //Remove any “active” class
    $(this).addClass(“active”).show(); //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
    if ($.browser.msie)
    {$(activeTab).show();}
    else
    {$(activeTab).fadeIn();}
    return false;
    });

    });

    Hope this helps someone out there!
    cheers! :)

  246. Orijit

    @ Colin
    I tried to set the tahb using url re-writing and specifying the tab but it was not working. Strange but true.

    @ Nick
    I have around 8 tabs mate and all of them work fine. No loading issues here. I will still try and see if something could be done about it.

  247. Duncan Smith

    This CSS/jQuery tab technique will work fine if you have static content, however if, like me, you have a Rails app that generates dynamic content and therefore has a “yield” call that needs to go into the tab_content container, you have a problem.

  248. Kiran

    :) Many thanks for a GOOD DEMO.

  249. Armand

    the auto rotation as specified above is not working for me

  250. Tony

    @ Nick H
    I am trying to implement your suggested code for the rotate function, but didn’t have any success. I am a novice at CSS and a Muppet at Javascript and would really appreciate some pointers. I got the tabs working somehow using my own customized CSS, which looks like this(sry for long post):
    .container {
    width: 755px;
    margin: auto;
    background-color:#ffffff;
    }
    ul.tabs {
    margin: 0;
    padding: 0;
    float: left;
    list-style: none;
    height: 25px;
    font-size:11px;
    background-color:#ffffff;
    width: 100%;
    }
    ul.tabs li {
    float: left;
    height: 25px;
    width:20%;
    text-align:left;
    font-size:11px;
    }
    ul.tabs a {

    }
    ul.tabs a span {
    padding:7px 7px 0 4px;
    height: 24px;
    float:left;
    text-align: center;
    width: 140px;
    display:block;
    background: url(../images/nav-all.png);
    background-repeat: repeat-x;
    background-position:100% -95px;
    color:#fff;
    }
    ul.tabs a:hover {
    background-position:100% -155px;
    }
    ul.tabs a:hover span {
    background-position:100% -145px;
    }
    ul.tabs li a:hover {
    width:140px;
    height:25px;
    text-align: center;
    background: #000;
    }
    html ul.tabs .active a span {
    height:24px;
    color: #7AA9DD;
    background: url(../images/nav-all.png);
    background-repeat: repeat-x;
    background-position:100% -145px;
    }
    .tab_container {
    border-top: none;
    float: left;
    font-size:12px;
    color:#999;
    height: 150px;
    text-align:left;
    font-family:Arial, Helvetica, sans-serif;
    width: 755px;
    margin-bottom: 10px;

    }
    .tab_content {
    display: none;
    }
    .banner_left
    {
    width:500px;
    text-align:left;
    margin: 0 10px;
    font-size:12px;
    background-color:#ffffff;
    color:#000;
    float:left;
    }
    .banner_leftright
    {
    width:200px;
    padding-top:2px;
    float:right;
    text-align: right;
    }

    I know it ain’t pretty, but it works for me, but when i replace the initial JS code from Soh with yours, the .container goes blank and the tabs loose functionality.
    I suspect i need to change some classes in the JS, but i am totally lost. Please help a newbie. Cheers!

  251. José

    Nice Tutorial!
    Is it possible to build nested tabs?

  252. iodyssee

    I have been trying to submit contents of a search form in one of the tabs and I would like the result to show onto the same tab. I am in dire need of your help on this. It’s been so frustrating.
    many thanks.
    ~O

  253. Shane

    I’ve implemented the code as it is shown at the top of the page but I am running into an issue that I don’t see commented here. The only way I can get my tabs to work is if I click on the far-right of the tab. If I click on the tab itself the color changes but nothing happens and the tab content doesn’t change. I am new to JavaScript / jQuery so I don’t know if it’s something I did or if another bit of code is affecting it. Please help.

  254. Shane

    Update: Ok I changed a few things and I still don’t think it is working as intended. It seems that clicking on the words IE:”Gallery” do nothing, but clicking on the tab itself is what changes the content. Still tinkering though.

  255. mia

    thank you for this tutorial.
    the tabs do not work in cold fusion based cms but work in a non-cms site.
    Not working here:
    http://staging.asta.rd.net/about/content.cfm?ItemNumber=3645

    working here:
    http://www.travelsense.org/misrak/joinPage/joinPage2.html

    please help!

  256. dan

    great tabs!

    but i have a slight problem! i want to be able to link to a tab i see that colin (5/31) posted the script to allow this ?
    now i must be doing something wrong here i pasted his code in my doc BUT it brakes my tabs! they worked with the original script just not collins js!

    what am i doing wrong any one need help ASAP ?????

  257. James

    Same as Dan – links now correctly load the tab, but I’m seeing all my tabs in one long row now – it’s not hiding the non active tabs. Any ideas?

  258. James

    Sorted. The “s were being reformatted when I copied and pasted.

    Hopefully this may help others.

  259. Mark

    Hey Guys,

    I would like to add the fade effect that is used in this tutorial but i don’t know how to implement it into my code. Copy and pasting it into my javascript doesn’t work.

    Also i would like to hide all the tab content upon the start of the page. thats seems to be presented here but again i don’t know how to implement it.

    My website is MikeandMarkWebsites.com
    Please view the source to see the code.

    I would appreciate the quick help. Thanks

  260. Matti Ressler

    Using only the hash value in the tab link can cause problems, depending on the URL structure of the website/browser. It is better for the link to contain the full relative path and thus the javascript would need to be adjusted:

    var activeTabLink = $(this).find(“a”).attr(“href”);
    var activeTab = activeTabLink.substr(activeTabLink.indexOf(“#”));

  261. Mark Fasel

    Great tutorial, very easy to implement and simple to understand!

  262. Vladimir Dzhuvinov

    To prevent unnecessary reloading of current tab if clicked again just do a class== “active”? check in the click hander:

    $(document).ready(function() {

    $(“.tab_content”).hide(); //Hide all content
    $(“ul.tabs li:first”).addClass(“active”).show();
    $(“.tab_content:first”).show();

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

    if ($(this).hasClass(“active”))
    return false; // Tab already selected

    $(“ul.tabs li”).removeClass(“active”);
    $(this).addClass(“active”);
    $(“.tab_content”).hide();

    var activeTab = $(this).find(“a”).attr(“href”);
    $(activeTab).fadeIn();
    return false;
    });

    });

  263. Rory

    Having trouble with multiple use. only one is working at a time.

  264. Adrian

    I’ve only just come across your site and your blog seems like a great place for tutorials. I’ve been looking for some nice, simple tabs to use on an upcoming project and these look great. I also like you post on ‘Greyscale Hover Effect’

    Thanks for sharing!

    I think I’ll be returning here :)

  265. srikanth gupta

    Excellent Article…Thanks for the posting

  266. sath

    Very simple and detailly explained…very nice sharing..Thanku

  267. raghavendra

    This is quite a good article..
    Help needed on tabs…
    Hi! iwas working on a website that would offer tabs just like a browser… that is if i add facebook in the frame1 of “tab1″.. and user logs on it.. and.. after that user goes to “tab2″ and is busy on it for a while….. but when he clicks on “tab1″ again.. he must be able to go to his signedin page instead of the home page of facebook.. how do i do that? please help me on this one…

    send in your answer to
    raghavendrabsrg@gmail.com

    thank you,
    RAGHAVENDRA

  268. Ashish Shah

    Hi I am Ashish Shah. You are doing excellent job. your code for creating simpl tab page is i implemented and work successfully. But I would face little problem for setting dynamic tab selection when i implemented paging in it. I m redirect page to successfully but the tab which on this paging is appear is not selected. If u have some solution for paging with tab page please give me or contact me by mail:ashushah48@gmail.com
    Thank You

  269. Brian

    Hi, I really like this demo. I have been adding more tabs and you can do that on the fly, but the main question right nowe for me is the following: How do i enable scroolbars within this content tab at about 14 lines of text.

    Thanks

  270. kiran

    Thank You once again for not deleting this demo. Actually I was doing wrong.
    Thank You :)

  271. Shahab Uddin

    Hey its not working :s
    my code is

    $(document).ready(function() {

    //When page loads…
    $(“.descpanel”).hide(); //Hide all content
    $(“.cpqmenu ul li a:first”).addClass(“active”).show(); //Activate first tab
    $(“.descpanel:first”).show(); //Show first tab content

    //On Click Event
    $(“.cpqmenu ul li a”).click(function() {

    $(“.cpqmenu ul li a”).removeClass(“active”); //Remove any “active” class
    $(this).addClass(“active”); //Add “active” class to selected tab
    $(“.descpanel”).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;
    });

    });

    HOME
    ABOUT US
    SERVICES
    CONTACT US

    asdasdasd

    asdasdasd

    it just shows the first content only once and then when i click other links the active class works fine and content is gone :s nothing is showed. help me please

  272. dave

    i have got this working fine but everytime i click a tab it forces the page back to top again. anyone know a way around this.

  273. Mike Creuzer

    Brilliant work!

    I was wondering. I am trying to do a link inside of the content to advance to the next tab.

    $(“a.button_main”).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;
    });

    with

    Continue Reading

    as my html.

    Do you think you could point me in the right direction to get this done? I know I have to use a different link so I can’t use {this} to change my class or show the content. Any ideas?

    Thank you for this,
    Mike Creuzer

  274. Matt

    the problem i’m having is using this as a div swapper. i am using it as once a thumbnail is clicked it slides out the div and slides in the new div. it works fine when you click the same thumbnail, but when clicking a different thumbnail it only slides the new div without transitioning the old div first. i can’t figure out why. any thoughts?

    //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”);
    $(this).addClass(“active”);
    $(“.tab_content”).slideUp(1000);

    var activeTab = $(this).find(“a”).attr(“href”);
    $(activeTab).slideDown(1000);
    return false;
    });

  275. Fernando

    Hei, I think that someone have copied your work and don’t gave your credits . See: http://www.tuliofaria.net/jquery-tabs-abas-simples-e-leve/ (in portuguese)

  276. Owen

    will search engines look at this as one page of content or multipul pages. I would prefer one page.

    Any help would be great

  277. King

    Your Website is Awesome!

  278. Anoop D

    Great tutorials and thanks a lot .. it was very useful since i was finding it difficult with UI .. thanks a lot .. and hope you will continue this great effort

  279. todd

    This should solve the jumping issue mentioned:
    onclick=”return false;”

    Drop the above into your tag and you should be good to go.

  280. Phil

    At last, a simple tutorial on this. Thank you very much!

  281. Steve

    That’s a great tutorial, thanks!

    One question – I’d like to get it to cycle through the tabs – is it easy to do that?

    I’m thinking of using it as a tall list of products in a column beside the main content of the page. The user would see the products on the first tab, but most probably wouldn’t think to click to a new tab so it’d be good if I could get it to automatically swap after, say, 30 seconds. That way they would then see a second tab of products that they may be interested in.

    I appreciate that you’d not want to add it to the main tutorial as it adds complications to a simple, clear tutorial – but I wondered if you had any pointers on how I could amend it?

  282. Steve

    Ooops – should’ve read through all the comments first as it looks like the idea of cycling through all the tabs has been discussed already! I’ll give the code there a try!

  283. Stefan

    Hey,
    very very nice and simple code. Thank you :)
    But I have one issue, my Firebug Debugger say:

    Warnung: Unknown Pseudoclass or Pseudoelement ‘first’.
    Sourcefile: http://localhost/mfv1/#infos
    Line: 0

    How can I fix it?

    Thank you, Stefan

  284. Zenx

    This is how to make it suitable for several different tab blocks in the same page:

    $(“.tab_content”).hide();

    $(“.tab_container .tab_content:first-child”).show();
    $(“ul.tabs li:first-child”).addClass(“active”).show();

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

    $(this).parent().children(“li”).removeClass(“active”);
    $(this).addClass(“active”);
    $(this).parent().next(“.tab_container”).children(“.tab_content”).hide();

    var activeTab = $(this).find(“a”).attr(“href”);
    $(this).parent().next(“.tab_container”).children(activeTab).fadeIn();
    return false;
    });

  285. Nick H

    @Tony

    This is the full code for the rotator function that I posted above:

    This will start you Active tab on tab#3. If you need to change the active tab on page load adjust the code accordingly.

    /***********************************************
    * Description : Activate the Tabs – jQuery
    ***********************************************/

    var rotateDelay = 1000; //milliseconds
    var rotateTabs= true;
    var rotateTimeout = “”;

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

    var activeTab = $(obj).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;

    var nextTab = $(obj).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;
    }

    function doRotateTabs(){ //recursive function
    if (rotateTabs) {

    var rotateTimeout = setTimeout(function(){

    if (!rotateTabs){
    clearTimeout(rotateTimeout);
    return;

    } else {

    var currentTab = $(“ul.tabs li.active”);

    if(currentTab.length == 0 || currentTab.attr(‘id’) == $(‘ul.tabs li:last’).attr(‘id’)){ //if there are no active tabs
    switchTabs($(“ul.tabs li:eq(2)”));
    rotateTabs=false;
    } else {
    switchTabs($(“ul.tabs li:eq(2)”)); //if there are active tabs
    rotateTabs=false;
    } doRotateTabs();}
    }, rotateDelay);
    }
    }

    $(document).ready(function() {

    //When page loads…
    $(“.tab_content”).hide(); //Hide all content

    if(location.hash != “”) {
    var target = “#”+location.hash.split(“#”)[1]; // need semicolon at end of line
    $(location.hash).show(); //Show first tab content
    $(“ul.tabs li:has(a[href="+target+"])”).addClass(“active”).show();
    rotateTabs=false;
    } else {
    $(“ul.tabs li:eq(0)”).addClass(“active”).show(); //Activate first tab
    $(“.tab_content:eq(0)”).show(); //Show first tab content
    }

    //start rotation
    doRotateTabs();

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

    $(“ul.tabs li”).removeClass(“active”); //Remove any “active” class
    $(this).addClass(“active”).show(); //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
    if ($.browser.msie)
    {$(activeTab).show();}
    else
    {$(activeTab).fadeIn();}
    return false;
    });
    });

  286. Johan

    very good code helped me a lot.

    there is some way that does not always load tab1

    I want to load the tab3 Tab2 or by calling the page from a link

    thank you very much for the help

  287. David

    There is an issue in the code (first element)! Look at your Debugger.
    How can I fix it?
    Greets David

  288. Charly

    Nice script – im using this
    Is there any way i can disable a few of the tabs, one or two tabs?, tried removing the a link and its still clickable!

    thanks

  289. Matt

    Hi! Fantastic script – really easy to use.

    Quick question – i have a table that i want to insert into one of the tabs however it keeps pushing it under the tabs. Any ideas?

    Thanks!

  290. jChannel

    Again Soh, thanks for being an outlet for awesome resources & inspiration!

    I am using this in a similar manner, but without the “tabs” look. My problem I have found when using this with Cufon for the “tabs”, it doesn’t work unless you put Cufon.refresh(); within the click function like so:

    //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
    $(“.package-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(“normal”); //Fade in the active content
    Cufon.refresh();// Add this line if using Cufon Font Replacement for tabs
    return false;
    });

    This works when “clicking” on a tab item, but its not adding the “active” class to the :first element on page load until you hover over it. I’m trying so find out how to invoke Cufon.refresh(); on page load to see if this rectifies the problem but without success. Have you or anybody else run into this problem or am I the only one discovering this?

    Thanks again Soh. Cheers.

  291. Badrul Haider

    Can we add a ‘next’ button inside tab content

  292. Shawn Plep

    You saved my skin with this one…I’ve been trying many different pre-packed scripts and tutorials that just didn’t work. As usual, I’ve enjoyed your tutorial and wanted to let you know it helped me.

  293. Taylor

    Nice tutorial. I have one question. I have three tabs on my website. I changed the tabs so they are black and the font to white. I would like the active tab to be white, like it is in the tutorial, but I need only the active font to be black. It needs to be black while the fonts in the black tabs are white. Here is the website. http://www.elite-soccer-fitness.com/

    Please any ideas would be helpful. I have been messing with this all day after work and would like to get it resolved.

  294. taylor

    Never mind. I figured it out.

Speak Your Mind...

Post HTMLNeed to Post HTML Code? Use Postable to post code friendly html. *Wrap all html code in <pre></pre> tags.
Post HTMLNeed to Keep Updated with Comments? Subscribe to comments now.

CSS Gallery