use*_*902 13 html css css-selectors css3
我知道一堆伪类(第一个孩子,最后一个孩子,第n个孩子),但我在列表或最后2个孩子中选择前2个孩子时遇到问题,列表是动态的并且一直在变化所以我基于对李的计数而无法实现目标
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
412*_*412 32
对于前两个孩子,您可以使用:
ul li:nth-child(-n + 2) {
color: orange;
}
Run Code Online (Sandbox Code Playgroud)
对于最后两个:
ul li:nth-last-child(-n + 2) {
color: orange;
}
Run Code Online (Sandbox Code Playgroud)