简体   繁体   English

如何覆盖第n个孩子

[英]How to override nth-child

Lets assume I have this simplified HTML tree: 假设我有这个简化的HTML树:

 <ul>
   <li>Item 1</li>
   <li>Item 2</li>         
   <li>Item 3</li>
    .
    .
    .
   <li>Item n</li>
 </ul>

And this CSS style: 而这种CSS样式:

ul li:nth-child(2n){ background-color: blue; }

I have no access to this selector, and so I want to override it to be 我无权使用此选择器,因此我想将其覆盖为

ul li:nth-child(3n){ background-color: red; }

However, this affects both every 2nd, as well as every 3rd element . 但是,这会影响第二个元素以及每个第三个元素 Here's a js-fiddle example. 这是一个js小提琴的例子。

How can I override an nth-child psuedoselector with a new formula, so that only my new formula (3n) takes effect? 如何使用新公式覆盖nth子级psuedoselector,以便只有新公式(3n)生效?

ul li:nth-child(3n){ background-color: transparent; }
ul li:nth-child(2n){ background-color: red; }

I guess this is what you want. 我想这就是你想要的。

ul li:nth-child(3n+1){
    background-color: white;
}

ul li:nth-child(3n+2){
    background-color: blue;
}

ul li:nth-child(3n){
    background-color: red;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM