Html 仅将 CSS 样式应用于嵌套列表根中的第一个 <li>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10451988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:10:02  来源:igfitidea点击:

Applying CSS styling to only the first <li> in the root of a nested list

htmlcss

提问by Jassi Oberoi

i want to apply the CSS only first li but :first-childapply to all first child of every ul

我想只在第一个 li 应用 CSS,但:first-child适用于每个 ul 的所有第一个孩子

here is my CODE

这是我的代码

#menu-navigation li:first-child{
   color:red;
}?

When applied to this HTML:

应用于此 HTML 时:

<ul class="nav" id="menu-navigation">
    <li>Home</li>
    <li>About Us
        <ul>
            <li>Our Team</li>
        </ul>
    </li>
</ul>?

...both "Home" and "Our Team" turn red.

...“家”和“我们的团队”都变红了。

回答by BiAiB

use the child selector:

使用子选择器:

#menu-navigation>li:first-child{
   color:red;
}?

For example: http://jsfiddle.net/w47LD/3/

例如:http: //jsfiddle.net/w47LD/3/

回答by Mike

Wouldn't it be easier to just use an id/class?

仅使用 id/class 不是更容易吗?

<li class="red"> Our Team </li>

.red
{
  color: red;
}



Alternatively you could use an ID...



<li id="red"> Our Team </li>

#red
{
  color: red;
}