Advertisement
  1. Web Design
  2. Site Elements
Webdesign

How to Create a CSS3 Tabbed Navigation

by
Languages:

Hiya folks! Today we're going to have fun creating a CSS3 Tabbed Navigation. In this tut we're going to be creating the darkwash jean version out of the many different themes available for these awesome tabs just because... #1 we can and #2, there is more styling involved and I can show you more neat tricks with CSS3 than with the other styles. If you prefer, there are much simpler themes in the source files so feel free to follow along with those as the structure is pretty much the same.

Note: Support for IE7 & 8 is there but limited in that the functionality works perfectly but some of the overlay styles aren't treated properly. If not for the wonderful and awesome CSSPIE and Dean Edwards fix, there would be no support for any version other than IE9.

Ok, now that that's out of the way, I think it's time we got started!


The Video Tutorial

We're offering this tutorial in two different formats: A video as well as a full written tutorial below. Follow along with whichever method of learning you prefer (or both!):


The Written Tutorial

The step by step written tutorial is below. Be sure to grab the full downloadable source as well!


Step 1 Creating the Markup

The Head

For our first bit of markup, we're going to create our head using HTML5 doctype, inserting our stylesheets and telling IE 7 & 8 that's it's ok to use CSS3.

Building the Containers

After you've added the head, in-between the body tags we need to create some containers for the tabbed navigation. Let's break it down...

As you can see, the first container is id="container". This is used to create a full width of the tabbed navigation and to position it however you like.

Last is the class="tab-container" which is the wrapper for the actual tabs themselves as well as it helps contain the content block. This is a very important class and there's no need to edit it, although I'll explain the styling later on in this tutorial.

"Last is the class="tab-container". This is a very important class and there's no need to edit it"

Creating your first Tab

The last markup we'll want to write is for our actual tabs and the content inside them.

First of all we want to create a div with an id so that we can know which content to show when clicking on the corresponding tab. That looks like this...

Then we will create the tab for that content id and tell it to show the div with an id of c1 by using a hash with c1 after it in the link. So now your markup should look like this...

Now let's create a place to put our content. To do this we'll simply create a div with a class of .tab-content and insert some dummy text, making your markup now look like this...

Let's Finish the Markup

Just add the following below your first tab (ie: c1)

And that's it. Very little markup for an awesome tabbed navigation. Here's the rest of the tabs which all follow suit with the markup you just created! Just add the following below your first tab (ie: c1)...

Note: The last tab you add will always be a hidden tab and the content will show up first so make it something like an introduction.

Your tabbed navigation should look something like this. Looks nice in it's basic form but let's add some styling and get this baby working!


Step 2 Digging into some CSS

Now that we have the markup all taken care of, you're feeling like a winner right about now! Let's take it one step further and build up that confidence by applying some awesome CSS to the markup you've just created... then you'll really have something to brag about!

Global Tags

Ok so for the purposes of this tutorial, we're using a standalone tabbed navigation, so let's add some global styles such as for the body, p tags, heading, etc.

When implementing the tabs, you may already have these styles in your stylesheet. In that case you can either delete these styles or taget the .tab-container to create some tab specific styling so not to overwrite your websites own styles.

Styling the Containers

Now we are going to style our two containers. For this tutorial we are going to center that main container using margin: 0 auto, give it a width of 480px and push it down from the top about 4%.

For the .tab-container, it is important to add a position of relative for each tab content block to overlap each other correctly in a nice and contained space. We want our width match the width of our main container 100% and a z-index of 0 which will help keep IE from showing the text of the content block behind the current one.

At this point you should have a working tabbed navigation. It doesn't look pretty yet but hey... it's working! My apologies if it's hard to see.

Styling the Tabs and their Content

Ok, so since we have working tabs, the first think we want to do is line them up horizontally. To do that we'll simply add a display:inline to the first div of each tab.

So now we need to add some default styles to the tabs themselves. We're going to add a postition of relative which is important for IE. We'll also put a display of inline-block so to align them properly with the tab content. By putting a margin of 2px, we can add some space around each tab to make it cleaner. We're also going to add a gradient background for each browser along with a border radius and box shadow for some extra awesomeness! Since we are using CSSPIE again for IE 7 & 8, there is no need for nasty filters in the gradients.

Next on the list, we want to let the user know which tab they're on. So to do that we will use the target selector and use Dean Edwards IE9.js to make IE 7 & 8 recognize it.

So .tab-container > div:target > says that when the first div (ie: div id="c1", etc) is selected or is the target then we will change what the button (ie: a) inside of that div will look like.

Now that we've styled the tab buttons, let's go ahead and add some character to the content area.

What we're going to do here is target the second div within the .tab-container which is div class="tab-content" and give it a min-height of 250px which is important so that all the content divs behind the target div have no overlap. So since we have the content with the correct height, we still need to hide the inactive content div behind the target one, and to do that we'll simply add a z-index of -2. Also we want IE 7 & 8 to play nice so we'll add a position of absolute. So that's the important stuff and the rest of the code is there to add some extra styling and give us a nice darkwash jean look.

After the initial content divs have been styled, we'll want to bring the target one to the front. To do this we simply use the target selector with a z-index of 3 and make it important.

Last thing we'll do is add some padding to the content and some default styling if you'd like to add an image inside your tabs.

On a side note, don't forget to import your reset.css file at the top of the stylesheet, we need this to override browser defaults.

After all that has been finished, you should end up with something like the following image. If yours doesn't look the same or similar then either you missed something or decided to go with your own styling, just go back and recheck your work, I'm sure it's only something minor.

One More Thing...

We want to tell IE7 & 8 that it's ok to use box shadows, rounded corners, etc. So to that we simply add the elements that are in our stylesheet with CSS3 features to our ie.css file, then we use the behavior for CSSPIE like so...

Great Job... You're Finished!

If you've gotten this far, then that means your all done so... congratulations! Hopefully you enjoyed this tutorial as much as I enjoyed writing it. It's always a pleasure teaching you guys new tips and tricks you may not already know. With that said, good work, be patient and keep soaking up as much knowledge as you can and before you know it you'll be a master at whatever you do!

Leave your comments and questions below ;)

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.