Go to the menu - Go to the contents

[Site map] You are here --- > Newbies Paradise > Tutorials > Unofficial > Website > XHTML / CSS > Reading a tutorial

Tab menus

Avatar
Author : anonymousguest
Creation : : on 06/26/2007 06:07:20 AM
Last modification: : on 06/26/2007 03:48:03 PM
Rate and comment this tutorial
Print this tutorial
You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
This tutorial is a translation, the original version has originally been written in french by Xav57.


Hello everyone! :D

In this tutorial, you are going to learn how to create tab menus very simply. The reading of M@teo21's tutorial about XHTML/CSS is however advised to fully understand it.

The example that will be used in the whole tutorial is Newbies Paradise's menu.
Chapter Contents :

XHTML part

Let's begin at the beginning :lol: !

The XHTML part is the simpliest. Indeed, we are only going to create a menu using an unordered list with <ul> and <li>. Then, as in a menu there are necessarily links, we are also going to use the <a> tag. The attribute "class" will be useful to differentiate the current tab. The whole will be surrounded by a <div> tag.

Finally, we must get this:

Code: HTML
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<div id="menu">
  <ul id="tabs">
    <li class="current"><a href="home.html"> Home </a></li>
    <li><a href="forums.html"> Forums </a></li>
    <li><a href="guestbook.html"> Guestbook </a></li>
    <li><a href="staff.html"> Staff </a></li>
    <li><a href="register.html"> Register </a></li>
    <li><a href="login.html"> Login </a></li>
  </ul>
</div>


The three important elements are: the activation of a link with class="current", the tag <div id="menu"> that will enable us to position the menu in the page, and the tag <ul id="tabs"> that will be used to modify the style.

If we look at the result of this code, we get this:

User image


As you can see, it's a very simple menu. :lol:

We are now done with the XHTML part. It hasn't been very complex so far :) , don't worry the following will not be much harder ;) !

CSS part

Now, the question is:

How do I set up the tabs?

Let's go to the code directly, I'll explain it after :)

Code: CSS
1
2
3
4
5
6
7
8
#tabs
{
    font : bold 11px Batang, arial, serif;
    list-style-type : none;
    padding-bottom : 24px;
    border-bottom : 1px solid #9EA0A1;
    margin-left : 0;
}

I select the menu with #tabs, not with #menu, because it will only be used to position the menu in the page!

Here, I change the font, I remove the list bullets and I set a border at the bottom of the list items. The padding-bottom indicates the "height" of the bottom border. To help you to understand, change its value and it'll be clearer! ;) I also removed the left margin so the menu is at the left side of the page.

Code: CSS
1
2
3
4
5
6
7
8
9
#tabs li
{
    float : left;
    height : 21px;
    background-color: #F4F9FD;
    margin : 2px 2px 0 2px !important;  /* For all browsers but IE */
    margin : 4px 2px 0 2px;  /* For IE  */
    border : 1px solid #9EA0A1;
}

Here, I create my tabs o_O . They are within <li> tag, therefore I must select them using #tabs li. I create my tabs with: border:1px solid #9EA0A1. I also add a small margin so the cells and the text are spaced.

The float : left is used to align the tabs horizontally.

I could also have used display : inline, but the result isn't the same!

You also must specify the height of the cell.

The height depends on the bottom margin set previously. If you increase or decrease one of the two margins, you'll have to adapt the other.

Both margin's are required because Internet Explorer doesn't interpret CSS like other browsers!
We are getting closer from our goal! :)

User image

Let's move on, it's nearly finished! :D

Code: CSS
1
2
3
4
5
#tabs li.current
{
    border-bottom: 1px solid #fff;
    background-color: #fff;
}

Now, I select the tabs with the id: "menu". As each tab is surrounded by <li> tags, I select them too. And I select the class: "current" to modify only the activated tab.
I set a background color and I add a border at the bottom of the cell which will cover the existing one.

Code: CSS
1
2
3
4
5
6
7
#tabs a
{
    display : block;
    color : #666;
    text-decoration : none;
    padding : 4px;
}


Here, I'm working on the links within the tabs. I display them as block elements, it enables you to click wherever in the tab to activate it. Its color is also specified here. The text-decoration : none removes the default underlining of links. Finally, I set an inner margin with padding : 4px.

The final touch: the background color changes when the mouse hovers a tab. Therefore, we must select the a:hover link within #tabs.

Code: CSS
1
2
3
4
#tabs a:hover
{
    background : #fff;
}


:whistle: Phew, we did it !

Ultimately, we should get this (with the background getting white on mouse hover):

User image

Isn't it great? :p

Now, feel free to make your own tab menus! :D

Menu positionning

Then, how do I position this menu in the page?

It's pretty difficult to position it at the wanted place if you don't know the trick. The trick I'm going to show you give the same result with Internet Explorer, Opera, and Firefox.

What's this trick?

It consists in creating a block element containing the tabs and in positionning it in the page.
To do this, the XHTML doesn't change!
In the CSS code, you need to add:

Code: CSS
1
2
3
4
5
#menu
{
        border-bottom : 1px solid #9EA0A1;
        padding-bottom : 25px;
}

This creates the horizontal line across the screen. The padding-bottom places it at 25 pixels from the top of the page.

In the first CSS code, there was #tabs. You need to erase it all. We are going to rewrite it.
That's where we set a border around the tabs. In order not to see it on display, the color is transparent. Like in the first code, we remove the list bullets and we select a font.

We obtain:

Code: CSS
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#tabs
{
        position : absolute;
        border : 1px solid transparent;
        padding : 0;
        font : bold 11px Batang, arial, serif;
        list-style-type : none;
        left : 50%;
        margin-top : 0;
        width : 430px;
}

The padding : 0; is used to remove space between the block and the tabs.
The margin-top : 0; places the tabs on the horizontal line. To enable you centering the tabs, it's important that the block has the menu width. Here, it's 430px, therefore the block width is: width : 430px;.
To center the menu in the page, simply use left : 50%; that places the block at 50% from the left side (in the middle).

It doesn't work!!

Indeed, the left : 50%; places the left side of the block at 50%. To solve it, simply remove the half of the block width to the left margin. Therefore, you need to add in #tabs:

Code: CSS
1
margin-left : -215px; /* half of the width */


The menu is now centered with every browser! ;)

After the reading of this tutorial, menus should be a piece of cake for you. I must tell you that if you want to make drop down menus, it'll be more difficult than the "simple" tab menus we have been doing here :( !

If you have any question or if something isn't clear, contact me!
Author : anonymousguest
Rate and comment this tutorial
Print this tutorial

Change your template | Learn more about NP | Site map | Terms of use | Rules | RSS feed | XHTML 1.0 | CSS 2.0
Powered by Simple IT SARL: Contact us

There is nothing else to read, you have to go up now !

Do you want to be published here? Contact us.

Number of Newbies connected 4 Newbies Connected | SQL requests 7 Requests | Page loading delay 0.2274s (0.0218s)