CSS: apply background color to first-child of ul li menu

て烟熏妆下的殇ゞ 提交于 2020-01-04 04:06:07

问题


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;
}


来源:https://stackoverflow.com/questions/15250920/css-apply-background-color-to-first-child-of-ul-li-menu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!