CSS Overlapping Tabs Menu
In this tutorial you’re going to learn to create a simple, yet cool menu with overlapping tabs. I’ve kept it simple not because of the lack of time or inspiration, but in order for you to be able to improve on it and modify it in any way you need/want, both code and graphics. It should also be easy to understand its “inner workings”, as we will see during this tutorial.
This should be a fairly easy to follow tutorial, although you should know your basic HTML and CSS before you try it out.
First of all download the tab image (right-click, then Save As…) which contains both on and off states. You can edit/customize this image in any way you want to fit your pages. You need to place this image in the same folder as your page
The HTML
Create a new HTML page in Dreamweaver, or Notepad for that matter. Make sure you’ve got the DOCTYPE set as XHTML.
In the body tag of the page insert this code:
<div id="ts_tabmenu">
<ul>
<li><a href="http://www.tutorialsphere.com/"><strong>Tutorialsphere</strong></a></li>
<li><a href="http://www.tutorialsphere.com/tutorials/photoshop"><strong>Photoshop</strong></a></li>
<li><a href="http://www.tutorialsphere.com/tutorials/illustrator"><strong>Illustrator</strong></a></li>
<li><a href="http://www.tutorialsphere.com/tutorials/fireworks"><strong>Fireworks</strong></a></li>
<li><a href="http://www.tutorialsphere.com/tutorials/flash"><strong>Flash</strong></a></li>
<li><a href="http://www.tutorialsphere.com/tutorials/css"><strong>CSS</strong></a></li>
<li><a href="http://www.tutorialsphere.com/tutorials/php"><strong>PHP</strong></a></li>
</ul>
</div>
What I did there was assign to each menu item a list item from an Unordered List (UL). I did this for the convenience of easy formatting using CSS.
The way the above code works is as follows. Think of your tab image as a two piece puzzle. We need two pieces in order for our tabs to stretch according to the length of the word it contains. First piece of the puzzle is a 10px wide piece of the tab. The second piece is the remaining part of our tab.
The CSS
You can either paste this code in the HEAD section of your page, wrapped in STYLE tags, or you can create a stylesheet (.css) file and paste it in there, then link to it in your HTML page. In order for everything to work right off the bat, you need to place the .css file in the same folder as the image and the HTML page. Otherwise you need to adjust some paths in it.
#ts_tabmenu {
font-size: .75em; /* set the font size */
padding: 20px 0px 0px 20px; /* set the padding */
}
#ts_tabmenu ul {
line-height: 1em; /* setting the line height now so we don't have any headaches*/
margin: 0px; /* let's keep the margin set to 0 for the same reasons as above*/
list-style-type: none; /* we remove the UL's default disc bullets */
float: left; /* we float the list to the left like we will all the elements inside*/
padding: 0px 0px 0px 5px; /* give it a left padding of 5px to counter the effect of margin -5px below */
}
#ts_tabmenu ul li {
float: left; /* floatin' left */
}
#ts_tabmenu ul li a {
text-decoration: none; /* remove the default underline off the anchor text */
display: block; /* we display this text as block so that we can apply padding/margin without problems */
float: left; /* floatin' left, to make the menu horizontal */
padding: 0px 0px 0px 10px; /* we give it a left padding of 10px to show the first 10 pixels of the tabs image. you can also use padding-left: 10px; */
background: url(tabs.gif) no-repeat left top; /* we link to the tabs image, no tiling, showing the top left part of it */
margin-left: -5px; /* this is important for the overlapping part - we are overlapping the tabs by 5 px */
z-index: 0; /* keep it on layer 0 (default) */
position: relative; /* very important as this enables the z-index to work for us and keeps the tabs where they should be */
color: #666666; /* color of the tab text */
}
#ts_tabmenu ul li a strong {
font-weight: normal; /* remove the bold effect */
display: block; /* display the strong element as a block so we can pad it, etc. */
float: left; /* float it left as well */
background: url(tabs.gif) no-repeat right top; /* now we show the right part of the tab and we complete the "puzzle" */
padding: 6px 10px 7px 5px; /* important, as through this you define the position of the text within the tab */
cursor: pointer; /* this makes the browser show the "hand" cursor when hovering over the tab */
}
#ts_tabmenu ul li a:hover {
position: relative; /* again, keep things relative */
z-index: 5; /* we show this tab over all other tabs in the menu, which would be on layer 0, thus overlapping occurs */
background: url(tabs.gif) no-repeat left bottom; /* now we show the bottom part of the tabs image, the "hover" instance */
color: #000000; /* we color the hovered tab's text black */
}
#ts_tabmenu ul li a:hover strong {
background-image: url(tabs.gif) no-repeat;
position: relative; /* keep it relative */
z-index: 5; /* show this on layer 5 as well */
background-position: right bottom; /* we show the right bottom part of the tabs image (the hover instance) */
}
The Result
I bet most of you scrolled over to this section, didn’t you?
Here’s our final menu.
Something Extra
Some of you might want that tab to stay on when the user clicks it and enters that page. You can do that with this menu with two small code additions.
You need to give each list item (LI) an id, like so <li id="li_tsmenu2">. This id needs to be unique on that page. Now our HTML code would look like this:
<div id="ts_tabmenu">
<ul>
<li id="li_tsmenu1"><a href="http://www.tutorialsphere.com/"><strong>Tutorialsphere</strong></a></li>
<li id="li_tsmenu2"><a href="http://www.tutorialsphere.com/tutorials/photoshop"><strong>Photoshop</strong></a></li>
<li id="li_tsmenu3"><a href="http://www.tutorialsphere.com/tutorials/illustrator"><strong>Illustrator</strong></a></li>
<li id="li_tsmenu4"><a href="http://www.tutorialsphere.com/tutorials/fireworks"><strong>Fireworks</strong></a></li>
<li id="li_tsmenu5"><a href="http://www.tutorialsphere.com/tutorials/flash"><strong>Flash</strong></a></li>
<li id="li_tsmenu6"><a href="http://www.tutorialsphere.com/tutorials/css"><strong>CSS</strong></a></li>
<li id="li_tsmenu7"><a href="http://www.tutorialsphere.com/tutorials/php"><strong>PHP</strong></a></li>
</ul>
</div>
Now that we have our id’s in place, we need to specify the id of the body tag, which should be something similar to the id of the list item we want to highlight.
For example, say we want to highlight the Photoshop tab. Let’s give the body tag an id of “tsmenu2″
<body id="tsmenu2">
Up to this part, our modifications would be pretty much useless if we didn’t have the CSS to enable us to use them. We need to make the following addition to our CSS code:
#tsmenu1 #li_tsmenu1 a, #tsmenu2 #li_tsmenu2 a,
#tsmenu3 #li_tsmenu3 a, #tsmenu4 #li_tsmenu4 a,
#tsmenu5 #li_tsmenu5 a, #tsmenu6 #li_tsmenu6 a,
#tsmenu7 #li_tsmenu7 a {
position: relative;
z-index: 5;
background: url(tabs.gif) no-repeat left bottom;
color: #000000;
}
#tsmenu1 #li_tsmenu1 a strong, #tsmenu2 #li_tsmenu2 a strong,
#tsmenu3 #li_tsmenu3 a strong, #tsmenu4 #li_tsmenu4 a strong,
#tsmenu5 #li_tsmenu5 a strong, #tsmenu6 #li_tsmenu6 a strong,
#tsmenu7 #li_tsmenu7 a strong {
background-image: url(tabs.gif) no-repeat;
position: relative;
z-index: 5;
background-position: right bottom;
}
You will notice that we’ve used the same rules as we did for the hover state of our tabs. You can obviously modify this so that the “current” tab has a different look, but for now let’s go with this
What I did there was define rules for each of the menu items, but since they would all be the same, I’ve organized them into two groups. We got #tsmenu1 #li_tsmenu1 a which is a CSS rule with multiple selectors (3 in this case). The way this works right here is this rule is only applied to the anchor contained within an element with the id li_tsmenu1 which also must be contained within an element with the id tsmenu1. Which is what we need: we’ve got the body with the id #tsmenu1 containing the li with the id id li_tsmenu1 and the anchor inside the li – Bingo!
I’m somewhat notorious for confusing explanations, so if the above was too much for you, here’s the working example of all that talk.
In order to change the currently highlighted tab you need to change only the id of the body tag and nothing else.
The End
Well folks, this concludes my first CSS tutorial ever. I hope it was easy enough to understand, but if you have any questions, the comment form is right there. You can use/modify this menu in any way you need, without my permission. A link back would be nice, however
Thanks for reading, you two!

