[英]CSS first-child pseudo class with unordered list (Bootstrap)
I am trying to render a background color on the list item that is the first child of the unordered list. 我正在尝试在作为无序列表的第一个孩子的列表项上呈现背景色。
HTML structure is as follows HTML结构如下
<div class="nav-collapse">
<ul class="nav">
<li><a href="#">test 1</a></li>
<li><a href="#">test 2</a></li>
<li><a href="#">test 3</a></li>
</ul>
</div>
and to apply the background color on the first child element I did 并在我做的第一个子元素上应用背景色
.nav-collapse > .nav:first-child {
background-color: orange;
}
It renders orange background to all list items. 它将橙色背景呈现给所有列表项。 I've played with slight variations but it doesn't make difference. 我玩的时候有一些细微的变化,但这并没有什么不同。
.nav-collapse > ul.nav:first-child
.nav-collapse > ul:first-child
Use the following: 使用以下内容:
.nav > li:first-child {
background-color: orange;
}
Working jsFiddle here 在这里工作jsFiddle
You were trying to style the first .nav
item - which there is only one of. 您试图设置第一个.nav
项目的样式-只有一个。 Simply change it to style the first li
that is a direct child of .nav
. 只需将其更改为.nav
的直接子.nav
li
样式即可。
If you want to be more specific use: 如果您想更具体地使用:
.nav-collapse > .nav > li:first-child {
background-color: orange;
}
You can do it in many ways, try this too 您可以通过多种方式进行操作,也可以尝试
ul.nav > li:first-child {
background-color: orange;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.