Skip to content Skip to sidebar Skip to footer

Text Color Of Link With "active" Class

I have a navigation bar, and I need the active page, marked by 'subactive' class on the li element to have white text instead of black. The HTML and CSS can be found here: http://j

Solution 1:

That's largely due to how CSS "weighs" the styles. IDs always have more presidence over a standard tag name, class name or pseudo-class. A simple solution is to be as-specific with your anchor class applying the "Active" styling as you are with the rest of the standard elements. Basically:

.subactive a

Should become

#sidenav .subactive a

If that doesn't work for your schema, you'll need to either turn #sidenav in to a class, or work-around it some other way.


By the way, what I was referring to earlier is a style's specificity. As it currently stands, your styles weigh in like so:

#sidenav a              a=0, b=1, c=0, d=1 -> specificity = 0,1,0,1
.subactive a            a=0, b=0, c=1, d=1 -> specificity = 0,0,1,1
#sidenav .subactive a   a=0, b=1, c=1, d=1 -> specfiicity = 0,1,1,1

Almost think of it like this:

(a * 1000) + (b * 100) + (c * 10) + d

The style with the highest number wins.


Solution 2:


Solution 3:

I wouldn't use the !important if I didn't need to.

To make it work I would replace the line

#sidenav a:hover, .subactive a

to

#sidenav a:hover, #sidenav .subactive a:link

and remove the code below which doesn't seem to be used

.subactive a
{
   color: #fff;
   background-color: #034676;
}

​ Check out the working example on http://jsfiddle.net/a4hBz/1/


Post a Comment for "Text Color Of Link With "active" Class"