Css :: nth-child:我错过了什么?

时间:2015-12-03 09:52:16

标签: html css css-selectors

我在表格中有2个div,就像2 td's一样。因此我希望2个div为红色,然后2个div为蓝色,依此类推。这不起作用,我错过了什么:

<style>
.irow :nth-child(4n+0), .irow :nth-child(4n+1) {
  background-color: blue;
}
.irow :nth-child(4n+2), .irow :nth-child(4n+3) {
  background-color: red;
}
</style>

<div class="irow">
    <div class="col-md-8">Inès va morfler grave</div>
    <div class="col-md-4">Tête</div>
    <div class="col-md-8">Inès que des mots&nbsp;!</div>
    <div class="col-md-4">Pompon</div>
</div>

2 个答案:

答案 0 :(得分:1)

试试这个https://jsfiddle.net/2Lzo9vfc/257/

.irow div:nth-child(4n+1),
.irow div:nth-child(4n+2) {
  background-color: blue;
}
.irow div:nth-child(4n+3),
.irow div:nth-child(4n+4){
  background-color: red;
}

答案 1 :(得分:1)

您需要使用以下CSS:

    var queueIndex = hash(filename) % workersCount
    // Warning!!
    // Blocks if numbers.Count == dataItems.BoundedCapacity
    filesQueues[queueIndex].Add(fileName);
    filesQueues[queueIndex].CompleteAdding();

这是它的工作原理:

.irow div:nth-child(4n+1), .irow div:nth-child(4n+2){ background-color: red; } .irow div:nth-child(4n+3), .irow div:nth-child(4n+4) { background-color: blue; } 选择每个xn个元素,x是偏移量。例如,+number会找到2n+1的倍数的每个元素,并选择其后面的元素(2)。这将返回具有以下索引的元素:`3,5,7,9&#39;。

在我上面发布的代码中,第一条规则选择每个+1元素(即4th4th)。第二个选择每个8th元素4th(即+15th),依此类推其他两个

JSFiddle