Tuesday, June 17, 2008

Tabs without JavaScript

It is possible to implement a tabbing system without needing to use JavaScript. All you need is CSS and standard (X)HTML. The downside is that you won't get styling on the active tab to indicate that it is active.

CSS:

div.tabcontainer {
 width: 500px;
 background: #eee;
 border: 1px solid #000000;
}
ul.tabnav {
 list-style-type: none;
 margin: 0;
 padding: 0;
 width: 100%;
 overflow: hidden;
}

ul.tabnav a {
 display: block;
 width: 100%;
}

ul.tabnav a:hover {
 background: #ccc;
}

ul.tabnav li {
 float: left;
 width: 125px;
 margin: 0;
 padding: 0;
 text-align: center;
}

div.tabcontents {
 height: 290px;
 background: #ccc;
 overflow: hidden;
 border-top: 1px solid #011;
 padding: 3px;
 
}

div.tabcontents div.content {
 float: left;
 width: 100%;
 height: 102%;
 overflow-y: auto;
}

div.tabcontents div.content h2 {
 margin-top: 3px;
}

HTML:

<div class="tabcontainer">
 <ul class="tabnav">
  <li><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="tabcontents">
  <div class="content" id="tab1">
   <h2>Tab 1</h2>
  </div>
  <div class="content" id="tab2">
   <h2>Tab 2</h2>
  </div>
  <div class="content" id="tab3">
   <h2>Tab 3</h2>
  </div>
  <div class="content" id="tab4">
   <h2>Tab 4</h2>
  </div> 
 </div>
</div>

Demo page

No comments: