Reply LinkBack Thread Tools Search this Thread
Old 27-02-2008, 05:43   #1 (permalink)
hotchilidamo
Registered User
 
Join Date: Feb 2008
Posts: 8
problems using li class="active"

I have a sub nav using list items and I am using li class="active" to show which page the user is currently on. This is working fine until I use it for the top level pages where the active carries over to the sub items within that section. So in the example below, Sub Nav 3 is highlighted along with all the Sub Sub Navs. I need to find a way to just highlight SubNav3 as active. Any ideas?

Here's a snippet of the code:

<ul>
<li><a href="subnav1.html" title="subnav1">SubNav1</a></li>
<li><a href="subnav2.html" title="subnav2">SubNav2</a></li>
<li class="active"><a href="subnav3.html" title="subnav3">SubNav3</a></li>
<ul>
<li><a href="subsubnav1.html" title="subsubnav1">SubSubNav1</a></li>
<li><a href="subsubnav2.html" title="subsubnav2">SubSubNav2</a></li>
<li><a href="subsubnav3.html" title="subsubnav3">SubSubNav3</a></li>
</ul>
<li><a href="subnav4.html" title="subnav4">SubNav4</a></li>

I'm new to CSS so could be missing something obvious.

It works fine in Firefox by the way, just not in IE
  Reply With Quote
Old 27-02-2008, 05:54   #2 (permalink)
88mph
Great Scott!!
 
88mph's Avatar
 
Join Date: Feb 2008
Posts: 223
just to clarify, you are looking for a method to use CSS to change the display properties to the nav element related to the page that is currently active?

For example, if the user is on the Home page, the 'Home' button in the navigation is styled differently?
  Reply With Quote
Old 27-02-2008, 06:31   #3 (permalink)
herkalees
Semantics, yay.
 
herkalees's Avatar
 
Join Date: Nov 2005
Location: Salem, Massachusetts
Posts: 1,093
You're falling victim to the power of the cascade. What you need to do is add just a little bit more CSS. This is because you've got some special 'active' styling applied to a <li> that contains its own child <li>s. Any rules applied to a parent automagically become applied to its children.

So, if you have CSS, for example, like this:

li.active {font-weight:bold;} /* This targets the parent element and all children inside of it */

and you don't want it to apply to the <li>s inside of it, you add this to your CSS:

HTML Code:
/* This targets the parent element and all children inside of it */ li.active {font-weight:bold;} /* This targets the children of any li with a class of 'active' */ li.active ul li {font-weight:normal;}
__________________
  Reply With Quote
Old 27-02-2008, 06:51   #4 (permalink)
hotchilidamo
Registered User
 
Join Date: Feb 2008
Posts: 8
Quote:
Originally Posted by herkalees
You're falling victim to the power of the cascade.

Indeed I am!

Ok, so I tried as you suggested and you have kindly solved the problem. Only snag is, the children are now no longer activating on rollover. Here's the code:

div#s11 ul li a:hover{
color: #474747;
background-color: #DFF0DB;
}

div#s11 li.active a {
color: #474747;
background-color: #DFF0DB;
}

div#s11 li.active ul li a{
background-color: #F2F2F2;
}
  Reply With Quote
Old 27-02-2008, 06:58   #5 (permalink)
herkalees
Semantics, yay.
 
herkalees's Avatar
 
Join Date: Nov 2005
Location: Salem, Massachusetts
Posts: 1,093
Quote:
Originally Posted by hotchilidamo
Indeed I am!

Ok, so I tried as you suggested and you have kindly solved the problem. Only snag is, the children are now no longer activating on rollover. Here's the code:

div#s11 ul li a:hover{
color: #474747;
background-color: #DFF0DB;
}

div#s11 li.active a {
color: #474747;
background-color: #DFF0DB;
}

div#s11 li.active ul li a{
background-color: #F2F2F2;
}
div#s11 ul li a:hover{
color: #474747;
background-color: #DFF0DB;
}

div#s11 li.active a {
color: #474747;
background-color: #DFF0DB;
}

div#s11 li.active ul li a{
background-color: #F2F2F2;
}
div#s11 li.active ul li a:hover{
/* set desired hover rules, for the children, here. */
}
__________________
  Reply With Quote
Old 27-02-2008, 07:00   #6 (permalink)
hotchilidamo
Registered User
 
Join Date: Feb 2008
Posts: 8
Quote:
Originally Posted by 88mph
just to clarify, you are looking for a method to use CSS to change the display properties to the nav element related to the page that is currently active?

For example, if the user is on the Home page, the 'Home' button in the navigation is styled differently?

Yes thats exactly it. I want the nav elements to have a green highlight when the user is on that page (i.e. active) and I want the other navigation elements to have the same highlight but only on rollover. The dfficulty comes when I have a parent nav element which is active and then the children nav elements taking on the same behaviour
  Reply With Quote
Old 27-02-2008, 07:03   #7 (permalink)
herkalees
Semantics, yay.
 
herkalees's Avatar
 
Join Date: Nov 2005
Location: Salem, Massachusetts
Posts: 1,093
Quote:
Originally Posted by hotchilidamo
Yes thats exactly it. I want the nav elements to have a green highlight when the user is on that page (i.e. active) and I want the other navigation elements to have the same highlight but only on rollover. The dfficulty comes when I have a parent nav element which is active and then the children nav elements taking on the same behaviour
Additionally, this little tutorial I wrote only moments ago might help you: CSS navigation problem…
__________________
  Reply With Quote
Old 27-02-2008, 07:03   #8 (permalink)
hotchilidamo
Registered User
 
Join Date: Feb 2008
Posts: 8
Quote:
Originally Posted by herkalees
div#s11 li.active ul li a:hover{
/* set desired hover rules, for the children, here. */
}

Ah ok, I get it. So you have to pretty much state everything again for children.

Excellent, thank you so much. You've saved me hours of pain
  Reply With Quote
Old 27-02-2008, 07:16   #9 (permalink)
djeglin
goober :-)
 
djeglin's Avatar
 
Join Date: Dec 2006
Location: Birmingham, UK
Posts: 521
Send a message via MSN to djeglin
Incidentally, you might not even need to use a "class=active" here. If this is a nav menu, you can actually use the cascade to your advantage thus:

Put an id on your body tag, or some other parent element to your navigation, that is indicative of the page you are on, like this:

Code:
<body id="home">

Then, you can make your menu like this:

Code:
<ul id="nav> <li class="home"><a href="your home link">Home</a></li> <li class="stuff"><a href="stuff">Stuff</a></li> etc...

and your css can work like this:

Code:
#nav li { Normal styles here } #home #nav li.home, #stuff #nav li.stuff, #stuff2 #nav li.stuff2, (etc...) { active styles here }

This way, you don't need to add a "class=active" to any li elements in your markup, meaning that you can just copy and paste it across your pages without having to edit anything.

Thought that might help.

David
__________________
My signature sucks.
  Reply With Quote
Old 27-02-2008, 07:22   #10 (permalink)
herkalees
Semantics, yay.
 
herkalees's Avatar
 
Join Date: Nov 2005
Location: Salem, Massachusetts
Posts: 1,093
Quote:
Originally Posted by djeglin
Incidentally, you might not even need to use a "class=active" here. If this is a nav menu, you can actually use the cascade to your advantage thus:

Put an id on your body tag, or some other parent element to your navigation, that is indicative of the page you are on, like this:

Code:
<body id="home">

Then, you can make your menu like this:

Code:
<ul id="nav> <li class="home"><a href="your home link">Home</a></li> <li class="stuff"><a href="stuff">Stuff</a></li> etc...

and your css can work like this:

Code:
#nav li { Normal styles here } #home #nav li.home, #stuff #nav li.stuff, #stuff2 #nav li.stuff2, (etc...) { active styles here }

This way, you don't need to add a "class=active" to any li elements in your markup, meaning that you can just copy and paste it across your pages without having to edit anything.

Thought that might help.

David
Beat ya to it, but yes you're right, this is the best method: problems using li class="active"
__________________
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8