CSS TABS - CSS Only “DOM TABS”
September 12, 2007 @ 11:46 am . Comments (23)
While working on my company site redesign, and a new project CSS Uber Clean Gallery I came across a cool CSS trick
There are several variations of “dom tabs” out there, and lets be honest they work pretty well. If a user has javascript everything works as planned, and if not you can read all of the content as all laid out vertically. However you can accomplish the same effect using just CSS, saving you the javascript download and dependency.
What do you need?
So the essentials of any dom tab script include different sections of content some which are hidden, one which is active, and a navigation bar that lets you switch between them.
We will keep it simple and start by building our basic structure in HTML (figure 1.1)

<ul id="navigation">
<li><a href="#c1">Content Block 1</a></li>
<li><a href="#c2">Content Block 2</a></li>
<li><a href="#c3">Content Block 3</a></li>
</ul>
<div class="content">
<h1>Heading 1</h1>
<p>Here is some content, I hope that you like it!</p>
</div>
<div class="content">
<h2>Heading 2</h2>
<p>Now that you have read content block 1, you can learn more in this block</p>
</div>
<div class="content">
<h3>Heading 3</h3>
In conclusion, content blocks are fun
</div>
How do we make it work?
So now you have the basic structure of the site and content, but there is a problem; It is just layed out flat on the page, it needs some styling and it needs to hide and show content!
We will start with some basic styling. On comes the CSS:
#navigation { margin: 0 10em 0 0; }
#navigation li { display: inline; padding: 0 5em 0 5em; }
.content { background: #CCCCCC; height: 50em; }
a { color: #0066FF; }
a:hover { color: #00CCFF; }
a:active { font-weight:bold; }
As you can see I styled everything in “em” set a height, and gave the links some function. More on this later…
The nitty gritty
So now we have the structure, we have the styling, we have to get to the functionality. The magic here is a container div set to “overflow: hidden” that is set as the same height of the content blocks. This will cause only one active block of content to be shown at any given time. Then we will give each set of content a named anchor so that the links will jump to that content at any given time (figure 1.2)

we then add the xhtml:
<div id="container" > ... previous code ... </div>
and the css:
#container { height: 20em; overflow: hidden; }
That is about it! Pros/cons
There you go! Here is an example to see it in action. Feel free to download it and repost it as you would like.
Now there are definite pros and cons to this method. The pros are you are keeping it simple, no need for javascript, it works in all browsers, and is accessible to anyone browsing with out CSS, screen readers, etc.
However there are some con’s. You have to have a set height for the content, meaning you need to know how much (or little) is going to be in each content block. You also have to use EM’s for sizing so that if someone has an elarged font none of the copy will get chopped off. Finally there is no way to highlight the active tab unless you use some sort of javascript.
Want to see a more practical use? Check out my company website. With javascript turned on you get fun prototype slides, however with it turned off you can still navigate through all of the tabs as if you had DOM tabs! Cool no?
Tag: Web Design
« My latest brochure proof | Some of us need help writing (me?) »

COMMENT Pedro Rogério
Very good, as I never thought about this before, it is so simple!
COMMENT Dan Shields
Wow, never thought of this either, great post!
COMMENT MarkO
Another problem is that even though you specified the height of the content divs, if the elements inside it are greater than the height it will still overflow.
To solve this, you can hide content if it overflows:
.content {
overflow: hidden;
}
Using
overflow: autowill not work.. It produces nasty bugs.COMMENT MarkO
Another problem is that even though you specified the height of the content divs, if the elements inside it are greater than the height it will still overflow.
To solve this, you can hide content if it overflows:
.content {
overflow: hidden;
}
Using
overflow: autowill not work.. It will produce something not quite good.. ^_^COMMENT Trevor
There’s just one problem. It doesn’t work in Safari 2 for Mac. Love it or hate it, Safari is a legitimate web browser and people should not suffer just because they don’t use Firefox (old machines or short on RAM.)
If a technique is going to be written about, then it needs to work across all major browsers currently in use, or we’re really not that much farther ahead.
COMMENT Ywg
“it works in all browsers”
Well, it does not work on Opera neither on Safari.
The idea is clear and simple, but it sure does need a little more testing.
COMMENT PHP Tutorials
I always prefer CSS tabs over tabs generated through javascript. Less chances of breaking. Easy to maintain and modify.
COMMENT craig
I have been working along similar lines for presenting hidden content with CSS. Opera has problems with this idea while Firefox and even IE can process it. Here is one example:
http://www.rumble.sy2.com/emptyhead/xcss/opera_hidden.html
I have also done demos with :target and :hover to bring the content into view, using z-indexing to stack layers of content.
No perfect solution yet…
COMMENT ross
Excellent resource Craig, thanks for the link. It seams we are at a point where theoretically these sorts of techniques should work on all modern browsers, there are a just a few that are lagging behind.
At some point we should get to a point where javascript and css can perform similar tasks, and the question of “presentation vs behavior” should dictate which we use. Seems we are not quite there yet?
COMMENT napyfab:blog» Blog Archive » links for 2007-09-17
[…] » CSS TABS - CSS Only “DOM TABS” - Web Design Marketing Podcast & Blog (tags: webdesign css tabs dom tutorial design web tab howto development library menu navigation) […]
COMMENT Skylog » Blog Archive » links for 2007-09-18
[…] CSS TABS - CSS Only “DOM TABS” (tags: css) […]
COMMENT Website Design
Very nice. Clean and effective. I’d implemented something very similar to this on one of my past projects. I’d never put much thought in to it. I’m going to digg up my old code and mull it over.
COMMENT RUDEWORKS
[…] estúpido. Simplemente jugando con links a selectores y un div con la propiedad overflow: hidden, CSS TABS logra una bonita interfaz de […]
COMMENT Nachlese September 2007 - Die Seiten des Monats | Nachlese
[…] CSS Only DOM Tabs Ein auf Registerkarten basiertes Navigationsmenü lässt sich ganz elegant auch ohne JavaScript, mit reinem CSS erzeugen. […]
COMMENT Best of September 2007 | Best of the Month
[…] CSS Only DOM TabsThere are several variations of “dom tabs” out there, and they work pretty well. However you can accomplish the same effect using just CSS, saving you the javascript download and dependency. […]
COMMENT elv
Another drawback, is that it break the back button, as every tabs is an anchor so each click is saved in the history.
COMMENT lost node » Blog Archive » Best of September 2007
[…] CSS Only DOM TabsThere are several variations of “dom tabs” out there, and they work pretty well. However you can accomplish the same effect using just CSS, saving you the javascript download and dependency. […]
COMMENT Anes
So easy and simple. Great!
COMMENT Bill
I’ve noticed that the browser “Back” and “Forward” buttons don’t work. The URL changes, but it doesn’t move through the content. I’m using Firefox, BTW…
COMMENT 行搏客 » 2007年9月最佳
[…] CSS Only DOM Tabs There are several variations of “dom tabs” out there, and they work pretty well. However you can accomplish the same effect using just CSS, saving you the javascript download and dependency. […]
COMMENT sanny
I noticed that once you click on the tabs the entire page shifts up or down to depending where you place the tabs in your design to focus on the container.
Any workarounds for that??
COMMENT poss
people have been doing this on vampirefreaks.com for ages.
its bad.
badbadbad.
it doesnt work in Opera >.
COMMENT trademark registration
Very nice–clean and effective. I’d implemented something very similar to this on one of my past projects. I’d never put much thought in to it. I’m going to digg up my old code and mull it over.