选择4列网格中的每个第二和第三个元素

时间:2017-03-09 17:20:26

标签: css css3

我正在寻找网格中的中间元素。我对nth选择者并不是特别擅长,所以我在这里不知所措。选择元素的最佳方法是什么:2 + 3,6 + 7,10 + 11,14 + 15.

*,
*:before,
*:after {
  box-sizing: border-box;
  font-family: Helvetica;
}

.flex-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  border: 4px solid #000;
  width: 400px;
}

.flex-child {
  flex-grow: 0;
	flex-basis: 25%;
  background: tomato;
  padding: 10px;
  border: 5px solid red;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}
<div class="flex-container">
  <div class="flex-child">1</div>
  <div class="flex-child">2</div>
  <div class="flex-child">3</div>
  <div class="flex-child">4</div>
  <div class="flex-child">5</div>
  <div class="flex-child">6</div>
  <div class="flex-child">7</div>
  <div class="flex-child">8</div>
  <div class="flex-child">9</div>
  <div class="flex-child">10</div>
  <div class="flex-child">11</div>
  <div class="flex-child">12</div>
  <div class="flex-child">13</div>
  <div class="flex-child">14</div>
  <div class="flex-child">15</div>
  <div class="flex-child">16</div>
  <div class="flex-child">17</div>
  <div class="flex-child">18</div>
  <div class="flex-child">19</div>
  <div class="flex-child">20</div>
</div>

1 个答案:

答案 0 :(得分:0)

*,
*:before,
*:after {
  box-sizing: border-box;
  font-family: Helvetica;
}

.flex-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  border: 4px solid #000;
  width: 400px;
}

.flex-child {
  flex-grow: 0;
	flex-basis: 25%;
  background: tomato;
  padding: 10px;
  border: 5px solid red;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}

.flex-child:nth-child(4n+2),.flex-child:nth-child(4n+3) {
   background: lightgreen;
}
<div class="flex-container">
  <div class="flex-child">1</div>
  <div class="flex-child">2</div>
  <div class="flex-child">3</div>
  <div class="flex-child">4</div>
  <div class="flex-child">5</div>
  <div class="flex-child">6</div>
  <div class="flex-child">7</div>
  <div class="flex-child">8</div>
  <div class="flex-child">9</div>
  <div class="flex-child">10</div>
  <div class="flex-child">11</div>
  <div class="flex-child">12</div>
  <div class="flex-child">13</div>
  <div class="flex-child">14</div>
  <div class="flex-child">15</div>
  <div class="flex-child">16</div>
  <div class="flex-child">17</div>
  <div class="flex-child">18</div>
  <div class="flex-child">19</div>
  <div class="flex-child">20</div>
</div>