Tabbed Interface

Update 18 Aug 2008: Added an openTabOfElement method and now check for the hash of the url and open the tab that contains the hash item. Try opening the Contact tab. This also works for form validation routines when you want to set focus back to a field that contains an error. If the tab is closed you will get an error. To open the tab pass the element to the tabs.openTabOfElement method before setting focus to the form element.

tabbedinterface1.gif

I liked the tabs created in Sliding Doors of CSS, Part II by Douglas Bowman. Please read the article so that you will understand how to modify the CSS of the tabs. The tabs are very nice for site navigation, but I wanted to use it in a tabbed interface within a page so that a user could click on each tab without reloading the page. First I started with the markup for the tabs.

<div class="tabs">
  <ul>
    <li class="selected"><a href="#home">Home</a></li>
    <li><a href="#news">News</a></li>
    <li><a href="#ps">Products & Services</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#contact">Contact</a></li>
  </ul>
</div>

A class is used on the div instead of an ID so that mutiple and nested tabs can be created. Next the markup for the tab body and tab content (tabItem).

<div class="tabBody">
	<div id="home" class="tabItem selected">
		<h2>Home Tab</h2>
	</div>
	<div id="news" class="tabItem">
		<h2>News Tab</h2>
	</div>
	<div id="ps" class="tabItem">
		<h2>Products & Services Tab</h2>
	</div>
	<div id="about" class="tabItem">
		<h2>About Tab</h2>
	</div>
	<div id="contact" class="tabItem">
		<h2>Contact Tab</h2>
	</div>
</div>

Here we give the overall tab body a class and each tab item an ID. When javascript is disabled all tabs display and the ID is used for navigation within the page. Because classes are used for each item in the tab you can nest tabs within another tab as seen in the demo.

Usage

Download the tabbed interface files and add the following to the head section of your page (modified for your directory structure).

<script type="text/javascript" src="js/events.js"></script>
<script type="text/javascript" src="js/tabs.js"></script>
<link  type="text/css" media="screen" rel="stylesheet" href="css/tabs.css">

On DOMContentLoaded the tab function will add a classname of tabs to the body. This is used to tell the CSS that tab items can be hidden from view since javascript is enabled. To see this in action view the demo with and without javascript enabled. Then click behavior is added to each tab. When a tab is clicked the tabs.showTab() function is called to display the tab content.

Sample and Dowload

Related Articles

Sunday, January 21st, 2007 04:11:18 PM in  CSS  Web Development 

6 Comments

Gravatar Image1. Posted at 2/24/2007 7:38:13 AM by Pascal Venier

Very nice! :-)

Gravatar Image2. Posted at 1/11/2008 12:15:31 AM by jitendra

i got an error on ie 6 ( stack overflow at line 0)

Gravatar Image3. Posted at 2/20/2008 3:41:03 AM by gos

I have found several DHTML 'nested tab interface' examples, but I guess this is one of the best one.

I really like the example.

:-)

Gravatar Image4. Posted at 8/17/2008 11:12:16 AM by kuroitora

Simple, customizable and stable ;) Thanks for this! it's honestly the best I've seen as far. One question though, is there a way to specify the tab that is highlighted when the user lands on the page? For example, an external page can link to page#tab_01, page#tab_02 etc..?

Gravatar Image5. Posted at 8/18/2008 1:20:09 PM by Tanny O'Haley

Hi Kuroitora,

I updated the code today to support your request to open a tab if the URL hash is the ID of an element within a tab. Please see the update message at the top. Also the code now requires util.js as well as events.js.

Tanny

Gravatar Image6. Posted at 8/18/2008 6:23:39 PM by kuroitora

Thanks Tanny, you're a lifesaver!!

Add a comment

Comment Preview

Comment Guidlines

Keep comments relevant. Inappropriate, offensive or off topic comments may be edited and/or deleted. Advertisements will be deleted.

If you choose to comment, please leave a real name, not an alias, and an email address or a link to your own site.

E-mail addresses will not be displayed. If you leave an e-mail address and I respond to your comment you will receive the response at your e-mail address.