In this demo we’ll be creating a simple tabbed interface using html and css and utilising the power of JQuery to control the appearance of the content. The mechanics of this tutorial are to have a tabbed interface that when a link is clicked displays the corresponding information in the content area below. The interface will degrade gracefully if the user has javascript disabled.
Demo – View demo
The Visual Layout
Firstly we are going to look at our visual layout. For this tutorial I have kept the layout simple with a common tabbed interface with a content area below.

The HTML
Secondly we are going to set up the page structure for our tabbed interface using an unordered list for the navigation controls and a container div for each section. Each link is linked to each section div by the divs corresponding unique id, this is required for our jQuery and we will cover this in the next section.
<div id="tabs">
<ul>
<li><a href="#tab-1">Tab One</a></li>
<li><a href="#tab-2">Tab Two</a></li>
<li><a href="#tab-3">Tab Three</a></li>
<li><a href="#tab-4">Tab Four</a></li>
</ul>
<div id="tab-1">
<h3>Tab 1</h3>
<p>Some content</p>
</div>
<div id="tab-2">
<h3>Tab 2</h3>
<p>Some content</p>
</div>
<div id="tab-3">
<h3>Tab 3</h3>
<p>Some content</p>
</div>
<div id="tab-4">
<h3>Tab 4</h3>
<p>Some content</p>
</div>
</div>
The CSS
#tabs {
font-size: 90%;
margin: 20px 0;
}
#tabs ul {
float: right;
background: #E3FEFA;
width: 600px;
padding-top: 4px;
}
#tabs li {
margin-left: 8px;
list-style: none;
}
* html #tabs li {
display: inline; /* ie6 double float margin bug */
}
#tabs li,
#tabs li a {
float: left;
}
#tabs ul li a {
text-decoration: none;
padding: 8px;
color: #0073BF;
font-weight: bold;
}
#tabs ul li.active {
background: #CEE1EF url(img/nav-right.gif) no-repeat right top;
}
#tabs ul li.active a {
background: url(img/nav-left.gif) no-repeat left top;
color: #333333;
}
#tabs div {
background: #CEE1EF;
clear: both;
padding: 20px;
min-height: 200px;
}
#tabs div h3 {
text-transform: uppercase;
margin-bottom: 10px;
letter-spacing: 1px;
#tabs div p {
line-height: 150%;
}
The JQuery
I have commented each line of the jQuery to explain why each step was undertaken.
<script type="text/javascript">
$(document).ready(function(){
$('#tabs div').hide(); // Hide all divs
$('#tabs div:first').show(); // Show the first div
$('#tabs ul li:first').addClass('active'); // Set the class of the first link to active
$('#tabs ul li a').click(function(){ //When any link is clicked
$('#tabs ul li').removeClass('active'); // Remove active class from all links
$(this).parent().addClass('active'); //Set clicked link class to active
var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
$('#tabs div').hide(); // Hide all divs
$(currentTab).show(); // Show div with id equal to variable currentTab
return false;
});
});
</script>
12 Comments
Just use jQuery Tabs for it?
http://docs.jquery.com/UI/Tabs
I’ve been trying to learn javascript/jquery for quite some time now, and I’m still having a hard time understanding one statement, and it happens to be the line that’s not documented on this tutorial:
return false;
I’m trying to understand when to use this or “return true” in general, as well as why you used it here specifically.
How would you change this code to have the div fade in and fade out?
@Dave
In this example ‘return false;’ is used to disable the default action of the anchor tag. So the action of clicking on a link will run the jquery function but will not load the href attribute.
Whilst ‘return true;’ will allow the default action to process as normal.
@Cody
To fade the visible tab out and fade the new tab in you could adjust the jquery as follows.
$(document).ready(function(){
$(’#tabs div’).hide(); // Hide all divs
$(’#tabs div:first’).show(); // Show the first div
$(’#tabs ul li:first’).addClass(’active’); // Set the class of the first link to active
$(’#tabs ul li a’).click(function(){ //When any link is clicked
$(’#tabs ul li’).removeClass(’active’); // Remove active class from all links
$(this).parent().addClass(’active’); //Set clicked link class to active
var currentTab = $(this).attr(’href’); // Set variable currentTab to value of href attribute of clicked link
$(”#tabs div:visible”).fadeOut(”slow”,function(){ //fade out visible div
$(currentTab).fadeIn(”slow”) //fade in target div
});return false;
});
});
Thanks Andrew. I copied this code and used it in the place of the demo that you gave and now the divs just show up all in a stack with no hiding or fading. What am I missing?
@Cody
Sorry to here your having problems. Sounds like a problem with the jquery. I’ve posted a revised tutorial for you. Have a look at the JQuery tabs with fade tutorial – hope this helps.
Thank you for the tutorial Andrew. After playing around with the CSS in found out that using display:inline for the text area for some reason disables the fade effect, not sure why. I turned off display in the CSS and it works fine. However, since I am trying to create a column layout and not a stacked one I am having some problems trying figure out how to create this look, have it work in IE and also fade. I appreciate your help.
took off clear:both and now it works great!
Great little tutorial but shame on you for putting curly quotes in a blog about code.
[wags finger]
[ then gives thumbs up
]
nice job! thanks
Saved me alot of coding !
Thank you. I like to use one library and not import all sorts of plugins if can be made with simple jquery.
Tested in IE6 working perfect.
Cheers !
One Trackback/Pingback
[...] http://apricotstudios.wordpress.com/…tabs-tutorial/ http://trevordavis.net/blog/tutorial…ed-navigation/ [...]