70 Comments for CSS Overlapping Tabs Menu
great tutorial, thanks for sharing
Like it allot, will be using this on my website at http://www.dannykorf.com
Aha ive been looking for something like this. Bookmarked for future reference
This is great. How do you add text to the tabs?
*applause*
Would it be possible to Overlap the tabs so Tutorialsphere is above Photoshop is above illustrator and so on?
Wade: The current (selected) tab is the one above all other tabs. I don’t see this tab system working and be intuitive in any other way, though. (i.e. different overlapping look)
Gabriel: I mean like this http://img198.imageshack.us/img198/5470/navu.jpg
So the tabs overlap the other way
?
I’m a little busy with work these days. Turns out it’s a bit harder than I thought to do reverse tabs.
I’ll look over some code for inspiration.
very nice
Great tutorial and nice result. Some suggestions:
- replace strong tag with span (no need to set font weight then)
- for the selected tab you only need to set a class .selected in the anchor you want selected. Then add in css #ts_tabmenu ul li a.selected:hover and #ts_tabmenu ul li a:hover strong (or better span). No need to play with the body tag.
Hey kostas,
Thanks very much for the suggestions.
The reason I did it so that you only need to modify the body id is that it’s a lot easier (in my opinion) to just change that around. It also allows you to keep the HTML code for the menu intact throughout your pages, which I have found to be very helpful.
Anyway, thanks again.
I see your point, and you are right that it is easier to modify just an id of a certain tag. That tag though, should not be the body tag. Just imagine what would happen if every web part (which this menu is) needed the id of the body to work properly. I insist that the right way is what I mentioned above, but since you want the easiest one, just put the id=”tsmenu2″ in *your* ul tag instead. Everything else stays intact
Yes, I suppose that could work as well. Or even make up a wrapper div and use its id. Lots of possibilities.
For overlapping in the other direction, it is tricky. The reason is that since the following tab overlaps the previous one (this is how they are drawn by the browser) you should find a way to draw them in reverse order. And the only way I can think of right now, is to float them right (but this will change the order of the list). Another way would be to play with z-index. I’ll look into it when I find time and I will post again.
Thank you Gabriel for your tutorial
To me it was very challenging one on the tab menu when i created one for http://www.keebu.com
It was made for dynamic menu as well as the first item and the last item are 2 different ways of representation of graphics.
Well, i had to do lots of hacks inside php code too.
Client was very happy on the work.
Hey Nice tutorial.. I also wrote an overlapping tabs tutorial recently based on the designed created by collis of nettuts.com
great tutorial…love the overlapping tabs definatly gives a great feel to any site…how would i go about getting it to center i’ve tried essentially everything & float has no center property it’s only left, right, none, or inherit
thanks again for the tutorial!
okay here’s a tricky one…i want to use the secondary part of this tutorial (where when you click on the tab it stays highlighted)…how would i do that with an iframe? would i have to use an on.mouseclick script?
I’d like to use something similar to this on my website but I need submenus. Is it possible to have a dropline submenu associated with some of the tabs, so that when you rollover the tab a non-tabbed submenu appears below it (ie just a horizontal bar)?
But there is one thing I’ve also noticed in this site’s menu – the background images should be in one image file and used by setting background-position property to avoid loading delay on hover…
This is kick a@#! Thanks for writing such a great tutorial. I have a project I am working on now and I kept putting off the navigation menu because I still haven’t figured out how I wanted to execute it. Your example is exactly what I needed to accomplish. Thanks for saving me some time:)
This is not confusing at all, Gabriel. Excellent tutorial. Every day, I am more surprised at the sheer power of CSS and all the things you can do with it. Great work!
Keep posting stuff like this i really like it
This is an incredible post. Its amazing to see what CSS can do
I am taking the liberty of adding this article to my CSS aggregator site. Hope you dont mind.
Hi – great tutorial – just wondering though, am trying to implement this code with a drop down and not having much luck… any chance of some guidance?
thanks…
great blog thank you
Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!