How to Create a CSS3 Tabbed Navigation

Final Product What You'll Be Creating

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.
  1. "utf-8">  
  2.   
  3.   
  4. "stylesheet" href="styles/themes/jean/darkwash.css" media="screen">  
  5.   
  6.   

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.
  1.   
  2. "container">  
  3.   
  4.   
  5. class="tab-container">  
  6.   
  7. Tabs will go here  
  
  •   


  •   




  •   

  •   


  •   


  •   


  •   


  • “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…
    1. "c1">  
    2. //Next markup will go here  
      
    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…
    1. "c1">  
    2.         "http://webdesign.tutsplus.com/videos/how-to-create-a-css3-tabbed-navigation/#c1" title="First">First   
    3.         //Next Markup will go here  
      
    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…
    1. "c1">  
    2.         "http://webdesign.tutsplus.com/videos/how-to-create-a-css3-tabbed-navigation/#c1" title="First">First   
    3. class="tab-content">    
    4. This is the first tab

        
    5.   
    6. Lorem ipsum dolor sit amet, consectetur adipiscing elit. porttitor est et rhoncus.Nam ipsum leo, ornare ac volutpat suscipit, convallis eu metus ipsum nunc.  
    7.   
    8. Suspendisse at aliquet turpis. Phasellus eu libero euismod leo imperdiet sagittis elementum ut erat. Nam euismod risus at sem consequat vestibulum.   
      
  •   


  • 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.
    1. "c2">  
    2.         "http://webdesign.tutsplus.com/videos/how-to-create-a-css3-tabbed-navigation/#c2" title="Second">Second   
    3. class="tab-content"  
    4. Add as many tabs as you like

        
    5.   
    6. Lorem ipsum dolor sit amet, consectetur adipiscing elit. porttitor est et rhoncus. Nam ipsum leo, ornare ac volutpat suscipit, convallis eu metus. ipsum nunc.  
    7.   
    8. Suspendisse at aliquet turpis.   
    9.   
    10. Lorem ipsum dolor sit amet, consectetur adipiscing elit. porttitor est et rhoncus. Nam ipsum leo, ornare ac volutpat suscipit, convallis eu metus. ipsum nunc.  
      
  •   


  • "c3">  


  •         "http://webdesign.tutsplus.com/videos/how-to-create-a-css3-tabbed-navigation/#c3" title="Third">Third   


  • class="tab-content"  


  • 5 Preset Styles & Color Themes

      


  •   


  • Consectetur adipiscing elit. porttitor est et rhoncus. Nam ipsum leo, ornare ac volutpat suscipit, convallis eu metus. ipsum nunc.  


  •   


  • Suspendisse at aliquet turpis. Phasellus eu libero euismod leo imperdiet sagittis elementum ut erat. Nam euismod risus at sem consequat vestibulum.   


  •   


  •   


  • "c4">  


  •         "http://webdesign.tutsplus.com/videos/how-to-create-a-css3-tabbed-navigation/#c4" title="Fourth">Fourth   


  • class="tab-content"  


  • NO Javascript!  NO Images!

      


  •   


  • Consectetur adipiscing elit. porttitor est et rhoncus. Nam ipsum leo, ornare ac volutpat suscipit, convallis eu metus. ipsum nunc.  


  •   


  • Suspendisse at aliquet turpis. Phasellus eu libero euismod leo imperdiet sagittis elementum ut erat.  


  •   


  •   


  • "c5">  


  • class="tab-content" 


  • 2 Tab Types - Regular and Newsreel

     


  •  


  • If you have an opening tab like this, it should always be last in the list of tabs otherwise it won't show first when the page loads.  


  •   


  • Text sometimes overlaps with hidden tabs in IE7 & 8  


  •   


  •   


  • 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.

    1. body {  
    2.     font-size:13px;  
    3.     font-family:Arial, Helvetica, sans-serif;  
    4.     color:#E7E7E7;  
    5. }  
    6.   
    7. p, .tab-content li, h1, h2, h3{ /* This insures that there's enough space between your paragraphs, headings, etc */  
    8.     margin-bottom: 10px;  
    9. }  
    10.   
    11. a{  
    12.     text-decoration:none;  
    13.     color:#EFD24A;  
    14. }  
    15.   
    16. a:hover{  
    17.     color:#AF9B41;  
    18. }  
    19.   
    20. .tab-container h3{ /* Effects only the h3 headings inside the tabs */  
    21.     font-size:147%;  
    22.     color:#EFDFB3;  
    23. }  

    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%.
    1. #container{ /* Use this to position the entire tab module */  
    2.     margin:0 auto;  
    3.     width:480px;  
    4.     margin-top:4%;  
    5. }  
    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.
    1. .tab-container {  
    2.     position: relative;  
    3.     width: 100%;  
    4.     z-index:0;  
    5. }  
    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.
    1. .tab-container > div {  
    2.     display: inline;  
    3. }  
    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.
    1. .tab-container > div > a {  
    2.     position: relative !important; /* Keeps the tabs in line with each other */  
    3.     text-decoration: none;  
    4.     color: #D7D7D7;  
    5.     display: inline-block;  
    6.     padding: 4px 14px;  
    7.     font-size:15px;  
    8.     font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;  
    9.     font-weight:bold;  
    10.     text-transform:uppercase;  
    11.     margin:2px;  
    12.     background: #4C4648; /* old browsers */  
    13.     background: -moz-linear-gradient(top, #4C4648 0%, #474344 52%, #2F2D2E 100%); /* firefox */  
    14.     background: linear-gradient(top, #4C4648 0%, #474344 52%, #2F2D2E 100%); /* future browsers */  
    15.     -pie-background: linear-gradient(top, #4C4648 0%, #474344 52%, #2F2D2E 100%); /* pie for ie */  
    16.     background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#4C4648), color-stop(52%,#474344), color-stop(100%,#2F2D2E)); /* webkit */  
    17.     border:dashed 2px #CFA840;  
    18.     padding: 4px 14px;  
    19.     -moz-border-radius: 3px;  
    20.     -webkit-border-radius: 0px;  
    21.     border-radius: 3px;  
    22.     text-shadow: 0 -1px 0 #000000;  
    23.     -moz-box-shadow: 0 4px 10px -5px #000000;  
    24.     box-shadow: 0 4px 10px -5px #000000;  
    25.     -webkit-box-shadow: 0 4px 10px -5px #000000;  
    26. }  
    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.
    1. .tab-container > div:target > a {  
    2.     background: none repeat scroll 0 0 #948a81;  
    3.     text-shadow: 0 1px 0 #4C4648;  
    4. }  
    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.
    1. .tab-container > div > div { /* This is the container which holds the tab content */  
    2.     background: #4C4648; /* old browsers */  
    3.     background: -moz-linear-gradient(top, #4C4648 0%, #474344 84%, #2F2D2E 100%); /* firefox */  
    4.     background: linear-gradient(top, #4C4648 0%, #474344 84%, #2F2D2E 100%); /* future browsers */  
    5.     -pie-background: linear-gradient(top, #4C4648 0%, #474344 84%, #2F2D2E 100%); /* pie for ie */  
    6.     background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#4C4648), color-stop(84%,#474344), color-stop(100%,#2F2D2E)); /* webkit */  
    7.     -moz-box-shadow: 0 0 12px 1px #000000 inset;  
    8.     -webkit-box-shadow: 0 0 12px 1px #000000 inset;  
    9.     box-shadow: 0 0 12px 1px #000000 inset;  
    10.     z-index: -2;  
    11.     top: 50px;  
    12.     padding: 20px;  
    13.     border:solid 6px #4C4648;  
    14.     outline: 2px dashed #CFA840;  
    15.     outline-offset: -8px; /*Delete if you don't want an offset*/  
    16.     min-height:250px; /* Change this value if you need more or less content space */  
    17.     position:absolute; /* Fixes IE 7 & 8 */  
    18. }  
    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.
    1. .tab-container > div:target > div {  
    2.     position: absolute;  
    3.     z-index: 3 !important; /* Brings the content to the front depending on the tab that was clicked */  
    4. }  
    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.
    1. div.tab-content{ /* Styles the inner content of the tabs */  
    2.     padding-bottom: 70px;  
    3.     padding-left: 20px;  
    4.     padding-right: 20px;  
    5. }  
    6.   
    7. .tab-content img{  
    8.     margin:0 auto;  
    9.     display:block;  
    10.     padding-bottom: 20px;  
    11.     padding-top: 10px;  
    12. }  
    13.   
    14. /******  This imports other stylesheets so you don't have to call them in an html file *****/  
    15. @import url('../../reset.css');  
    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…
    1. .tab-container > div > div, .tab-container > div > a{behavior: url(../styles/csspie/PIE.htc);} 
     
    How to Create a CSS3 Tabbed Navigation How to Create a CSS3 Tabbed Navigation Reviewed by BloggerSri on 11:03 PM Rating: 5

    No comments:

    Powered by Blogger.