CSS Change List Item Background Color with Class
Asked Answered
G

5

13

I am trying to change the background color of one list item while there is another background color for other list items.

This is what I have:

<style type="text/css">
    
ul.nav li
{
  display:inline;
  padding:1em;
  margin:1em;
  background-color:blue;
}
    
    
.selected
{
  background-color:red;
}
   
<ul class="nav">
 <li>Category 1</li>
 <li>Category 2</li>
 <li class="selected">Category 3</li>
 <li>Category 4</li>
</ul>

What this produces is all list items with a blue background (from the "nav" class), as if there were no "selected" class. However, when I take out the background color from the "nav" class, I get the red background for the list item with the "selected" class.

I would like to use the "selected" class for other items on the page (i.e. other list items, divs, etc.).

How would I go about solving this?

Thank you in advance.

Gnomon answered 13/2, 2011 at 10:52 Comment(0)
G
16

This is an issue of selector specificity. (The selector .selected is less specific than ul.nav li.)

To fix, use as much specificity in the overriding rule as in the original:

ul.nav li {
 background-color:blue;
}
ul.nav li.selected {
 background-color:red;
}

You might also consider nixing the ul, unless there will be other .navs. So:

.nav li {
 background-color:blue;
}
.nav li.selected {
 background-color:red;
}

That's a bit cleaner, less typing, and fewer bits.

Gill answered 13/2, 2011 at 10:59 Comment(5)
Thank you! Again, I didn't know about specificity. Does this mean I cannot have one class to affect all sub elements if I had declared a style for those elements?Gnomon
I just found that "!important" from the above answer to do this. Is there a disadvantage to using "!important"?Gnomon
There are tradeoffs. !important is a tool of last resort: it's easy to overuse, and while simple at first it ends up complicating your code in the long run. The general rule is that if you don't have to use !important (and you generally don't), then don't. Even then, you'll want to keep an eye on specificity because !important can be overridden. It's just usually less clear when it will be (one of the reasons to prefer selector specificity).Gill
Here's a good article on this subject: smashingmagazine.com/2010/11/02/…Gill
Thank you, D_N. Point taken about using "!important." I ended up removing '.nav' from '.nav li', and now it's behaving as I want it: to have '.selected' a class selectable by all elements on the "current" page.Gnomon
R
2

The ul.nav li is more restrictive and so takes precedence, try this:

ul.nav li.selected {  
  background-color:red; 
}
Retardment answered 13/2, 2011 at 10:56 Comment(1)
Thank you! I didn't know about specificity.Gnomon
S
2

Scenario: I have a navigation menu like this. Note: Link <a> is child of list item <li>. I wanted to change the background of the selected list item and remove the background color of unselected list item.

<nav>
        <ul>
            <li><a href="#">Intro</a></li>
            <li><a href="#">Size</a></li>
            <li><a href="#">Play</a></li>
            <li><a href="#">Food</a></li>
        </ul>
        <div class="clear"></div>

       </nav>

I tried to add a class .active into the list item using jQuery but it was not working

.active
{
    background-color: #480048;
}

$("nav li a").click(function () {
        $(this).parent().addClass("active");
        $(this).parent().siblings().removeClass("active");
    });

Solution: Basically, using .active class changing the background-color of list item does not work. So I changed the css class name from .active to "nav li.active a" so using the same javascript it will add the .active class into the selected list item. Now if the list item <li> has .active class then css will change the background color of the child of that list item <a>.

nav li.active a
{
    background-color: #480048;
}
Supercool answered 26/2, 2014 at 13:27 Comment(0)
A
1

Live Demo


If you want this to be highlighted depending upon the page your user is on then do this:

To auto-highlight your current navigation, first label your body tags with an ID or class that matches the section of the site (usually a directory) that the page is in.

<body class="ab">

We label all files in the "/about/" directory with the "ab" class. Note that we use a class here to label the body tags. We found that using an ID in the body did not work consistently in some older browsers. Next we label our menu items so we can target them individually thus:

<div id="n"> <a class="b" id="hm"
href="/">Home</a> ... <a class="b"
id="ab" href="/about/">About</a> ...
</div>

Note that we use the "b"utton class to label menu items as buttons and an ID ("ab") to label each unique menu item (in this case about). Now all we need is a CSS selector that matches up the body label with the appropriate menu label like this:

body.ab #n #ab, body.ab #n #ab
a{color:#333;background:#dcdcdc;text-decoration:none;}

This code effectively highlights the "About" menu item and makes it appear dark gray. When you label the rest of the site and menu items, you'll end up with a grouped selector that looks something like this:

body.hm #n #hm, body.hm #n #hm a,
body.sm #n #sm, body.sm #n #sm a,
body.is #n #is, body.is #n #is a,
body.ab #n #ab, body.ab #n #ab a, 
body.ct #n #ct, body.ct #n #ct
a{color:#333;background:#dcdcdc;text-decoration:none;}

For example when the user navigates to the sitemap section the .sm classed body tag matches the #sm menu option and triggers the CSS highlight of the "Sitemap" in the navigation bar.

Source

Archibold answered 13/2, 2011 at 14:21 Comment(1)
Thank you! However, I think what you've provided is a bit outside my comprehension. I will try to study it.Gnomon
B
0

1) You can use the !important rule, like this:

.selected
{
  background-color:red !important;
}

See http://www.w3.org/TR/CSS2/cascade.html#important-rules for more info.

2) In your example you can also get the red background by using ul.nav li.selected instead of just .selected. This makes the selector more specific.

See http://www.w3.org/TR/CSS2/cascade.html#specificity for more info.

Basketwork answered 13/2, 2011 at 10:58 Comment(1)
Thank you! I didn't know about "!important".Gnomon

© 2022 - 2024 — McMap. All rights reserved.