tags:

views:

73

answers:

2

Hi I've been trying to make my subnav active when on the relevant page and have been trawling forums etc for days/weeks!! I'm quite new to CSS and would appreciate any help! I've managed to make the top nav button active with a#current but the subnav doesn't follow. I know I'm probably missing out something really basic but I just don't get it! I'd like it to work in a similar way to www.forbes.com (saw this through another persons issue thought I was so close as well!!) Any help would be great. Thanks Rebecca

Please see CSS below.....

<style>
ul#topnav {
 margin: 0 ; padding: 0 ;
 float: left;
 width: 955;
 list-style: none;
 position: relative;
 font-size: 12px;
 background: url(images/testbut.jpg) repeat-x;
}
ul#topnav :hover {
 font-family:Arial, Helvetica, sans-serif; 
 font-size:12px; color: #CCCCCC;
}
ul#topnav li {
 float: left;
 margin: 0; padding: 0;
 border-right: 1px solid #ffffff; /*--Divider for each parent level links--*/
}
ul#topnav li a {
 padding: 10px 27.5px;
 display: block;
 color: #f0f0f0;
 background-image: url(images/testbut.jpg) repeat-x;
 text-decoration: none;
 font-family:Arial, Helvetica, sans-serif;
 font-size:12px;
} 
ul#topnav li span {
 float: left;
 padding: 10px 0;
 position: absolute;
 left: 0; top:35px;
 display: none; 
 width: 100%;
 color: #fff;
 /*--Bottom right rounded corner--*/
 -moz-border-radius-bottomright: 5px;
 -khtml-border-radius-bottomright: 5px;
 -webkit-border-bottom-right-radius: 5px;
 /*--Bottom left rounded corner--*/
 -moz-border-radius-bottomleft: 5px;
 -khtml-border-radius-bottomleft: 5px;
 -webkit-border-bottom-left-radius: 5px;
}
ul#topnav li:hover span { display: block; background: #6a6a6a url() repeat-x;} /*--Show subnav on hover--*/
ul#topnav li span a { display: inline;} 
ul#topnav li span a:hover { font-family:Arial, Helvetica, sans-serif; font-size:12px;}
ul#topnav a#current { background: #6a6a6a url() repeat-x;}
</style>
A: 

If you are using static pages then set an active class for your links / list items in the HTML

Example:

index.html

<ul>
<li class="active">Home page</li>
<li>Content Page</li>
<li>Contact Page</li>
</ul>

content.html

<ul>
<li>Home page</li>
<li class="active">Content Page</li>
<li>Contact Page</li>
</ul>

li{color: black;}
.active{color: red;}

If you want the subnav to be active then it will just be a matter of setting the class on the ul as well:

<ul>
   <li>
   <ul class="active">
      <li class="active"></li>
      <li></li>
   </ul>
   </li>
   <li>
   <ul>
      <li class="active"></li>
      <li></li>
   </ul>
   </li>
</ul>

However if you want a dynamic nav, it gets a bit more tricky, but is simple enough to do with some PHP.

danixd
HiThanks for the speedy reply, but my my subnav code is within span tags it's not a nested list...... and the active thing doesn't want to work - or I'm being really dim!Rebecca
Beccs
Your span makes no sense. Firstly, it has no class and secondly it has no closing tag! Have you got an online version of this page?
danixd
Hi, it's not online yet as it doesn't work! When I add a class to it as in <span class="current"> it makes no difference and it's closing tag is </span> isn't that right? Did I mention I'm new to this???!
Beccs
I see you are following the tutorial from : http://www.sohtanaka.com/web-design/horizontal-sub-nav-with-css-jquery/The active CSS class is activated by jquery (hiding and showing). Are you sure you have added the correct jquery in your page?
danixd
The markup isn't great on that tutorial, you would be better off looking at something that doesn't use jquery if you are a beginner.
danixd
+1  A: 

I wasn’t quite sure where to begin with your existing code, so instead, I’ve put together a working sample from scratch. Please note that the supplied example is very basic, and is just one way of achieving the stated goal. I have tested it on Firefox 3.6, and nothing else. There are likely to be cross-browser compatibility issues, especially with the :hover pseudo class in Internet Explorer. The idea behind this code is simply to give you a simple framework from which to work.

I highly recommend that you check out the following articles, as they explain the use of nested lists, the problems with cross-browser compatibility, Javascript solutions to the :hover pseudo class IE problem, and accessibility issues with hiding elements using display: none:

Suckerfish Dropdowns
http://www.alistapart.com/articles/dropdowns

Son of Suckerfish Dropdowns
http://www.htmldog.com/articles/suckerfish/

Hiding with CSS: Problems and solutions
http://www.456bereastreet.com/archive/200905/hiding_with_css_problems_and_solutions/

