Style Sheets
  Home arrow Style Sheets arrow Tabbed Browsing with CSS2
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Tabbed Browsing with CSS2
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2004-07-28

    Table of Contents:
  • Tabbed Browsing with CSS2
  • The Style
  • Conclusion and All the Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Tabbed Browsing with CSS2


    (Page 1 of 3 )

    So you've created one sweet web application, and you're very proud of the functionality, but the User Interface is somewhat lacking. Hey, don't worry, it happens to all of us. We are programmers after all, not UI designers! But don't despair; this tutorial will show you how to separate the pages of your application into ergonomically distinct chunks, by the use of CSS tabs. That's right, CSS, no tables involved!

    It has become a very established fact that tabs are an easy way to separate pages or different areas of a website. If done properly, they can be very aesthetically pleasing and intuitive to use. You can prevent users from ever having to ask “umm, where's the _______ feature?”, because it will very obviously be one of the tabs. And not only that, but you can transcend the stereotype of a programmer and deliver something that works AND looks good!

    For the sake of this example, I'm going to assume that you have a few different pages here, and they have very basic names, such as 'news', 'photos', and 'users'. I'm going to throw them into a very basic array for simplicity sake. We will use each item of the array to display a distinct tab, to show an icon, and to provide a link. For your own use you can make this as complicated as you want. In one application that I built, I used a multidimensional array for the sake of being able to control access based on privileges, but we don't need all that for the tutorial.

    The Code

    OK, to start off with, let's create the array. I'm using ASP syntax for this; feel free to translate it however you wish:

    <%
    dim arTabs( 5 )
    arTabs( 0 ) = "home"
    arTabs( 1 ) = "events"
    arTabs( 2 ) = "news"
    arTabs( 3 ) = "photos"
    arTabs( 4 ) = "users"
    arTabs( 5 ) = "admin"
    %>

    Now that we have the array, here's what needs to be accomplished. We're going to have one main layer to hold all the tabs. We will not use any tables. Tables are bad. Tables are the enemy. OK, maybe that's a little extreme, but truthfully this is not what tables should be used for, so we won't use them.

    We'll have one layer (div) called navTabs, and loop through the array to output each item into its own little box, which we'll style into a tab!

    Here we go:

    <div id="navTabs">
    <%
    dim theClass, thisPage

    thisPage = Request.ServerVariables("SCRIPT_NAME")
    thisPage = replace( right( thisPage, len( thisPage ) - inStrRev( thisPage, "/" ) ), ".asp", "" )

    This little piece is important. Working from the inside out, it first strips out the filename from after the last “/”. So if the URL is '/webapps/myapp/admin/users.asp', the resulting string is just 'user.asp'. Then we take that, and chop off the file extension, which is '.asp' in this case. The result is now just 'users', which you should recognize as one of the items in the array. That is important, as you'll see in a second.

    for i = 0 to uBound( arTabs )
    if thisPage = arTabs(i) or left( thisPage, len( arTabs(i) ) ) = arTabs(i)

    then
    theClass = "on"

    else
    theClass = "off"

    end if

    This is where the location identification takes place. It's obvious that if the current array item matches the current page, then we should set that tab item to be 'on'. However the second part of the 'if' statement might not make sense to you. It is there so that you can have multiple pages use the same tab. If I have pages that all work on users, I would want them all under the users tab. So I might create a generic users.asp, then maybe a users_password.asp, and perhaps a users_info.asp. As the first portion of the filename matches the array item, they all cause the 'users' tab to be active.

    response.Write("<div class=""tab" & theClass & """ onclick=""javascript:window.location='" & arTabs(i) & ".asp'"">")

    You'll notice I'm not anywhere outputting an anchor link. This is because I want the entire tab to appear as a link. So I set the onclick handler to send the user to the page, and as you'll see in the CSS, I set the cursor to a hand, to simulate a link.

    Response.Write("<img src=""" & arTabs( i ) & ".gif"" align=""absmiddle"" />&nbsp;")

    Response.Write( replace( arTabs(i), "_", " " ) & "</div>" & vbCrLf )

    Here I've inserted an icon to enhance the usability, and then the name of the tab. The icon is just a gif with the same name as the array item, for the sake of simplicity. I didn't include any dimensions to the image, in case I want to quickly swap them down the road, or in case some of them are slightly different. Now we'll finish off the loop and I'll get into the CSS.

    next
    %>
    </div>


    More Style Sheets Articles
    More By Justin Cook


       · Here is the PHP verison of the original ASP code. Note, this version is just as...
       · <?php $arTabs[] = array(); $arTabs[0] = "msds"; $arTabs[1] = "diagrams"; ...
       · The tabs don't display too well in firefox (v0.9). Any idea of a fix?
       · This doesn't work on Safari,IE5 and Firefox 0.8 on Mac OS X
       · The php section should be like this:<?php $arTabs[] = array(); $arTabs[0] =...
       · yes, that is a more efficient way of using basename...shoulda looked it up...the css...
       · This also doesn't work in Mozilla 1.7.1.Has anyone found any solutions to...
       · I have found a solution all, thanks to teh excellent Paul O'Brian. Include the...
       · $thisPage = basename($_SERVER[PHP_SELF], ".php");This line works nicely too. And...
       · Please help. Thanks.
       · How useful, please help you with what? Your hairstyle? Your failed social life?
       · where will i put <a href=""> on every tab...??tnx..
       · You do not use href as the javascript takes care of switching...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek