3

我有一个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 的东西感到头疼,如果我能做到这一点,瞧!

结果应如下所示:

在此处输入图像描述

4

4 回答 4

7

演示

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

​</p>

于 2012-09-24T07:54:39.920 回答
0

Just after the solution from @Miszy, I also found a jQuery solution that does the same thing regardless of how many divs will appear on the page:

$(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"});
});

Either one will work fine.

于 2012-09-24T08:09:38.397 回答
0

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

重复模式在 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正方形的首字母并使用父背景。

于 2017-10-18T09:40:44.007 回答
0

这是css棋盘格(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;
  }
}

网页:

<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>
于 2021-07-20T08:55:31.033 回答