In this tutorial, we’re going to look at how to create tabs in HTML, CSS and jQuery. If you get stuck, don’t hesitate to ask me a question.
Tabs are a great way of grouping lots of content into a very small space. Think of tabs like the TARDIS…bigger on the inside. They can be incredibly handy when you have a lot of content that would simply fill up your page with too much information in one go.
You might also like: Creating an Accordion with HTML, CSS & jQuery.
I go into detail on the specifics of the HTML layout below.
<div class="tabs">
<ul class="tab-links">
<li class="active"><a href="#tab1">Tab #1</a></li>
<li><a href="#tab2">Tab #2</a></li>
<li><a href="#tab3">Tab #3</a></li>
<li><a href="#tab4">Tab #4</a></li>
</ul>
<div class="tab-content">
<div id="tab1" class="tab active">
<p>Tab #1 content goes here!</p>
<p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis.</p>
</div>
<div id="tab2" class="tab">
<p>Tab #2 content goes here!</p>
<p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis. In hac habitasse platea dictumst. Ut euismod tempus hendrerit. Morbi ut adipiscing nisi. Etiam rutrum sodales gravida! Aliquam tellus orci, iaculis vel.</p>
</div>
<div id="tab3" class="tab">
<p>Tab #3 content goes here!</p>
<p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum ri.</p>
</div>
<div id="tab4" class="tab">
<p>Tab #4 content goes here!</p>
<p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis. In hac habitasse platea dictumst. Ut euismod tempus hendrerit. Morbi ut adipiscing nisi. Etiam rutrum sodales gravida! Aliquam tellus orci, iaculis vel.</p>
</div>
</div>
</div>
Starting off there’s a div with class="tabs"
. Inside there’s an unordered list, (or an ordered one if you like, it makes no real difference), with class="tab-links"
. Inside the list items there are anchor links. The first list item is marked as class="active"
. This indicates where to start the tabs.
Note: When the CSS is all done, the tab link parent with class="active"
will be the highlighted one on page load.
<div class="tabs">
<ul class="tab-links">
<li class="active"><a href="#tab1">Tab #1</a></li>
<li><a href="#tab2">Tab #2</a></li>
<li><a href="#tab3">Tab #3</a></li>
<li><a href="#tab4">Tab #4</a></li>
</ul>
</div>
There’s a div with class="tab-content"
. Inside of that, we have 4 divs, each with class="tab"
, (this number corresponds to how many tab links there are). The currently visible tab will also need an active class, e.g. class="tab active"
.
<div class="tab-content">
<div id="tab1" class="tab active">
<p>Tab #1 content goes here!</p>
<p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis.</p>
</div>
<div id="tab2" class="tab">
<p>Tab #2 content goes here!</p>
<p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis. In hac habitasse platea dictumst. Ut euismod tempus hendrerit. Morbi ut adipiscing nisi. Etiam rutrum sodales gravida! Aliquam tellus orci, iaculis vel.</p>
</div>
<div id="tab3" class="tab">
<p>Tab #3 content goes here!</p>
<p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum ri.</p>
</div>
<div id="tab4" class="tab">
<p>Tab #4 content goes here!</p>
<p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis. In hac habitasse platea dictumst. Ut euismod tempus hendrerit. Morbi ut adipiscing nisi. Etiam rutrum sodales gravida! Aliquam tellus orci, iaculis vel.</p>
</div>
</div>
I’m going to keep it simple with basic CSS (as opposed to an SCSS pre-processor like Sass, Less or Stylus). The bulk of this tutorial is aimed at beginners, so I’m taking that into account.
I go into detail on the specifics of the CSS styling below.
/*----- Tabs -----*/
.tabs {
width:100%;
display:inline-block;
}
/*----- Tab Links -----*/
/* Clearfix */
.tab-links:after {
display:block;
clear:both;
content:'';
}
.tab-links li {
margin:0px 5px;
float:left;
list-style:none;
}
.tab-links a {
padding:9px 15px;
display:inline-block;
border-radius:3px 3px 0px 0px;
background:#7FB5DA;
font-size:16px;
font-weight:600;
color:#4c4c4c;
transition:all linear 0.15s;
}
.tab-links a:hover {
background:#a7cce5;
text-decoration:none;
}
li.active a, li.active a:hover {
background:#fff;
color:#4c4c4c;
}
/*----- Content of Tabs -----*/
.tab-content {
padding:15px;
border-radius:3px;
box-shadow:-1px 1px 1px rgba(0,0,0,0.15);
background:#fff;
}
.tab {
display:none;
}
.tab.active {
display:block;
}
The display:inline-block;
adds a type of clearfix while also making it more ‘stable’ than if we used display:block;
.
The reason we use a clearfix above yet not here is as follows: display:inline-block;
sometimes adds 7px-ish of margin below the affected element with no way of removing it. Sometimes using a minus bottom margin works, but it depends heavily on how your HTML is structured. In this instance it’s fine.
/*----- Tabs -----*/
.tabs {
width:100%;
display:inline-block;
}
We declare no list-style to stop it appearing as a default list. Then the margin so the links are spaced evenly apart, (using margin:0px 5px;
is the same as using margin:0px 5px 0px 5px;
).
.tab-links li {
margin:0px 5px;
float:left;
list-style:none;
}
These are all basic styles which you should understand if you’re attempting this tutorial. If you don’t, please find the proper values and the definitions for each on the CSS-Tricks Almanac.
You may be wondering why I’ve used transition:all linear 0.15s;
instead of transition:background linear 0.15s;
. The reason is simple, in my experience, you have to think ahead. If you don’t then nasty, (sometimes hard to fix), bugs pop-up seemingly out of nowhere. By using background instead of all, we’d be limiting what can be transitioned on the :hover
effect. Since this is a tutorial, many of you may want to change the text colour on hover. By using all instead of background that makes it possible.
.tab-links a {
padding:9px 15px;
display:inline-block;
border-radius:3px 3px 0px 0px;
background:#7FB5DA;
font-size:16px;
font-weight:600;
color:#4c4c4c;
transition:all linear 0.15s;
}
.tab-links a:hover {
background:#a7cce5;
text-decoration:none;
}
li.active a, li.active a:hover {
background:#fff;
color:#4c4c4c;
}
This is the background of the tab content section.
/*----- Content of Tabs -----*/
.tab-content {
padding:15px;
border-radius:3px;
box-shadow:-1px 1px 1px rgba(0,0,0,0.15);
background:#fff;
}
We set all tabs to display:none;
which hides them. Then we set the active tab to display:block;
so only that one will show up. It’s a simple case of using CSS the way it was designed, which was to cascade down. Hence the name, Cascading Stylesheets.
.tab {
display:none;
}
.tab.active {
display:block;
}
You might also like: Creating a Dropdown Menu with HTML & CSS
Below you can see the completed jQuery code. Keep scrolling for a good explanation of how it all works and what it all means.
Let me clarify something before we start: Everyone has their own way of coding, with jQuery this is mine. Writing it out like this makes it easier for a beginner to grasp, it’s also efficient.
jQuery(document).ready(function() {
jQuery('.tabs .tab-links a').on('click', function(e) {
var currentAttrValue = jQuery(this).attr('href');
// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).show().siblings().hide();
// Change/remove current tab to active
jQuery(this).parent('li').addClass('active').siblings().removeClass('active');
e.preventDefault();
});
});
You have to start by checking if the document is ready, for this we use:
jQuery(document).ready(function() {
// Code Here
});
Next is to check if the user has clicked on a tab, to do that we simply reference every link that happens to be a tab link with .tab-links a
.
jQuery('.tab-links a').on('click', function(e) {
// More Code Here
});
Next, we create a variable called currentAttrValue
, (current attribute value), which grabs the href=""
of the clicked anchor link.
var currentAttrValue = jQuery(this).attr('href');
We need a way of matching up the .tab-link
anchors with .tab
sections. We start off by referring to the entire tabs component, with .tabs
, then we say “If .tabs
has a child with an #id
that then matches a .tab-link
href=""
, then they should be linked up”.
Then we say “Show the element with the referenced #id
, such as #tab2
, then find the other tabs, which are siblings of #tab2
, and hide them”.
jQuery('.tabs ' + currentAttrValue).show().siblings().hide();
Changing the active state of the tab link is relatively the same as showing/hiding the tab content.
First, we look for jQuery(this)
, which is the current anchor link being clicked. Then we find it’s parent list item with .parent('li')
tag. After that, we give that parent list item a class of .active
by using .addClass('active')
. Lastly, we find all siblings of that parent list item and remove, if any, classes of .active
by using .siblings().removeClass('active')
.
jQuery(this).parent('li').addClass('active').siblings().removeClass('active');
You may notice that when you click an anchor link with an #id specified instead of an actual URL, it tends to add the #id into the address bar. E.G. http://sebkay.com
becomes http://sebkay.com/#id
. This can be a massive pain-point for users when trying to click the back button.
To get past that UX problem we use:
e.preventDefault();
This handy little function stops the default action of the clicked element from happening.
Remember when we put that function(e)
at the start of our click event? Well, this is where that ties in.
You may not like the way it just jumps to the new tab content without any nice animation of any kind, well here’s some extra credit.
To apply these changes simply replace the below snippet with the other code snippets that follow.
// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).show().siblings().hide();
// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).fadeIn(400).siblings().hide();
// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).siblings().slideUp(400);
jQuery('.tabs ' + currentAttrValue).delay(400).slideDown(400);
// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).slideDown(400).siblings().slideUp(400);
If you’re reading this conclusion, (most people, including me, tend to leave after they’ve got the code they were after), then please leave a comment below with any problems or bugs regarding this tutorial. I’m writing this to help people learn and become the best they can be.
You might also like: 21 Must-Have Front End Development Tools.
Join the newsletter to get the best articles, tutorials and exclusive freebies every two weeks.
Surya
August 27, 2017 at 6:34 am
Hello,
I am interested in your tutorial on “Creating Tabs with HTML, CSS & jQuery”. But, I wonder, does the codes (html, css, and jQuesry) can be written in one .html file?
Best regards,
Surya
Bree
June 23, 2017 at 9:50 pm
Thanks! Very helpful!
Mel
May 14, 2017 at 11:33 pm
Awesome ! Thanks
Ian Haney
April 30, 2017 at 5:08 pm
Hi
I am using your tabbed content in a website I am designing but the tabs are not working, I am not sure if the javascript is conflicting with something, can you help please, if can email me or reply, I can send the coding over. I wonder if it could be to do with the website built in bootstrap?
Anirban Das
April 18, 2017 at 7:49 am
Hi,
I tried implementing your codes in one of my newform.aspx page in a SharePoint list. The problem which I am facing is – only when i.e. the tab is active, the contents of the tab are visible but the inactive tabs (When I click on them), it doesn’t show anything. If I make all of them active, the content gets displayed in the same page and the jquery just helps me to pop up on that particular section content. My requirement is to display these contents in different tabs and not on a single page with different sections.
I have applied the jquery along with the HTML Code and CSS, but is there anything I am missing out over here?
Thanks and Regards,
Anirban Das
Savio Scott
September 22, 2017 at 1:15 pm
You’ll need to create a script.js file and copy the jQ coding to that.
Then add the below code just above the tag:
Tania Salgado
April 5, 2017 at 10:50 pm
Hey! I literally tried this code an hour ago, and then i tried it again, and it’s not working… help? The info is not changing when clicking the tab, nor is it appear as active.
Mike
January 30, 2017 at 9:12 pm
I’m sure there is some great info in here, but I find it very difficult to read when you use a black background with your code examples. Yes, I am old, but I can still read if I can see it.
Sean
January 26, 2017 at 7:23 pm
Awesome tutorial looks great and easy instructions plus great explanation thank you so much for posting this!
varsha
January 4, 2017 at 12:28 pm
How to add tab within tabs. If I am
tab in tabs content than those tabs are seen In all tabs but I want it only one tab. So I want sub tabs shorty for one parent tab but not for another parent tabs
Peninah
November 30, 2016 at 6:55 am
Just used this tutorial and it’s working beautifully. Thank you so much for taking the time to write it out so clearly! I was able to learn and not just copy.
Peter Wilson
October 29, 2016 at 1:44 am
Am a beginner and you need to better explain you css code the way you explain things still have me wanted more but i have learnd alot i must say thank you very much but i need more you need to explain each and every line of you css code remember its for beginners sorry to say but am new to this and i love it
and also the jQuery part dose not work and which file do you put it in??? html or css
Amit
October 28, 2016 at 4:43 pm
This is great. Learning is so much fun. You rock !
Rebekah
October 26, 2016 at 8:14 pm
When I click each tab nothing is displayed but the url makes it look like it should be showing each tab.
Chris
October 24, 2016 at 4:29 am
There’s a small error in your JQuery. The line
[javascript]
jQuery(‘.tabs ‘ + currentAttrValue).show().siblings().hide();
[/javascript]
should be
[javascript]
jQuery(‘.tab-content ‘ + currentAttrValue).show().siblings().hide();
[/javascript]
The ‘tabs’ class refers to the tabs themselves, not the content. So if someone tries to use your example as written, the content doesn’t change.
Quin452
February 6, 2017 at 4:39 pm
I’ve followed this tutorial through, and it works, to an extent.
I want to use the different animation styles, but nothing seems to work. As it is now, from the tutorial, I can easily switch between the tabs, and it only breaks when I comment out both jQuery lines before the preventDefault.
I’ve tried using fadeIn and also tab-content as suggested, but that breaks it too.
Commenting out the show line still doesn’t break it, nor does commenting out the parent line after; it has to be both to break.
Any ideas?
louise
October 21, 2016 at 6:47 pm
hi i just wrote a comment and sent the wrong url file. it isnt working at all in fiddle for me at all
Maranda
October 14, 2016 at 11:59 am
Thank you for the demo. The tabs are very clean.
However I am using your code in SharePoint 2013. Everything is fine until I add the CSS code…then the page locksup but I can see the tabs. Do you know if I need to add additional code for CSS because I am using SharePoint?
Deny
September 9, 2016 at 2:44 pm
Thanks. It was very useful for me. Thank you again!
Jim D.
July 25, 2016 at 12:43 am
I like your tabs very much, but am having one difficulty. As the stylesheet is written, it (at least on browsers I have tested) displays the tabs with a substantial amount of space between the text (as in Tab #1, Tab #2, etc.) and the contents of that tab., so that the tabs no longer appear to belong to the tab contents below (they look fine in the example you provided). The only way I have been able to fix that is to change the style to:
.tab-links li {
margin: 0px 5px -16px 5px; [instead of 0px 5px;]
The problem with using as specific a measurement as -16px for the bottom margin is, of course, that it may look different on various browsers. And that is not even dealing with responsive web design issues and using pixels. I copied your material directly from your site, but obviously I am doing something wrong. Can you tell me where I am going wrong? Thanks.
Scot
June 29, 2016 at 5:52 pm
Thank you for the informative explanations. As a beginner to web coding I found them very easy to follow.
June
June 15, 2016 at 3:38 pm
Excellent tutorial and a heartfelt thank you for the detailed explanation. Not only did you save me who-knows-how-many hours of trial and errors but also allowed me to understand the CSS/JQuery usages. Thank you again!
Terrence
June 11, 2016 at 1:08 am
Great tutorial, very clean and helpful! Thank you! How would I go about linking from my main nav in my header to one of the tabs created in this tutorial on a different page that’s not active?
amr mohamed habeb
June 9, 2016 at 9:44 am
thank you very much
I have benefited greatly from this information
Lenora
June 1, 2016 at 3:11 pm
I’m having trouble with my Tab Links…whenever I click on the tab it brings the beginning of the tab content to the top of the page. Code is written exactly like yours.
Help?
Bharath
May 26, 2016 at 2:22 pm
Thanks for explaining each and every component in detail, which is very difficult find in resources available.
Khaula
May 25, 2016 at 6:11 pm
Upon Page refresh the control goes to the default tab instead of the current tab. How can we prevent this.
Joseph
May 23, 2016 at 10:23 pm
This has been very helpful for a project I am working on. Would it be possible to have this change the header image (located above) when it changes the tab?
Thanks! GREAT WORK!
Jerry Webb
May 13, 2016 at 2:17 am
Here is my tab HTML.
I copied your CSS and JQuery code into a .css and a .js file.
I have those files uploaded to my hosting account.
How can I get it so that the tabs look like tabs instead of a mere string of text?
Where do I need to place the link to the js file in the html file?
Payment
Shipping
Returns
Partners
Payment
Please pay for your item promptly when the listing ends.If you have purchased multiple items you can request an invoice to have your purchases combined.All payments are processed through PayPal.
Shipping
All items are shipped promptly after received a cleared payment.Please see the shipping details section above for the different shipping options that we provide.
Returns
Please contact us FIRST through eBay messaging if there are any problems with your order.Our specific return policy details are described above.We value your satisfaction and will do everything we can to ensure that you are happy with your purchase.Thank you!
Partners
www.SpencerCoffman.comwww.MichaelWilliams67.comwww.Affiliates2Go.com
Lily
May 9, 2016 at 1:39 pm
Thank you very very much!
Vishal
May 8, 2016 at 2:08 am
Great Tutorial. One recommendation, add a screenshot how page looks after execution above code
Bharath
May 26, 2016 at 2:23 pm
+1
gbee
May 4, 2016 at 9:21 pm
Great thanks for this. Very useful!
Chetan
April 28, 2016 at 6:25 am
Amazing are your tutorials . Thanks for your time
Mike
April 27, 2016 at 10:55 pm
Hi! Very neat and simple solution! thanks a lot for taking the time of doing this! Very helpful tutorials, have a great day
Mark G
April 21, 2016 at 2:09 pm
Have to say i really appreciated this tutorial, i got a working html page within 15 minutes and learnt a couple of things while reading it …
Just thinking for the extra lazy people, might be handy to have a ‘completed’ section which has complete demo i.e. complete with tags so they can cut and paste it into Notepad.
Thanks for the help!
Mark G
http://affinities.rocks
Churchill Id
April 4, 2016 at 4:54 pm
Hi,
I want to have more than one tab section in a page. How do I go about it.
Thank you
Kristen
April 4, 2016 at 10:15 am
Does anyone know how to make it so that no tab is active when the page first loads?
Ramanjulu
March 19, 2016 at 7:39 am
Hi i need active tab. which consist HTML and CSS for blogger please help me
Arun
March 18, 2016 at 2:04 am
I liked this tutorial, the step by step approach was very helpful.
Honey Thakuria
March 10, 2016 at 11:12 am
Really helping and simplest code ever seen 🙂
Crisoforo Lopez
March 7, 2016 at 7:25 pm
Getting this error from the tabs.js :
Uncaught TypeError: undefined is not a function tabs.js:3(anonymous function)
sometimes it works sometimes it doesn’t, know why?
Ryan
March 4, 2016 at 3:10 am
Awesome tutorial, I did have problems when the contents were dynamically loading. Basically the hidden tabs don’t layout correctly.
Shrabanee
March 3, 2016 at 6:32 am
Hi,
Good one with nice description.
Instead of making everything hard coded in html, you should try making things parametric.
This way it can help a lot.
Thanks.
Alskafi
February 28, 2016 at 12:55 pm
I was looking for this idea, and Iiked the way you explained the code more than the code it self.
Simply brilliant.
Adam
February 19, 2016 at 3:36 pm
I’m new to jQuery and such… How might I implement this sort of tab with Weebly? Thank you.
Entity
February 2, 2016 at 8:59 pm
Thanks! Very clean and I love the code explanation you put in here.
Blake
February 2, 2016 at 6:08 pm
Any chance you have a tutorial on how to add dropdown sub-menus to these tabs?
Vinu
February 2, 2016 at 12:13 am
I see a lot of people here having trouble in displaying tab contents when clicked on tabs.
Please change the line
jQuery(‘.tabs ‘ + currentAttrValue).show().siblings().hide();
to
jQuery(currentAttrValue).show().siblings().hide();
Because in the line “var currentAttrValue = jQuery(this).attr(‘href’);” you have already given a reference.
And for those of you having trouble with jQuery make sure to download latest jQuery development version from jQuery.com and include the refernce in your section.
Or you can include a CDN (Content Delivery Network) hosted by Google or Microsoft.
Google CDN:
Microsoft CDN:
Maria
January 27, 2016 at 2:55 pm
Can’t seem to get it to switch to the other tabs, think the JQuery isn’t working????
How do i fix it? I’ve tried putting it in the header already 🙁
KS
January 22, 2016 at 5:02 pm
Thank you so much for this! Maybe the best tutorial I have ever seen. So easy to follow and well-explained.
Donna
January 18, 2016 at 9:47 am
I want to use tabs on my blog. My site is on hubspot which has a specific blog template in place. I want to add the comments form to one of the tabs but the form code exists in a separate place on the page that the rest. If I add the tab content tag around the two different sections, will the script find it all and put it in the correct tabs?
Cobalt
January 16, 2016 at 9:04 am
do i have to copy the whole thing or just the one im using?