Okay, onto the code. First, the HTML. Here, I have used nested unordered lists to create the menu structure. This is a good way to go, as a navigation menu is really just a nested list of links. This will display correctly for those that do not use CSS, and will be read correctly by screen readers.

You may wish to look into ways of removing links from the active page. For example, if the active page is Page 1-3, you could replace the a tag with a span tag.

The HTML should be fairly self explanatory. Just give the active menu UL the active class:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="test.css" media="screen" />
<title>Horizontal Menus</title>
</head>

<body>
    <div id="topnav">
        <ul>
            <li>Menu 1
                <ul class="active">
                    <li><a href="#">Page 1-1</a></li>
                    <li><a href="#">Page 1-2</a></li>
                    <li><a href="#">Page 1-3</a></li>
                    <li><a href="#">Page 1-4</a></li>
                </ul>
            </li>

            <li>Menu 2
                <ul>
                    <li><a href="#">Page 2-1</a></li>
                    <li><a href="#">Page 2-2</a></li>
                    <li><a href="#">Page 2-3</a></li>
                    <li><a href="#">Page 2-4</a></li>
                    <li><a href="#">Page 2-5</a></li>
                </ul>
            </li>
        </ul>
    </div>
</body>
</html>

The CSS is full of comments, so it should be fairly simple to understand. I think the hardest part is getting the formatting right, and making the sub-menus horizontal instead of vertical. You’ve managed that bit, so the rest should be quite easy:

/*
 * The contain DIV for the navigation menu.
 * Use this to position the menu on the page.
 */
#topnav {
    /*
     * Set the containing DIV position to
     * relative.  This allows the UL to be
     * positioned relative to this container.
     * See static, relative and absolute, here:
     * http://www.w3.org/TR/CSS2/visuren.html#choose-position
     */
    position: relative;
}

/*
 * All ULs (first and second level).
 */
#topnav ul {
    /*
     * Remove bullets from the UL.
     */
    list-style: none;
    /*
     * Zero margins and padding.
     */
    margin: 0;
    padding: 0;
    /* 
     * Take the UL out of the normal flows so
     * that it can be given absolute positioning
     * coordinates, relative to its containing
     * block (the #topnav DIV).
     */
    position: absolute;
    /*
     * Align the UL with the left of the #topnav DIV.
     */
    left: 0;
    /*
     * The list must be wide enough to enclose all
     * second level list items (5 x 10em).
     */
    width: 50em;
    /*
     * Give the UL height and a background colour.
     * This enables the UL that is activated during a
     * hover to mask the UL that is active.  See
     * the active and hover z-index settings.
     */
    height: 1.5em;
    background: #eeeeee;
}

/*
 * All LIs (first and second level).
 */
#topnav li {
    /*
     * Float the LIs left so that they all
     * appear on one line.
     */
    float: left;
    /*
     * Set the width of each LI.
     */
    width: 10em;
}

/*
 * Second level UL.
 */
#topnav ul ul {
    /*
     * Hide all second level LIs.
     * Set their position to absolute, then
     * move them left by 999em.
     */
    position: absolute;
    left: -999em;
}

/*
 * Second level UL.
 * Selected when first-level LI is hovered over,
 * or when first-level UI has the 'active' class.
 */
#topnav ul li:hover ul, #topnav ul ul.active {
    /*
     * Show active LIs.
     * Reset the left position to zero.
     */
    left: 0;
}

/*
 * Second level UL.
 * Selected when first-level LI is hovered over.
 */
#topnav ul li:hover ul {
    /*
     * Set the stacking level (z-index) so that it is
     * above the active UL.
     */ 
    z-index: 200;
    background: #cccccc;
}

/*
 * Second level UL.
 * Selected when first-level UI has the 'active' class.
 */
#topnav ul ul.active {
    /*
     * Set the stacking level (z-index) so that it is
     * below the UL that is displayed during hover.
     */ 
    z-index: 100;
    background: #aaaaaa;
}

Good luck!

Mike
Hi Mike, I'm going to give it a go, I'll let you know how I get on!! Thank you so much for taking the time to help me out I really appreciate it.
Beccs
Hi Mike, I've done it!! Thank you so much again you have made my life so much easier and I think I'm actually starting to understand it all!! Hopefully it can all go live in the next few weeks! Thanks again.
Beccs
@Beccs: I'm happy to have been of assistance, and I'm glad you've got it sorted. Layout with CSS can be tricky at the best of times, but when you add in browser compatibility issues and fancy tricks like hiding and displaying menus, it can be a nightmare. However, if you stick to W3C compliant code, and follow the good web sites like [A List Apart](http://www.alistapart.com/ "A List Apart") and [456 Berea Street](http://www.456bereastreet.com/ "456 Berea Street"), to name but two, it will hopefully become a more pleasurable pursuit.
Mike