Creating Tabs with HTML, CSS & jQuery

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.

Table of Contents

  1. Skip to the HTML
  2. Skip to the CSS
  3. Skip to the jQuery

1.) The HTML

Completed Code (HTML)

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>

Tab Links

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>

Tab Content

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>

2 The CSS

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.

Completed Code (CSS)

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;
}

Tabs Container

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;
}

Tab Link Wrappers

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;
}

Tab Links

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;
}

Tab Content

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;
}

Non-Active/Active Tab Content

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


3 The jQuery

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.

Completed Code (jQuery)

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

Basic Setup

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

Dynamic Variable

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');

Match Anchor Link to Div #ID

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

Giving The Tab Link an Active State

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');

Prevent Default Anchor Link Behaviour

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.


Extra Credit: jQuery Animations

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.

Default

// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).show().siblings().hide();

Fade

// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).fadeIn(400).siblings().hide();

Slide 1

// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).siblings().slideUp(400);
jQuery('.tabs ' + currentAttrValue).delay(400).slideDown(400);

Slide 2

// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).slideDown(400).siblings().slideUp(400);

Conclusion

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.


Comments

Leave a Comment

Comments (561)

Marloes

January 12, 2016 at 2:12 pm

Thank you! I used this walkthrough after trying to achieve the same thing with a wordpress plugin.

Reply

Abhishek Aby

January 1, 2016 at 8:13 am

step 1: first of all you need to put this code in header ,like this :

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"

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

step 2: Put the HTML code in the body of program:

Tab #1
Tab #2
Tab #3
Tab #4

Tab #1 content goes here!
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.

Tab #2 content goes here!
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.

Tab #3 content goes here!
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.

Tab #4 content goes here!
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.

step 3: put this code in your CSS file:

