Placed in: Home arrow Programming arrow CSS arrow Sweet tabbed navigation using CSS3
Sweet tabbed navigation using CSS3

Although I don't understand why animations have been added in CSS3, this upcoming standard does have a couple of very neat features added to the CSS we're using today. I wanted to take a couple of these new things, and create a Sweet tabbed navigation using CSS3.

Sweet tabbed navigation using CSS3

This tutorial takes on the following CSS(3) properties:

  • rgba
  • opacity
  • text-shadow
  • pseudo selectors
  • rounded corners
  • gradients
  • box-shadow

Demo tabbed navigation using CSS3   Download tabbed navigation using CSS3

As you could expect, this demo only works in browsers that support the new CSS3 features. This code fully works in Safari / Firefox 3.6, but hasn't been tested in other browsers. Now let's see how you can create this kind of sweet menu yourself!

HTML

Before we do anything fancy with CSS, we'll need a decent backbone in the form of HTML. As you can expect, the menu excists from an ul with items (links to pages). We'll also add a main content division, but this tutorial has the focus on the menu.

 
<div id="menu">
   <ul>
      <li><a href="#" title="Home">Home</a></li>
      <li><a href="#" title="Blog" class="active">Blog</a></li>
      <li><a href="#" title="About">About</a></li>
      <li><a href="#" title="Register">Register</a></li>
      <li><a href="#" title="Contact">Contact</a></li>
   </ul>
</div>
<div id="main">
   <h3>Main content header</h3>
   <p>Main content</p>
</div>

Nothing really fancy or hard going on here, so I suggest we take our first steps into the CSS.

Simple CSS

First, we'll need some basic styling to the HTML we just created. We want to display the navigation inline and see no bullets what so ever. This is the code we need:

 
#menu { margin:40px 0 0 150px; }
#menu ul { list-style:none; }
#menu ul li { display:inline; float:left; margin-bottom:20px; }

Our outcome (see below) might look a little bit dull, these are the foundations we're going to work with in order to achieve what we want.

Step 1

RGBA, opacity and Text-shadow

Now, we're going to use some of the CSS3 features to make this simple navigation look beautiful. We'll select the anchor tags within the menu, and apply some styling to it. CSS3 supports RGBA() to select a colour. The fourth parameter sets the opacity of the element. We'll also use text-shadow to give the text an "outer glow" effect.

 
/* background color set to RGBA, with opacity on 0.3 and also using text-shadow */
#menu ul li a { padding:20px; background: rgba(255,138,30,0.3); text-decoration: none; font: bold 14px Helvetica, Sans-Serif; letter-spacing: -1px; color: #402e16;
    text-shadow: #eee 0px 0px 2px; }

We now have already something very fancy looking:

Step 2

Looks pretty nifty already, doesn't it? But we're not done yet - on to the next step!

Pseudo Selectors and Rounded Corners

Now that we have our navigation, we'll take it to the next level. The first anchor tag will have a rounded top-left corner, while the last one will have a rounded top-right corner. This will enhance the effect of tabs a little bit more.

With the use of pseudo selectors, we can easily find the first and last anchor element in the list, without the need of extra classes. Check out the following code:

 
/* :first-child pseudo selector with rounded top left corner */
#menu ul li:first-child a { -moz-border-radius-topleft: 12px; -webkit-border-top-left-radius:12px; }
 
/* :last-child pseudo selector with rounded top right corner */
#menu ul li:last-child a { -moz-border-radius-topright: 12px; -webkit-border-top-right-radius:12px; }

When applied, the menu will look like this:

Step 3

Whoah - looking better every step! But don't stop here, we'll add some more beauty to this menu in the next step.

Gradient and Box-shadow

Now that we have our menu fully complete, we want to place the cherry on the cake by styling the .active and :hover state of the menu. The first one is created to indicate the user is currently viewing that tab (added by the class in the HTML). The second one is the hover state.

