1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. |
* {
margin:0;
padding:0;
}
body {
background:url(../../images/background.png) top left;
font: .8em "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
h2{
margin-bottom:10px;
}
#wrapper{
width:720px;
margin:40px auto 0;
}
#wrapper h1{
color:#FFF;
text-align:center;
margin-bottom:20px;
}
#wrapper a{
display:block;
font-size:1.2em;
padding-top:20px;
color:#FFF;
text-decoration:none;
text-align:center;
}
#tabContainer {
width:700px;
padding:15px;
background-color:#2e2e2e;
-moz-border-radius: 4px;
border-radius: 4px;
}
.tabs{
height:30px;
}
.tabs > ul{
font-size: 1em;
list-style:none;
}
.tabs > ul > li{
margin:0 2px 0 0;
padding:7px 10px;
display:block;
float:left;
color:#FFF;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
-moz-border-radius-topleft: 4px;
-moz-border-radius-topright: 4px;
-moz-border-radius-bottomright: 0px;
-moz-border-radius-bottomleft: 0px;
border-top-left-radius:4px;
border-top-right-radius: 4px;
border-bottom-right-radius: 0px;
border-bottom-left-radius: 0px;
background: #C9C9C9; /* old browsers */
background: -moz-linear-gradient(top, #0C91EC 0%, #257AB6 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#0C91EC), color-stop(100%,#257AB6)); /* webkit */
}
.tabs > ul > li:hover{
background: #FFFFFF; /* old browsers */
background: -moz-linear-gradient(top, #FFFFFF 0%, #F3F3F3 10%, #F3F3F3 50%, #FFFFFF 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FFFFFF), color-stop(10%,#F3F3F3), color-stop(50%,#F3F3F3), color-stop(100%,#FFFFFF)); /* webkit */
cursor:pointer;
color: #333;
}
.tabs > ul > li.tabActiveHeader{
background: #FFFFFF; /* old browsers */
background: -moz-linear-gradient(top, #FFFFFF 0%, #F3F3F3 10%, #F3F3F3 50%, #FFFFFF 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FFFFFF), color-stop(10%,#F3F3F3), color-stop(50%,#F3F3F3), color-stop(100%,#FFFFFF)); /* webkit */
cursor:pointer;
color: #333;
}
.tabscontent {
-moz-border-radius-topleft: 0px;
-moz-border-radius-topright: 4px;
-moz-border-radius-bottomright: 4px;
-moz-border-radius-bottomleft: 4px;
border-top-left-radius: 0px;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
padding:10px 10px 25px;
background: #FFFFFF; /* old browsers */
background: -moz-linear-gradient(top, #FFFFFF 0%, #FFFFFF 90%, #e4e9ed 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FFFFFF), color-stop(90%,#FFFFFF), color-stop(100%,#e4e9ed)); /* webkit */
margin:0;
color:#333;
}
|
72 Responses
Just implemented this on a rather large project I’m working on. After doing a little bit of tweeking on the CSS, i got it to look just right. Excellent work!
Thanks a lot. Glad to be of help.
While going through this article I noticed that you are setting the z-index on the tabs but yet I don’t see any place where you set positioning to something other than the default static positioning. According the specification found here:
http://www.w3schools.com/css/pr_pos_z-index.asp
z-index only works with elements that use fixed, absolute or relative positioning. So I’m not entirely sure your z-index is doing anything at all.
That being said I could be missing something, after all I’m kind of just the new guy here.
You’re absolutely right. The z-index was doing nothing. When I originally wrote the CSS I was going to have the tabs and content absolutely positioned but I changed my mind. The z-index was left over from there and I forgot that I’d changed it when I wrote the post.
I’ll amend the post and all the source and demo files.
Thanks for telling me. If you spot any more errors be sure to get in touch!!
I’m trying to implement this code, but the page is not hiding all of the tabpages (except the 1st) when I initially load the page. Thoughts?
Could you send me what you have done and I’ll check it through for you?
Thanks for this script. It’s so beautifully concise!
One quick note–there seems to be an extra line (#7) dropped into the javascript code box in the article. Threw me for a loop when I kept getting alert boxes! (Yes, I’m a bit new to this….)
It’s not in the demo or download source, so if I had copied from there, I’d have saved myself some trouble, but I thought I’d pass the info along in case others make my mistake.
Also, I had a question about “.querySelector”, which I learned doesn’t work in IE7 and below. It makes sense that you use it, since you’re gearing towards newer browsers anyway, and it certainly simplifies the code. However, I’d like to increase my compatibility. I know it won’t be as elegant, but could you give me some clues as to how I could make this work without querySelector?
Thanks again!
Hi there.
Thanks for the great comments.
I’ve re-written the code so it will work in legacy browsers.The css needs looking at especially for IE6 but the javascript works.
Heres the file LegacyTabs
Hope it helps.
problem is that JS remove ALL content that is wrapped in DIV, also broke my other JS (galleryffic)
Hi Martin
Could you send me the code and I’ll have a look at it for you?
Cheers
The main problem was in double onLoad calls, which I fixed, another problem is, that:
//hide two tab contents we don’t need
var pages = tabcon.getElementsByTagName(“div”);
remove all div in code, which will screw my formating. Not sure whoat is this code good for anyway, I change it to:
var pages = tabcon.getElementsByClassName(“tabpage”);
but this doesnt work in IE7
BTW, can you rewrite this code for jQuery?
Hi Martin glad you sorted the onLoad calls.
The line:
var pages = tabcon.getElementsByTagName(“div”);
puts all the div elements within the tabscontent div into an array that you then can call later.
The reason I used this method is because getElementsByClassName doesn’t work in IE6-8.
So i don’t really know what you mean by this comment.
As for the rewrite. I’ve written a plugin for jQuery. Here: jQuery Tabs
awesome page! Thanks!
Hey awesome tutorial. But for some reason I can’t get the tabs to hide any suggestions. I followed the instructions to a tee.
Hi Jessica
Sorry for the late reply. Have you got a page link so I can look at the code?
Matt
Hi Matt,
I’m trying to get the text in the main content area formatted into two columns, but I’m having trouble achieving that. I’m trying to work w/ divs styles via CSS. Any ideas how to do it?
Thanks!
Gily
Hi Gily
Put 2 div’s in the tabpage div. Then style as follows:
#divLeft { width:320px; float: left;}
#divRight { width:320px; float: right;}
Then add the following to the styles for the outer div the tabpage:
overflow:auto;
That should work fin. Any issues get back to me or give me a link to the code.
Cheers
Matt
Thanks a lot for your prompt reply, Matt. Your solution works great – I had missed the overflow thing…
Gily
hey matt. nice job…
would it be possible to target one of these created tabs to open from another page? i’m using a similar system now but can’t seem to nail down how to open a specified tab on “page load”
many thanks in advance…
seth
hi,
i’ve got total of 31 tabs(each calendar day) and wanted to put the navigation button like in excel. could you please kindly give us some examples?
i’ve been looking for the solution for a while
Hi Matt -
I have ben looking for something like you have here and wish to talk to you
please e mail me back
Thanks
Can i make this to Vertical format?
thank you very match >>
i have Question ..
how i make A circular motion To move between tabs >>
For example 5 sec?
Hi Matt,
I was playing around with your tutorial and I noticed something strange. I added an extra tab so that I would have 4 total. I added the frame with a new div like this:
After this, I noticed that when I tried to add images in to the tab content section in the 2nd and 3rd tabs, they didn’t stay within the frame. In the 1st and 4th tab, images stayed within the frame. I’ve been playing around with the images and it’s specifically in the 2nd and 3rd tabs that the image pops out of the frame. I’m guessing it’s something in the java script?
If you or anyone else on here can help me with this issue, please let me know.
Thanks!
Thank you for the code, i’m using it in my website, but my issue here is the same as issue submitted by seth, which how set a specified tabs when the page load.
thank you,
this is the best pure tabbed view i’ve seen
is it possible to add back button funtionality
thx in advance
Great method! Any way to add an easing affect to this? Would make this perfect!
Thanks!
Also how can you make it so tab number 2 is active on load instead of tab 1?
Thank you!
Figured out the active tab question.
on this line of code add the ID name of the active tab you want after the li:
var navitem = container.querySelector(“.tabs ul li#tabHeader_2″);
Still searching for a way to have an easing effect on the div transitions.
Help please!??
Nevermind only tab 2 is active but tab 2 content is not. Back to the beginning
thanks, thanks, thanks! great help.
I want to use this code in my WordPress site on Contact Us page… How can I use this code there.. Help
Figured out the fade in of the div
(function ($) {
$.fn.acidTabs = function (options) {
options = $.extend(options);
return this.each(function () {
var o = options;
container = this;
container.setAttribute(“class”, o.style);
var navitem = container.querySelector(“li”);
//store which tab we are on
var ident = navitem.id.split(“_”)[1];
navitem.parentNode.setAttribute(“data-current”, ident);
//set current tab with class of activetabheader
navitem.setAttribute(“class”, “tabActiveHeader”);
//hide two tab contents we don’t need
var pages = container.querySelectorAll(“.tabpage”);
for (var i = 1; i < pages.length; i++) {
pages[i].style.display = "none";
}
//this adds click event to tabs
var tabs = container.querySelectorAll("li");
for (var i = 0; i < tabs.length; i++) {
tabs[i].onclick = displayPage;
}
});
// on click of one of tabs
function displayPage() {
var current = this.parentNode.getAttribute("data-current");
//remove class of activetabheader and hide old contents
$('#tabHeader_' + current).removeClass('tabActiveHeader');
$('#tabpage_' + current).hide();
var ident = this.id.split("_")[1];
//add class of activetabheader to new active tab and show contents
$(this).addClass("tabActiveHeader");
$('#tabpage_' + ident).fadeIn('fast');
this.parentNode.setAttribute("data-current", ident);
}
};
})(jQuery);
Thank you – a nice lightweight solution.
Hiya, I’m trying to make this work in IE7 and have used the legacy tabs solution however there’s a problem with the javascript and it hides everything on page load so none of my tabs show. Can you help?
Hi Matt or a CSS guru;
I’ve a problem which seems to occur when I place the tabs in the sidebar of a WordPress theme I’m developing. When I set the width of tabContainer to 300px in the demo, everything resizes fine. However, when I float it within a div in the sidebar, the tabs indent about 30px and I can’t get them to align to the left of the .tabscontent box.
Would appreciate any guidance.
Thanks.
hi,thanks for the info , one thing i noticed is the tabs are not reset perfectly if you check with different browsers like firefox and ie the tabs drop about 2 px.
hi…….
I have problem in ie7,ie8 this tabs not working..
plz give me suggestion, what should i do ?
Thanks a lot.
Hey Matt!
Great Work! I would love to use it on my website. Is there a way i can include multiple Containers with Tabs on one page?
Thanks alot!
[...] Back to Tutorial [...]
Great piece of code and very concise instructions for us novices.
I was able to change almost everything to create what I need, except a couple things:
1. is there a way to change the tab width with a percentage so it re-sizes the tabs based on the number of tabs to fill the horizontal space minus the padding in between?
2. besides using code like WEEK 1 to select the current tab linked from an email, is there a way to dynamically make the current week the current tab?
THANKS FOR ANY HELP.
is it possible by click submit button and it leads to the corresponding nearer tab?
Just nice, works perfectly! Thank you to share!
This is great. How would you recommend multiple uses on the same page?
I’m builidng a new page and using this, which is fantastic. Works great in Chrome and Firefox, but the .js is not working in IE8 (getting this error:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB7.3; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; MS-RTC LM 8)
Timestamp: Mon, 13 Aug 2012 00:54:50 UTC
Message: Object doesn’t support this property or method
Line: 7
Char: 5
Code: 0
URI: http://www.playhouseonpark.org/js/tabs.js
Here is a sample page:
http://www.playhouseonpark.org/pop/20122013Season/mainstageseries_miceandmen.html
It looks great in the other two browsers, so I’m hoping maybe it’s an easy tweak to get it to work in IE, because this is exactly what I was looking for. Thanks!! Rick
I’m so glad I found your blog, I’ve been scratching my head about a lot things, but a couple of hours spent here at work (time well spent) and I feel like I completely get HTML5 now. Can’t wait to get home and put it into practice.
One question: Is it at all possible to create a link in tab 3 to go to an anchor in tab 2, without refreshing the page?
Okay one more question: is it possible to link to the page from somewhere else, and open tab 2 by default?
(I think Seth and Jake asked this question earlier, and nearly sussed it, but not quite).
Thanks
Thanks Matt for your great article.. God Bless you..
No problem thanks for the comment
Yeah.
Check out my jQuery plugin for multiple tabs on a page.
Cheers
Matt
$(‘#tabs li’).click( function ( ) {
$(‘#tabs li’).addClass(‘unactivetab’);
$(this).removeClass(‘unactivetab’);
$(this).addClass(‘activetab’);
$panel = $(this).children(‘a’).attr(‘href’);
$(‘#tab-contents div’).addClass(‘unactivepanel’);
$($panel).removeClass(‘unactivepanel’);
$($panel).addClass(‘activepanel’);
} );
how is this code for switching tabs ?
Thank You Matt. Great article. Really helped me out a lot.
Hi,
I am trying to use this code for my blog.
But I am not getting proper result.
Result showing tabs but they are not clickable and all content is reflecting on first tab.
Please help me to get it fix.
Thanks
Thanks for the fantastic template for HMTL 5 Tabs. It is 90% better than the DekiScript Tabs I was working on.
Thank you once again.
Nice post, really awesome navigation solution.
Hi Matt,
can you give me some ideas on how to implement sub tabs.
Thanks in advance
Thanks for the codes i will forward the page to my webmaster i am sure he will like it
Hi Matt,
Thnx works great, btw in you sample file you have put in the google analytics code.
Don’t forget to close your tag for #wrapper in your HTML
Hi,
I am using your code and works very good for Firefox (16.01) also when I copied to the server and test the site works OK, but for IE 8 and IE9 the screen is complete in black and the tabs not work.
So I have tested the demos and Works for this kind of IE versions, I could not find the problem.
Some suggestions to fix this.
[...] Pure Javascript Tabs with HTML 5 and CSS3 my-html-codes.com 30/11/2012 · Javascript · [...]
Awesome tutorial , great work.
I implement your tab code in one of my website but i am getting one problem. you provided main tree tabs as follow
now whenever i m write another div tag inside your “tabpage” div it get disappear.
please suggest some solution as early as possible.
hello,
thanks for amazing tabs, but i have some issue when i put content of tabs and make zoom the ccontent appear out div .
i put all div-width : auto
i’m trying to implement this on a bigcommerce site, but the content loads then disappears. the js is some how blocking content that should be shown, know of any way I could trouble shoot this? Thanks!
thanks for amazing code ..i relly like it but i have issue
when i put the code on my page it,s ok but when i meak zoom in the content of tab be out side the div..and the pic &font zise be bigger i wont it the fixed
i hope there is soulotion for that
thanks agine
Hi Matt Walker
If I want to use multi tab in my page what i must be do???
I’ve been playing around with these tabs, and they’re pretty snappy. I’d like to put canvas objects *inside* the tabs (on a tab?), but I’m finding that the objects won’t display. Does anyone have any insight into why not, or what I can do to make it work?
[...] Source: http://www.my-html-codes.com/javascript-tabs-html-5-css3 [...]
I try include a form in each tab content you create, matt. But every time i submit a form (example i submit form in tab 2), it always goes back to tab 1 and tab 1 content. how to make it stay in current submitted form tab?
Help me anyone…?
Huge time saver. Thanks!
Anyone know how to display the content on hover rather than on click? I tried changing onclick=displayPage to onMouseOver=displayPage, but no luck. Seems like that should work though…
hi Matt,
Possible to clarify the code below a bit further. I’m kinda lost – Wanted to create two columns.
Put 2 div’s in the tabpage div. Then style as follows:
#divLeft { width:320px; float: left;}
#divRight { width:320px; float: right;}
Then add the following to the styles for the outer div the tabpage:
overflow:auto;
Thanks,
FM