CSS nth-child应用奇偶规则,但每4项切换一次

bik*_*y77 3 css css-selectors

我有一个divs与类连续显示4 的列表,我想创建一个棋盘背景样式,其含义是:

  • 为奇数和偶数应用不同的背景颜色 divs
  • 将每行的奇偶数切换为偶数

我已经试过了

.boxwrapper:nth-child(2n-1), .boxwrapper:nth-child(2n) {
    background:#ff0000;
}
.boxwrapper:nth-child(4n-2), .boxwrapper:nth-child(4n-3) {
    background:#0000ff;
}
Run Code Online (Sandbox Code Playgroud)

它适用于奇偶div,但无法使其每4个项目切换一次。如果我能得到正确的结果,我正在研究4n-1、4n + 1的东西!

结果应如下所示:

在此处输入图片说明

Mic*_*zyn 5

演示版

http://jsfiddle.net/mykhA/1/

的HTML

<div class="container">
    <div class="box"></div><div class="box"></div><div class="box"></div><div class="box"></div>
    <div class="box"></div><div class="box"></div><div class="box"></div><div class="box"></div>
    <div class="box"></div><div class="box"></div><div class="box"></div><div class="box"></div>
    <div class="box"></div><div class="box"></div><div class="box"></div><div class="box"></div>
</div>?
Run Code Online (Sandbox Code Playgroud)

的CSS

.container {
    width: 100px;
    height: 100px;
}

.line {
    width: 100px;
    height: 25px;
}

.box {
    width: 25px;
    height: 25px;
    float: left;
}

.box:nth-child(8n+2) {
    background-color: red;
}

.box:nth-child(8n+4) {
    background-color: red;
}
.box:nth-child(8n+5) {
    background-color: red;
}

.box:nth-child(8n+7) {
    background-color: red;
}

.box:nth-child(8n+1) {
    background-color: blue;
}

.box:nth-child(8n+3) {
    background-color: blue;
}

.box:nth-child(8n+6) {
    background-color: blue;
}

.box:nth-child(8n) {
    background-color: blue;
}
?
Run Code Online (Sandbox Code Playgroud)