The .active state isn't very spectecular: It'll have the same RGBA colour as the normal state, but with less opacity (0.8). The :hover state will be a little bit more complex.

With CSS3, you'll have the ability to create gradients. In this example, I'll go with a simple linear, two coloured gradient, but the possibilities are endless. You can use this great CSS3 gradients tool (just like I did) to help you a little bit out creating the CSS gradients. We'll also add a slight grey shadow on top, to enhance the 3d tab effect.

 
/* hover state shows a linear gradient and opacity it brought down to 0.9 and also shows a very slight grey shadow on top */
#menu ul li a:hover { -moz-box-shadow: 0 -5px 10px #777; -webkit-box-shadow: 0 -5px 10px #777;
   background: -webkit-gradient(
      linear, right bottom, left top, color-stop(0, rgb(237,227,112)), color-stop(0.72, rgb(255,173,10))) !important;
   background: -moz-linear-gradient(
      right bottom, rgb(237,227,112) 0%, rgb(255,173,10) 72%) !important;
   background-color:rgb(255,173,10) !important;
   -moz-opacity:.90; filter:alpha(opacity=90); opacity:.90; }
 
/* another RGBA background, now with an opacity of 0.8 */
#menu ul li a.active { background: rgba(255,138,30,0.8) !important; }

With all that CSS, our menu looks like this (one is active, the other one has the hover effect):

Step 4

How about that for a sweet CSS3 powered menu!

More CSS

Since we finished our CSS3 powered menu, we can style our main part a little bit. I don't need to explain any of the code here, since all effects applied are used in the menu too!

 
/* main contents with RGBA background (same colour as active tab) and three rounded corners */
#main { clear:both; background: rgba(255,138,30,0.8); width:500px; margin-left:150px;
   -moz-border-radius-topright: 12px; -moz-border-radius-bottomright: 12px; -moz-border-radius-bottomleft: 12px;
   -webkit-border-top-right-radius:12px; -webkit-border-bottom-right-radius:12px; -webkit-border-bottom-left-radius:12px;}
   
/* header with a text-shadow */
#main h3 { text-transform:uppercase; padding:20px 0 0 20px; color:#eee; text-shadow: #000 0px 0px 2px; }
#main p { padding-bottom:20px; color:#ddd; }

With that, our final page looks like this:

Step 5

And we're done!

Conclusion and Download

I added a little jQuery script to virtually switch between pages, but of course this needs to be done server-side and show the actual page. I think CSS3 has some very neat features, where some of them are used in a great way in this tutorial. Sadly, since this isn't cross-browser compatible, we'll need to wait for a while before we can use this in the public.

Demo tabbed navigation using CSS3   Download tabbed navigation using CSS3

What do you think? Do you see any room for improvement in this code? Do you like it? Feel free to share your two cents!


Tags:  tabs navigation css3 webdesign css

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!StumbleUpon!
Comments
Add NewSearchRSS
Magne Andersson   2009-12-07 13:25:31
Gravatar image I've used several of these features on sites, and I think they are a great addition to CSS.

"Although I don't understand why animations have been added in CSS3, this upcoming standard does have a couple of very neat features added to the CSS we're using today."

