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

时间:2012-09-24 07:49:21

标签: 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;
}

它适用于奇数偶数div但不能让它切换每4个项目。我正在为4n-1,4n + 1的东西而烦恼,如果我能做得那么正确的话!

结果应如下所示:

enter image description here

4 个答案:

答案 0 :(得分:3)

演示

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>​

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;
}
​

答案 1 :(得分:0)

在@Miszy的解决方案之后,我还发现了一个jQuery解决方案,无论页面上显示多少div,它都会做同样的事情:

$(document).ready(function() {
    $(.boxwrapper:nth-child(8n+3), .boxwrapper:nth-child(8n+5), .boxwrapper:nth-child(8n+8), .boxwrapper:nth-child(8n+10)").css({"background-color":"red"});
});

任何一个都可以正常工作。

答案 2 :(得分:0)

您也可以只使用4个选择器来切换背景颜色。 (回答类似于@MichałMiszczyszyn=&gt;更短的方法)

重复模式在2行4个元素上进行,:nth-child(8n)的选择确实是要处理的基本模式,例如:

:nth-child(8n-1),
:nth-child(8n-3),
:nth-child(8n-6),
:nth-child(8n-12){
background:/* switch to the other value here */;
}

.square {
  width:25%;
  margin:auto;
  background:turquoise;
  counter-reset:div;
  overflow:hidden; /* to deal with float */
}

.square div {
  float:left;
  width:25%;
  text-align:center;
  background:green;
}
.square div:nth-child(8n-1),
.square div:nth-child(8n-3),
.square div:nth-child(8n-6),
.square div:nth-child(8n-12){
background:tomato;
}

/* demo purpose */
body:hover div div {
background:initial;/* resets to none to show parents background */
}
.square div:before {
  content:'';
  padding-top:100%;
  display:inline-block;
  vertical-align:middle;
}
.square div:after {
  counter-increment:div;
  content:counter(div);
  display:inline-block;
  vertical-align:middle;
  font-size:2vw;
}
<div class="square">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
<hr/>
<div class="square">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

模式将每8个元素重复一次。

请注意,您可以省略最初的background-color正方形并使用父背景。

答案 3 :(得分:0)

这是一个响应式版本的 css checkerboard (damier),从每法线 1 个到每行 5 个。

http://jsfiddle.net/Choufourax/pb09o5fa/1/

CSS

div.grid {
  display: grid;
}
div.grid > div {
    color: red;
    height: 200px;
    background-color: gray;
    /* comment these 3 lines if you don't need to center content */
    display: flex;
    align-items: center;
    justify-content: center;
}
/* Default => 1 per ligne */
@media only screen and (max-width: 600px)   {
  
  div.grid > div:nth-child(odd) {
    background-color: black;
  }
}

/* 50%  => 2 per ligne */
@media only screen and (max-width: 840px) and (min-width: 600px)  {
  div.grid {
   grid-template-columns: 1fr 1fr; 
  }
  div.grid > div:nth-child(4n+1), div.grid > div:nth-child(4n+4) {
    background-color: black;
  }
}

/* 33%  => 3 per ligne */
@media only screen and (max-width: 1024px) and (min-width: 840px)  {
  div.grid {
   grid-template-columns: 1fr 1fr 1fr; 
  }
  div.grid > div:nth-child(6n+1), div.grid > div:nth-child(6n+3), div.grid > div:nth-child(6n+5)  {
    background-color: black;
  }
}

/* 25% => 4 per ligne */ 
@media only screen and (max-width: 1140px) and (min-width: 1024px)  {
  div.grid {
   grid-template-columns: 1fr 1fr 1fr 1fr; 
  }
  div.grid > div:nth-child(8n+1), div.grid > div:nth-child(8n+3), div.grid > div:nth-child(8n+6), div.grid > div:nth-child(8n+8) {
    background-color: black;
  }
}

/* 20% => 5 per ligne */ 
@media only screen and (min-width: 1141px)  {
  div.grid {
   grid-template-columns: 1fr 1fr 1fr 1fr 1fr; 
  }
  div.grid > div:nth-child(10n+1), div.grid > div:nth-child(10n+3), div.grid > div:nth-child(10n+5), div.grid > div:nth-child(10n+7), div.grid > div:nth-child(10n+9) {
    background-color: black;
  }
}

HTML :

<div class="grid">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
    <div>10</div>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
    <div>10</div>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
    <div>10</div>
</div>