/*—– 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;
}

step 4: save everything and run the code again. It will work 🙂

Reply

sajjad

December 31, 2015 at 2:14 pm

So good thanks alot 🙂

Reply

Daniel

December 30, 2015 at 7:55 pm

Well, I don’t understand very much of HTML, CSS and this was my first time to work with jQuery but I think I understood it. So congratulations and thanks for this very well done tutorial!

Reply

Jon

December 30, 2015 at 9:31 am

I’m creating a page which has tabs, but also tables and I want to be able to click from one of the table cells (which contain charts) to another tab but I can’t seem to get the syntax correct.

Clicking on the tab headers works fine, but not on the charts. The jscript code and the start of the table is below.

<br />
&lt;script&gt;jQuery(document).ready(function() {<br />
    jQuery(‘.tabs .tab-links a ‘).on(‘click’, function(e)  {<br />
        var currentAttrValue = jQuery(this).attr(‘href’);</p>
<p>        // Show/Hide Tabs<br />
        jQuery(‘.tabs ‘ + currentAttrValue).show().siblings().hide();</p>
<p>        // Change/remove current tab to active<br />
		jQuery(this).parents(‘li’).addClass(‘active’).siblings().removeClass(‘active’);</p>
<p>        e.preventDefault();<br />
    });<br />
	    jQuery(‘.dashboard-charts a ‘).on(‘click’, function(e)  {<br />
        var currentAttrValue = jQuery(this).attr(‘href’);<br />
		var currentDiv = jQuery(this).parents(div).attr(‘id’);</p>
<p>        // Show/Hide Tabs<br />
        jQuery(‘.tabs ‘ + currentAttrValue).show().siblings().hide();</p>
<p>        // Change/remove current tab to active<br />
		jQuery(‘.tabs ‘ + currentDiv).parents(‘li’).addClass(‘active’).siblings().removeClass(‘active’);</p>
<p>        e.preventDefault();<br />
    });<br />
});<br />
&lt;/script&gt;<br />
&lt;div class=&quot;tabs&quot;&gt;<br />
    &lt;ul class=&quot;tab-links&quot;&gt;<br />
        &lt;li class=&quot;active&quot;&gt;&lt;a href=&quot;#tab1&quot;&gt;Programme Summary&lt;/a&gt;&lt;/li&gt;<br />
        &lt;li&gt;&lt;a href=&quot;#tab2&quot;&gt;Programme Dashboard&lt;/a&gt;&lt;/li&gt;<br />
        &lt;li&gt;&lt;a href=&quot;#tab3&quot;&gt;Tasks Dashboard&lt;/a&gt;&lt;/li&gt;<br />
        &lt;li&gt;&lt;a href=&quot;#tab4&quot;&gt;Risks Dashboard&lt;/a&gt;&lt;/li&gt;<br />
    &lt;/ul&gt;<br />
    &lt;div class=&quot;tab-content&quot;&gt;<br />
        &lt;div id=&quot;tab1&quot; class=&quot;tab active&quot;&gt;<br />
			&lt;table style=&quot;width: 100%; text-align: left; margin-left: auto; margin-right: auto;&quot; border=&quot;0&quot; cellpadding=&quot;5&quot; cellspacing=&quot;5&quot;&gt;<br />
			&lt;tbody&gt;<br />
			&lt;tr&gt;<br />
			&lt;td class=&quot;dashboard-header&quot; colspan=&quot;2&quot;&gt;Programme Summary&lt;/td&gt;<br />
			&lt;/tr&gt;<br />
			&lt;tr&gt;<br />
			&lt;td class=&quot;dashboard-charts&quot;&gt;&lt;a href=&quot;#tab2&quot;&gt;&lt;img style=&quot;max-width: 100%; max-height: 100%;&quot; src=&quot;Chart1.png&quot;&gt;&lt;/a&gt;&lt;/td&gt;<br />
			&lt;td class=&quot;dashboard-charts&quot;&gt;&lt;a href=&quot;#tab2&quot;&gt;&lt;img style=&quot;max-width: 100%; max-height: 100%;&quot; alt=&quot;&quot; src=&quot;Chart2.png&quot;&gt;&lt;/a&gt;&lt;/td&gt;<br />
			&lt;/tr&gt;<br />
			&lt;tr&gt;<br />
			&lt;td class=&quot;dashboard-charts&quot;&gt;&lt;a href=&quot;#tab3&quot;&gt;&lt;img style=&quot;max-width: 100%; max-height: 100%;&quot; alt=&quot;&quot; src=&quot;Chart3.png&quot;&gt;&lt;/a&gt;&lt;/td&gt;<br />
			&lt;td class=&quot;dashboard-charts&quot;&gt;&lt;a href=&quot;#tab4&quot;&gt;&lt;img style=&quot;max-width: 100%; max-height: 100%;&quot; alt=&quot;&quot; src=&quot;Chart4.png&quot;&gt;&lt;/a&gt;&lt;/td&gt;<br />
			&lt;/tr&gt;<br />
			&lt;/tbody&gt;<br />
			&lt;/table&gt;<br />
		&lt;/div&gt;<br />

Reply

Amanda

December 24, 2015 at 10:36 pm

Really fantastic walkthrough. Very clearly put and explained in detail. A rarity sometimes. Nice work and I will absolutely be checking out more of what you have here!

Thanks

Reply

shaun

December 17, 2015 at 5:50 pm

So i copied the code as you typed it, but when i click between tabs, the new tab content doesnt load. it stays on tab 1 no matter what i do.. suggestions?

Reply

Mandar Shinde

December 20, 2015 at 6:18 pm

Include the jQuery in your header section and it will work for you

Reply

kshitij

December 23, 2015 at 9:03 am

same problem 🙁
i think my jquery is not working. im trying to implement this on a hubspot template.

Reply

Vince

December 14, 2015 at 11:35 am

So far, this has been the best explanation for this topic I’ve read. I was teaching my friend on how to do this but I have troubles when it comes to explaining so I’ll definitely share this to him. He’ll surely be happy.

Reply

Shubham

December 3, 2015 at 5:33 pm

nicely explained..thankyou

Reply

KLLTKD

December 2, 2015 at 10:15 pm

Thanks for this tutorial. I had a different structure (that worked okay but not clean). This one is cleaner.

Reply

Vishu

November 20, 2015 at 1:20 pm

How to link jquery with html?

Reply

Wet neck

December 18, 2015 at 3:03 pm

You can put it in your

like

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

Reply

Amy

November 5, 2015 at 11:20 pm

I just discovered this tutorial and it really helped me along as a jQuery beginner. Thanks!

My only concern is a graceful fallback. When I disable Javascript on my browser to see how it would appear to users not using JS or even if an error were to occur, I see no fallback and most of my content remains hidden due to the active class.

Does anyone have any thoughts or solutions for this?

Reply

James

October 21, 2015 at 2:03 am

Is there a way to put some tabs inside of my tabs?

Also this is unresponsive in my JS Fiddle. Is it me or the website?
https://jsfiddle.net/u41npgzc/

Reply

Nano

October 20, 2015 at 12:58 pm

hello
where i put the Code (jQuery) pls
im a newbe.
thank you

Reply

kristina

October 12, 2015 at 12:24 pm

Is there any way to make the content box slide from left to right and/or from right to left. More like a slider? Additionally, Is it possible to add prev/next arrows to the content box too?

Reply

Kamar

October 11, 2015 at 1:02 am

Thank you for the tutorial. please i have a question. i am using notepad++ so do i save the jQuery code on another page with another name or do i include it on the html page and use the script tag

Reply

Kamar

October 11, 2015 at 1:00 am

Thank you for the tutorial. please i have a question. Do i save the j

Reply

Drea

October 6, 2015 at 10:53 am

Hi, I’m a newbie and this may be a stupid question, but where exactly do I include the jQuery code? I am working with eclipse and and have a .html and .css in my project.
Thanks!

Reply

Cameron Lackey

October 5, 2015 at 1:53 am

Seb,

I cannot for the life of me, get this to switch between tabs. Is the JQ correct? Is there additional code I need to enter? I am a total beginner.

Reply

Ahmed Viquas

September 18, 2015 at 7:44 am

Thanks for the tutorials. Really helped..

Reply

wajid

September 15, 2015 at 9:39 pm

nice tutorial and it is the same what I am looking for ..Thank you

Reply

alexh

September 5, 2015 at 11:31 am

Hi,

Nice tut. The main.css is a chaos though.
I would suggest to separate the style from the essential files.

Because the way it is I cannot get it to work without losing all time I have left.

Reply

Gourav

September 1, 2015 at 12:53 pm

I am getting a gap between the tabs and the tab contents. How to fix that?

Reply

DJmiso

October 18, 2015 at 6:42 pm

did you ever get a response?

Reply

Eric Thompson

October 30, 2015 at 8:39 am

Do you get a solution for this? I have the same issue.

Reply

ernest

August 28, 2015 at 7:46 am

This is not working in localhost… what is the problem?? please help

Reply

Melanie

August 25, 2015 at 8:15 pm

How do I make the tabs links to other html pages?

Reply

Kaushal Partani

August 6, 2015 at 11:46 pm

Hi, is your complete source code available on github?

Reply

Brian Hegger

August 4, 2015 at 12:10 am

In the jQuery section in which you say:
““If .tabs has a child with an #id that then matches a .tab-link href=””, then they should be linked up”.”
… “child” should be “descendant” in this sentence
OR
you could make it “.tab-content” (in your code and comments) instead of “.tabs” so that child is the correct terminology. In which case you may utilize the plus selector instead of the space selector; i.e.: ““tab-content + ” + currentAttrValue”

Descendant vs. Child is clarified more here: http://www.w3.org/TR/css3-selectors/#selectors

Reply

Patti Macias

July 22, 2015 at 7:11 pm

Hello,
I have a customized page in our student management system for our school. I have tabs which I love to organize and not have to scroll. As I was looking at your tutorial I noticed you have a space in your tab text ‘tab #1’. I had issues with mine and the ref not working correctly when it contains the space. How are you able to add the space?
Thanks,
Patti

Reply

Nekud

July 21, 2015 at 9:18 pm

For those who are getting the clickable tabs but aren’t getting the content to change here is what I did. Just add this line to the jscript before the line e.preventDefault();

jQuery(‘.tab-content ‘ + currentAttrValue).addClass(‘active’).siblings().removeClass(‘active’);

Reply

Abhinav

December 2, 2015 at 5:56 pm

Still not working i think jquey seems to be pretty messed up.please help

Reply

jonathan

December 12, 2015 at 1:58 pm

put this before na script, it should work.

and about the space between tab and its content, u can work around with margin-top:-15; on css of tab-content.

Reply

jonathan

December 12, 2015 at 2:01 pm

src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js”
put this on a script tag before the given jquery

Reply

Damian

July 21, 2015 at 2:46 pm

Helpfull article, thank you!

Reply

Jani Kannisto

August 23, 2015 at 6:49 pm

Well this does not work.

In first lines of javascript there is some sort of error


jQuery(‘.tabs .tab-links a’).on(‘click’, function(e) {

is not an function, says console.

Reply

ronald davis

July 18, 2015 at 3:49 pm

i read all the comments and noone offered a real solution until the code author stated one damn thing http://code.jquery.com/jquery-1.11.1.min.js really dude that is all you had to say at the beginning instead all the bs that people had to go through to get this to work right. Even those who tried to provide help really didn’t . no it works COME-ON-MANNN

Reply

Sreeram

July 17, 2015 at 11:23 am

Great work. For a person who comes with no knowledge in HTML,CSS and Jquery can understand this. Kudos for the post.

Reply

Serena

July 16, 2015 at 11:01 pm

Really awesome! Thanks so much for sharing this.

Reply

Ruchi

July 15, 2015 at 12:37 pm

Mind blowing article. Its very helpful. Thank you so much for sharing this.

Reply

Mansi

July 15, 2015 at 7:14 am

Very Nice article. Since I am Just a beginner in JQUERY . So, can you please add the file extension in which we have to add this code snippet. ex – .js or we can use html with script tag

Reply

Rahamath

July 14, 2015 at 12:50 pm

How to stay current active tab after page load?

Reply

Robert Lintner

July 6, 2015 at 6:45 pm

when adding tabs is this the first thing you put on are there additional tags. Or does this go after the first content?

Reply

Sozon

July 5, 2015 at 8:12 pm

Hey man thanks for the great tutorial.
I am building a small application where I am using a GET in the url of the page where the tabs are displayed, in order to pass some data.
However when clicking on the tabs something seems to interfere , the clicked tab id is displayed right next to the get variable in the url and the show()/hide() of the jquery does not work. It is stuck showing the first tab.
Any thoughts?
Thanks!

Reply

MwangiCJ

July 4, 2015 at 8:38 am

The code would be really nice for my use as I have seen in the demo,
One problem though, my tabs remain static with only tab1 remaining active all through even if i click on other tabs. How can I fix? Thank you.

Reply

Swapnil

July 3, 2015 at 2:48 pm

Thanks!! But the tabs are not clickable in android. If someone could help me, it would be a great help.

Reply

John

June 30, 2015 at 6:02 pm

Thanks for the code. It is working perfect except that tab1 link always appears active and all the other tabs don’t change to active even though the content changes on the tabs panel.

Reply

Hamza

June 30, 2015 at 1:52 pm

Thanks! Extremely useful!

Reply

Harshil Shah

June 28, 2015 at 1:15 pm

Hi there. This tutorial is really great, thanks for it! Saved me a bunch of time.

I was just wondering if it would be possible to somehow open the appropriate tab based on the parameters to the URL. I’m planning on using this for an archives page for my Jekyll blog, and the tabs are gonna be for listing posts by date and tags. So far I’ve been using a separate page for tags, so /tags/#your+tag+here has been good so far to jump to the appropriate place. Wondering if there might be a way to switch to the tags tab if, say, the URL slug begins with a pound as shown above. Thanks in advance!

Reply

Rafael

June 26, 2015 at 2:55 am

Hey, thanks for the tutorial it was very helpful. =]

I would like to know , what is the theme used in this examples ?

Reply

Paul

June 19, 2015 at 3:46 pm

I have managed to add this to the site and it looks great. Just one quick question – how do you go about changing the colour of the text in the menu tabs. I’m not talking about the content underneath, purely the text inside the tab i.e. Tab #1, Tab #2 etc

Reply

Mollie

June 18, 2015 at 7:21 pm

First, I’d like to thank you for your website. It was very helpful, especially for an utter novice like me!
Also, I have a question. I successfully created tabs and they look great. However, I want to move them up and to the right. I tried to position them (position: relative; right: 50px) but that didn’t work. I inserted the code in the .tabs section of the CSS. Either 1. the tabs don’t count as element/a unit that can moved as a whole so the code I’m using isn’t what I need or 2. I’m just not putting the code in the right place

Sorry if this is a silly question, but like I said, I do not know much about it.

Thanks for you help and your great website!

Reply

Julia

June 18, 2015 at 7:17 pm

Thank you so much for this tutorial! It was a big help.

At first I couldn’t get the tabs to work either, or figure out the jquery, but then I realized I needed to upload the jquery.js file to my site. Everything works great!

Reply

general

June 16, 2015 at 4:30 pm

for those having problems with the tabs not changing, i have found a fix which has worked for me:

in the first line of the HTML code replace:

div class=”tabs”

with
div class=”tabs standard”

Reply

Don

June 16, 2015 at 3:05 pm

Is it possible to put all of this in the HTML? I’m updating a site with which I don’t have a lot of control of the HEAD and include files. But I can put html in a module. Can both the css and jquery be included in one place on the document? Thanks.

Reply

Srj

July 17, 2015 at 11:26 am

Yes Don

Make sure you link your html file to the jquery file.

Reply

Anggit Prayogo

June 15, 2015 at 8:06 am

Hey this really work guys, you really help me out . Thanks man!!

Reply

Inspirational Newsletter


Join the newsletter to get the best articles, tutorials and exclusive freebies every two weeks.

No spam. You can unsubscribe at any time.