Heads up! This was written in 2007 and many things have changed since then. This is probably not the most modern approach.
Last year I came up with the original semantic tab box, now I’m improving it. Here are the things I wanted to fix:
- Keep JavaScript unobtrusive — no inline event handlers and needs to work without JavaScript.
- Create a standard API that can be easily reused and customized.
- Make the markup more flexible.
- Support both vertical and horizontal tabs.
- Don’t constrain the height of the box — let it expand to the size of the content.
- Keyboard navigation.
How is this better that other tab boxes out there?
All the tab boxes I’ve seen overcomplicate their solutions. They start with an unordered list of the tab names, followed by the content blocks. With a bit of CSS this looks very nice, but for screen readers the content is disconnected from the tab names and can be very confusing. Just try turning off CSS, you’ll see how horrible this looks:
The Internet is a worldwide, publicly accessible network of interconnected computer networks that transmit data by packet switching using the standard Internet Protocol (IP).
Accessibility is a general term used to describe the degree to which a system is usable by as many people as possible.
Progressive enhancement is a label for a particular strategy of Web design that emphasizes accessibility, semantic markup, and external stylesheet and scripting technologies…
Notice how the content is disconnected from the “tabs” when CSS is disabled (or when being read by a screen reader).
To solve this problem, most people just duplicate the tab name with a header (h2, h3, etc) at the top of each content block and use CSS to hide them from visual users. Here’s an example of what this solution looks like without CSS:
Internet
The Internet is a worldwide, publicly accessible network of interconnected computer networks that transmit data by packet switching using the standard Internet Protocol (IP).
Accessibility
Accessibility is a general term used to describe the degree to which a system is usable by as many people as possible.
Progressive Enhancement
Progressive enhancement is a label for a particular strategy of Web design that emphasizes accessibility, semantic markup, and external stylesheet and scripting technologies…
Sure this works, but why the redundancy? Yuck!
My solution gets rid of the unordered lists and formats the content blocks logically — with header tags.
Enough talk…
…lets get to the examples:
In <head>
<script type="text/javascript">
var tabs = new Tabbox("editorTabs")
</script>
In <body>
<div id="myTabbox">
<!-- Internet Tab -->
<div class="tabPanel selected">
<h3><a href="#interWebTab">Internet</a></h3>
<div id="interWebTab" class="tabContent">
<p>
The Internet is a worldwide...
</p>
</div>
</div>
<!-- Accessibility Tab -->
<div class="tabPanel">
<h3><a href="#accessTab">Accessibility</a></h3>
<div id="accessTab" class="tabContent">
<p>
Accessibility is a general term used to...
</p>
</div>
</div>
</div>
Pretty simple huh?
Accessibility and Non-JavaScript Support
This new version supports accessibility by using CSS to hide the content divs (<div class="tabContent">
) off screen, instead of with the unaccessible display:none CSS property.
These CSS classes are applied with JavaScript, so the content will be viewable if JavaScript is disabled or broken on the page. Try viewing the examples with JavaScript disabled and you’ll see what I mean.
API
To setup the tab box all you need to do is initialize the Tabbox object with the main div ID:
var box = new Tabbox("myTabbox", { header: "h3",
vertical : true,
select: 1,
autosize: true });
All the values in the second parameter are optional:
Name |
Default |
Description |
header |
“h3” |
The header element used for each tab name. This element cannot be used anywhere else in the content block. |
vertical |
false |
If the tabs should be oriented vertically. The tabs are setup horizontally by default. |
select |
0 |
The tab index to select initially. |
autosize |
true |
Automatically size the box to the content of the tab. |
The code has been commented and layed out logically so updating it with features and enhancements should be easy. Speaking of enhancements…
Advanced Example — Hey Look! Something shiny.
So far the examples have been rather simple and plain so I wanted to create one that was more advanced. This example has a more complex UI, animations and implements the YUI history manager. Now when you change tabs it adds history to your browser and the back button will select the previous tab. Check it out — it’s the Über-Cool, Shiny, Tab Box.
Download
Now that you’ve seen the examples, you can download all the code and use it on your site:
tabbox2.zip (46K)