Create a DHTML Tab Strip
Tab strips are commonplace throughout the web, and they’re utilized in a wide variety of ways, such as in site navigation or form wizards. Organizing links in a tabbed format can add quite a bit of style and functionality to your site, as many visitors are already familiar and comfortable with using tabs on other websites and even on other platforms such as their operating system. This tutorial shows you how to create a simple DHTML tab strip using HTML, CSS, and a small bit of JavaScript.
When I use the term “DHTML,” I am referring to “Dynamic HTML.” This is an endearing term that many web designers use to describe the fusion of HTML with other web technologies, such as JavaScript and CSS. By using various technologies in tandem, we can create dynamic controls that allow website visitors to interact with content directly on the page without the need to refresh the site with every subsequent click.
The HTML
As you will soon see, the tab strip is a prime example of a DHTML control. Before we delve into every aspect of the tab strip, let’s first start with the HTML:
The HTML is relatively simple. We are using an unordered list (<ul/>
) for our tabs, and we are using <div/>
tags for our tab content. As you can see, I am giving a unique id
attribute to each tab link and to each tab content <div/>
. This is important, as we’ll need a unique id
in order to access these elements with JavaScript.
The JavaScript
We are using only one small JavaScript function for the tab control. Here is the code:
The CSS:
This CSS is a very basic example of what you can do to style your tabs. You can add colors, gradients, and background images to give the control even more design flare.
The Result:
In true DHTML fashion, we have combined HTML, JavaScript, and CSS to create a fully dynamic user control that can appear anywhere on the web. I have taken the fundamentals learned in this tutorial and have created a more visually appealing example of this code. Click on the image below to see the demo.
Note that the demo also utilizes Karate Corners to create rounded edges on the tabs. Learn more about Karate corners here.