It's presentational, easy to use, reduce load time heavily and doesn't require you to learn JavaScript. It's a brilliant addition to CSS3.
Marco - My two cents   2009-12-07 15:28:51
Gravatar image Thanks for your comment Magne! I personally don't think Animation is presentational, but behavioural. Feel free to check out my full opinion in my previous article (and share your opinion with others).
Anonymous   2013-03-06 23:41:03
Gravatar image STUPID! NOT USEFULL!
deb   2009-12-07 15:13:59
Gravatar image I was wondering where is the gradient & then noticed my firefox is one version older . Looks very promising on Chrome/Safari.
Marco - Firefox 3.6   2009-12-07 15:32:01
Gravatar image Hi Deb! Thank you for your comment! Firefox 3.6 hasn't been officially released yet (but probably will soon) and I'm personally using 3.5 too (which doesn't show the gradient).

I didn't test this script on Chrome, but since it runs on the safe engine (-webkit) as Safari, I could have expect that it runs fine there too. Thanks for testing!
Josh   2009-12-10 03:28:25
Gravatar image I love it. The pseudo selectors are the best part.
BEBEN - Prodigy of Head   2009-12-10 18:38:04
Gravatar image so smooth and elegant B)
Jon Crim   2009-12-10 20:17:46
Gravatar image I think it looks great, can't wait till it's cross browser compatible!
fractalfrog   2009-12-12 11:05:02
Gravatar image Cross browser CSS3 compatibilty? Bwahahahahaha!
Don't hold your breath for at least the next 5 years :-(
Huan Le - How to add more content?   2010-02-22 17:20:33
Gravatar image How to add more content for Home - Blog - About.... because in demo only one content for all. I tried but not done...
Please help me ?
Brad - Nice example   2010-03-19 14:13:50
Gravatar image This is a great example of CSS3. I just think you could have gone further and made it degrade properly into older browsers.
Robert Visser - history, bookmarkable tabs   2010-05-08 17:47:51
Gravatar image Hi.

Thanks for posting.

Impressed with the simplicity of the script.js. Wondering what's necessary to add the capability to bookmark individual tabs and history. For example, using the tab names in the demo, #home, #blog, #about, #register, and #contact would be added to the url.

Thanks.
Pady   2010-11-25 06:53:16
Gravatar image It's not working in IE :(
Sean Erdrich - Layout Artist   2010-12-13 06:19:29
Gravatar image Hi,

I'm running into an issue with this where the #menu and #main aren't aligning properly. I've double checked my CSS, and the margin for the left on both is set to 150px. It looks like the menu is for some reason scooting to the right an extra 30 to 40 pixels.

Any help would be great!

Thanks,

Sean
RealName - How to add more content?   2011-01-23 11:45:09
Gravatar image I have the same question like Huan Le: How can I add more content?
Frki   2011-02-01 19:38:01
Gravatar image I have same question like Realname!
DavidKnowles   2011-03-19 13:55:43
Gravatar image Tutorial is useless unless you explain how to add new content to the thing.

Other wise it simply a dead end. A I have spent the last copy of hours trying to figure it out my self.
sawebdesigns - IE prob   2011-04-25 05:39:16
Gravatar image How come this is not working on ie :?:
casino bancontact   2011-05-12 06:27:01
Gravatar image It is very sweet for sure the use of navigation bar. why does this not work on IE?
Wypromowa - Thanks!   2011-06-01 09:33:26
Gravatar image Excellent tutorial, even people who see css3 fot the first time, but have experiences with older versions of css, will have absolutelly no problem with including the script to their html/css code. Thanks fot sharing!
kitsune - Content   2011-06-06 15:44:23
Gravatar image He did mention in the article that he used jQuery to switch the content virtually on each page if your looking for that...

