CSharpZealot.com

.Net the C# way
Welcome to

CSharpZealot.com

Sign in | Join | Help
in Search
Sahil Malik Sharepoint 2007 Video Training DVD

Pure CSS Tabs with ASP.NET 2.0 Master Pages

Last post 03-22-2007, 9:04 PM by JamesGreen. 4 replies.
Sort Posts: Previous Next
  •  03-10-2007, 8:40 AM 15

    Pure CSS Tabs with ASP.NET 2.0 Master Pages

    Not strictly C# related, but I just posted this one on my blog and it caused me serious head scratching so hopefully by sharing it it will save someone else some bother.

    -- From: http://deepdark.net/PermaLink,guid,0eaa175f-2819-4d06-9820-38fe39e82fb5.aspx 

     

    So I came across a problem using a old CSS trick to make a tab strip/menu bar on a site from a Unordered List inside a Div

    To create the tabs the trick is you can use some CSS like this:

    #nav

       float:left

       margin: 0

       padding: 10px 0 0 46px

       list-style: none
    ;
    }

    #nav
    li


       font-size: 150%

       float: left

       margin:0

       padding: 0
    ;
    }

    #nav
    a


       float: left

       display: block

       margin: 0 1px 0 0

       padding: 4px 8px

       color: #333

       text-decoration: none

       border: 1px solid #949494

       border-bottom: none

       background: #686868

       color: #949494
    ;
    }

    ...to turn a <UL> with the ID="nav" into some tabs.  For example we add the following to the master page:

    <ul id="nav" > 
       <li id="navHome"><a href="default.aspx">Home</a></li> 
       <li id="navProducts"><a href="products.aspx">Products</a></li> 
       <!-- ...and so on... -->
    </ul>

     

    OK so far.  The thing with tabs is you always want the tab of the current page to glow a little or something to indicate what page the user is up to.

    A common way is to assign an ID attribute to the body tag of each page and include some CSS that looks at the body ID and overrides the #nav a  for the current page.

    The trick comes when the pages all descend from an ASP.NET 2.0 Master Page - the body tags are going to be identical on each page.

    As luck would have it, CSS lets us select based on any attribute.  Once the page is rendered each page will get its own form tag where the Action attribute represents the current loaded page as so:  

    <form name="aspnetForm" method="post" action="Default.aspx" id="aspnetForm">

    Which would let us add the following to our stylesheet:

    #nav a:hover, body form[action="Default.aspx"] #NavPlaceHolder #nav #navHome a
    {
       background: #ffffff
    ;
    }

    OK So far but the problem is it is case sensitve!  We would need - If the page loads as default.aspx our tab won't highlight.  This just won't do!

    The trick is to add a new ContentPlaceHolder in our master page straight above the UL listed above

    <asp:ContentPlaceHolder runat="server" id="pageIdPlaceHolder" ></asp:ContentPlaceHolder>

    Each page that descends from our master page should then override that ContentPlaceHolder to add an empty Span with a page-specific ID as so:

    <asp:Content ID="pageIdContent" ContentPlaceHolderID="pageIdPlaceHolder" runat="server" >
       <
    span id="ProductsPage"></span> <!-- This page will show the Products tab as selected -->
    </
    asp:Content>

     

    and then we can add a rule to our CSS for each page that would read:  Whenever there is an element with the ID="ProductsPage" right next to one with the ID="nav" do the following.  The + operator selects the next adjacent node in the document as so:

    #ProductsPage + #nav #navProducts a
    {
       background: #ffffff;
    }

    And there you have it!  Accessable and maintainable nav bars from a UL using CSS, plus keeping all the time saving layout features of Master Pages.


    James Green
    MCSD, MCAD

    Blog: http://deepdark.net/
    Filed under:
  •  03-14-2007, 1:13 AM 17 in reply to 15

    Re: Pure CSS Tabs with ASP.NET 2.0 Master Pages

    Hey James....that's neat...have you tested it on IE7/Vista?

    i've had some problems with CSS hacks on IE7 coupled with Vista..but that could simply be the display theme that's overwriting some of the more basic stuff.

    nice work tho...interesting to see how you can manipulate the UI once you put your mind to it.


    Regards
    Brian H. Madsen - CSharpZealot
    Microsoft MVP Visual Developer ASP/ASP.Net

    blog: http://www.msmvps.com/blogs/brianmadsen
    site: http://www.csharpzealot.com

    Microsoft SQL Server UG Perth - President
    http://www.sqlserver.org.au
  •  03-14-2007, 10:02 PM 19 in reply to 17

    Re: Pure CSS Tabs with ASP.NET 2.0 Master Pages

    Thanks Brian :-) 

    I was on a Vista/IE7 box when I was putting this together, so I think I need to be testing it on XP ;-)

    Also on the testing front I understand that an XP VPC on Vista is a better answer than a Vista VPC on XP, due to the way VPC emulates a pretty bare bones vid card to the guest OS.  I've been a bit slack in setting up tetsing VPCs since moving to vista :-(

    You're right in what you say that you don't have to go too far down the CSS road before "hack management" becomes an important consideration!

    Cheers

    James
     


    James Green
    MCSD, MCAD

    Blog: http://deepdark.net/
  •  03-22-2007, 1:56 AM 32 in reply to 19

    Re: Pure CSS Tabs with ASP.NET 2.0 Master Pages

    We are just about to do the tab thing, so this will come in handy...

    Thanks

    TooCrazy


    mr TooCrazy
  •  03-22-2007, 9:04 PM 33 in reply to 32

    Re: Pure CSS Tabs with ASP.NET 2.0 Master Pages

    No worries :-)

    There was an alternate solution using the CSS Friendly Control Adaptor toolkit for ASPNet 2.0 which renders the standard menu control as UL\LI tags rather than as tables

    I chose to not go that route because I don't need the tabs to link to a sitemap datasource or anything cool like that - UL\LI in a master page was plenty good enough for me, however if you did want/need to link the same code to a sitemap then the CSS Friendly adaptors would be well worth looking into.

     James


    James Green
    MCSD, MCAD

    Blog: http://deepdark.net/
    Filed under:
View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems