CSS: apply background color to first-child of ul l

2019-07-13 07:06发布

问题:

I want to apply red background color to 1st li only i have tried several method but not able to get what i want:

#rightmxmenu > ul > li:first-child > {background-color:#ca212d;}
#rightmxmenu > li:first-child > {background-color:#ca212d;}

it does not affect anything

here is my code and css http://jsfiddle.net/gwdp3/1/

hope any expert can tell me what should i need to change.

回答1:

Change this:

#rightmxmenu > ul > li:first-child > {
    background-color:#ca212d;
}

to this:

#rightmxmenu > ul > li:first-child  {
    background-color:#ca212d;
}

jsFiddle example

You don't need the trailing >.



回答2:

Remove the last ">" made it for me.

#rightmxmenu > ul > li:first-child { background-color:#ca212d; }
#rightmxmenu > li:first-child { background-color:#ca212d; }

Hope this helps.



回答3:

You may want to try using a class selector in the css and apply it to all your "first" menu items

.firstItem
{background-color:#ca212d;}

and in HTML use

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


回答4:

This should work just fine:

#rightmxmenu ul li:first-child {
background-color:#ca212d;
}


标签: css css3 menu