As for IE compatibility... it's CSS3. To put it quite frankly, IE has a hard time keeping up with any browser trends. I'd recommend switching to Firefox or Google Chrome.
top pharmacy reviews - top pharmacy reviews   2011-06-14 07:02:11
Gravatar image I had found lots of information from this site. Thanks a lot for the information and I had liked it very much. It is very helpful for me. Thanks a lot and hope to see more stuff.
herbal anti smoking pills guid - herbal anti smoking pills guide   2011-06-14 07:02:52
Gravatar image This was really a great site which I had found it to be very cordial and I really liked it very much. Thanks a lot.
hair loss diet pills informati - hair loss diet pills information   2011-06-14 07:03:34
Gravatar image The information which I had found in this blog was really very cordial and I had liked it very much. Thanks a lot for the information.
diabetiedtes pills journal - diabetiedtes pills journal   2011-06-14 07:04:12
Gravatar image This site was really very interesting and I had got impressed with this stuff. Thanks a lot for the lovely information which was given in this blog.
birth control pills news - birth control pills news   2011-06-14 07:05:12
Gravatar image I had really enjoyed viewing this blog. The information was really very cordial and it was very helpful for me. Thanks a lot for the stuff.
shop best birth control pills - shop best birth control pills   2011-06-14 07:05:51
Gravatar image I am very glad to post a comment on your blog as I really enjoyed reading your posts.
drugs pharmacy journal - drugs pharmacy journal   2011-06-14 07:06:29
Gravatar image The information provided was extremely useful and informative. Thanks a lot for useful stuff.
best deals pharmacy reviews - best deals pharmacy reviews   2011-06-14 07:07:27
Gravatar image This article is so interesting I am completely engrossed. Really I appreciate the efforts you take for making these posts. Thanks for sharing with us such useful information.
better drug store - better drug store   2011-06-14 07:08:21
Gravatar image I really enjoy reading your well written articles. I think you spend numerous effort and time in posting the blog. I have bookmarked it and I am looking ahead to reading new articles. Keep up the good articles!
best peoples pharmacy hub - best peoples pharmacy hub   2011-06-14 07:10:12
Gravatar image You are awesome people and bring great info; I definitely look forward for your work as I am a regular visitor of your site. Thanks a lot, superb work!!!!!
online safe weight loss contra - online safe weight loss contracts   2011-06-14 07:10:45
Gravatar image I am strongly associated with this site. As this site has inspired me a lot always in a new way and made my work easy by every time highlighting on the new issue and make me pleased. Thanks you people rock!!!!!!
hair loss comic treatment - hair loss comic treatment   2011-06-14 07:14:34
Gravatar image It was really surprising to see such a wonderful post that is inspiring and informative and caught the attention of many people. I am a regular visitor of the blog and love the work of these people.
cheap peoples pharmacy spot on - cheap peoples pharmacy spot online   2011-06-14 07:15:08
Gravatar image This is a wonderful website that has great info and is helpful for one and all. I always look forward for your website to gather any kind of information. Hope you people do like this only wonderful job. Cheers
madhav - Great Job   2011-06-24 13:25:36
Gravatar image Great job for implementing such an experimental css and publishing, i had a great experience in this site to collect any information over here. For most of the information i would like to preferred to be here.
hirmani80 - Web Hosting Pad Reviews   2011-08-02 07:44:09
Gravatar image This is providing the nice info in this blog and the great services in this blog. I am very much satisfied by the info in this blog. Thanks a lot for using the great technology is visible in this blog.
hirmani80 - Web Hosting Pad   2011-08-02 09:11:38
Gravatar image This is very good articles are providing in this website that to helpful info in this blog. I am very much satisfied by the info in this blog. It was really very happy for display the nice info in this blog. This is very much impressing with this info and helpful info in this blog. Thank a lot for visiting the nice info in this blog and using the nice services in this blog
Giancarlo - comment   2011-08-12 03:17:18
Gravatar image You should add this in your code if you want border-radius to work on IE and Opera :
border-top-right-radius:12px;
border-top-left-radius:12px;

inside ul li:first-child a and ul li:last-child a

If I'm not wrong, this is the right way to do it (according to www.w3.org)
Norja - Adding Content? How to relate to the list items?   2011-08-22 23:06:54
Gravatar image I have the same question like Huan Le: How can I add more content?
PhotoshopWarrior - Great   2011-09-24 07:49:35
Gravatar image The problem is how to add more content? :dry:
Johannes - thanks!   2011-10-31 19:43:38
Gravatar image works great for me, exactly what I was looking for!
Johannes   2011-10-31 19:44:35
Gravatar image works great for me, exactly what I was looking for!
Weijo   2011-12-08 11:39:49
Gravatar image I can't get the links to work inside the list, they just don't do anything when you add a link inside them???? Am I doing something wrong here? I have tried making a normal ul inside the same projects and that works fine but with this code it doesn't work??
Weijo   2011-12-08 11:45:52
Gravatar image It seems to be something with the javascript, when I take it away the links will work fine....
Web Design Nepal - Web Design Nepal, WEb Development Nepal   2012-02-02 12:30:43
Gravatar image It's really cool. I like it. I am hoping all can work well in all browser, Appreciate for your great post.
Web Design Nepal   2012-02-02 12:32:00
Gravatar image It seems to be something with the javascript, when I take it away the links will work fine....
Anonymous   2012-04-13 05:37:15
Gravatar image Works fine for me, just what I was looking after!
fanta   2012-06-08 07:51:57
Gravatar image How to add more content???????
fanta   2012-06-08 07:52:07
Gravatar image How to add more content???????
fanta   2012-06-08 07:52:34
Gravatar image How to add more content???????
maybe someone will see that question now
Leo   2012-06-20 23:06:29
Gravatar image Take a look at what I've written, hope it helps :)
Leo   2012-06-20 23:07:01
Gravatar image please read what I just wrote now, hope it makes sense :)
Leo - how to add content   2012-06-20 23:05:40
Gravatar image after a lot of looking around I found out how you can add content in different divs, and then call those...

First in the body:




tab1
tab2





TAB 1
First tab shows up here.


TAB 2
Second tab comes in here




Here we simply have two (or however many tabs you have) divs, each with the same class, as well as an id which is equal to the title of the corresponding anchor (a tag, the link). Also, all of the divs except for the first one should be invisble by styling it to display:none;

Then the JQuery in head:


$(function (){

$("a.tab";).click(function() //onclick
{
$(".active";).removeClass('active'); // removes the class 'active'
$(this).addClass('active'); // adds class 'active' to the link
$(".divtab";).slideUp(); //slides upp content of both tabs

var tab_show = $(this).attr("title";);
$("#"+tab_show).slideDown(); // slide down the content stored in tab_show
});
});


Here, we first choose to take away the active class from the former tab, and then add it to the new tab which is opened. We then slide up the content of all tabs, and then slide down the correct div.

The only problem that I've noticed is that the text jumps for about half a second when switching tabs, although this could probably be fixed by using an alternative to SlideUp. However, I haven't done that yet, as I just fixed this, and it's already late at night, just wanted to tell you guys how to do!
Leo   2012-06-20 23:10:08
Gravatar image body messed up o.O

Code:



tab1
tab2





TAB 1
First tab shows up here.


TAB 2
Second tab comes in here


Leo   2012-06-20 23:10:47
Gravatar image AGAIN?!

Quote:



tab1
tab2





TAB 1
First tab shows up here.


TAB 2
Second tab comes in here


tour in nepal - tour in nepal   2012-06-25 10:37:58
Gravatar image it is really look like ...............
Ramzi - Thanks Sir   2012-09-30 04:46:39
Gravatar image I like this css menu
thank you, maybe I can use for my blog
always success :)
Ramzi - Ask..   2012-09-30 04:49:15
Gravatar image I want to ask how to modify the menu at the top so that when clicking on the menu link leads directly to the page each menu dah does not appear on a page as in the example
thanks :cheer:
Ali   2012-10-17 23:50:25
Gravatar image This is really a very elegant
navigopresse - navigopresse   2012-10-23 15:14:59
Gravatar image works great for me, exactly what I was looking for!
Zahnarzt Salzgitter - Thanks   2013-02-21 14:45:02
Gravatar image Great css3 Menu, thanks! Exactly what i was looking for!
Zahnarzt Salzgitter   2013-02-21 14:45:54
Gravatar image Great css3 Menu, thanks! Exactly what i was looking for!
Greetz from Germany!
Lebron - How to add more content?   2013-02-26 14:00:46
Gravatar image Can someone please post the code of how they were able to add different info for each tab. The content stays the same unfortunately.
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Security Image
Please input the anti-spam code that you can read in the image.
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio