【问题标题】:Repeating color patter using css nth-child pseudo code [duplicate]使用css nth-child伪代码重复颜色模式[重复]
【发布时间】:2021-07-23 13:23:22
【问题描述】:

我已经构建了一个示例页面,如下所示。但是框的颜色是通过选择特定的框来分配的。

body, *{
  box-sizing: border-box;
}
.container{
  width:200px;
  margin:0px auto;
}
.box{
  width:100px;
  height:100px;
  float:left;
}
.box:nth-child(1){
  background-color:red;
}
.box:nth-child(2){
  background-color:green;
}
.box:nth-child(3){
  background-color:green;
}
.box:nth-child(4){
  background-color:red;
}
.box:nth-child(5){
  background-color:red;
}
.box:nth-child(6){
  background-color:green;
}
.box:nth-child(7){
  background-color:green;
}
.box:nth-child(8){
  background-color:red;
}
<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>

我想更改它,以便在添加新框或删除任何框时,红绿模式不会中断。我该怎么做?

【问题讨论】:

    标签: html css


    【解决方案1】:

    按照该模式,4n4n+1 .box 孩子是红色的,而 4n+24n+3 .box 孩子是绿色的

    body, *{
      box-sizing: border-box;
    }
    .container{
      width:200px;
      margin:0px auto;
    }
    .box{
      width:100px;
      height:100px;
      float:left;
    }
    
    .box:nth-child(4n), 
    .box:nth-child(4n + 1) {
      background-color:red;
    }
    
    .box:nth-child(4n + 2), 
    .box:nth-child(4n + 3) {
      background-color:green;
    }
    <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>

    在现代浏览器上,您还可以使用新的 :is 伪类对 :nth-child 伪类进行分组

    .box:is(:nth-child(4n), :nth-child(4n + 1)) {
      background-color:red;
    }
    
    .box:is(:nth-child(4n + 2), :nth-child(4n + 3)) {
      background-color:green;
    }
    

    如果您正在寻找更短的代码,只需默认为所有.box 元素分配green 背景,然后仅在4n4n+1 元素上将其更改为red

    .box {
      background-color:green;
    }
    
    .box:nth-child(4n), 
    .box:nth-child(4n + 1) {
      background-color:red;
    }
    

    【讨论】:

    • 该死的,太快了。谢谢 :) 我现在明白了,所以它不是 2 盒模式,而是 4 盒模式。我在我的系统上尝试使用 2n 和 2n+1 ,但它不起作用。 :)
    • 它是4n,因为该模式在 4 个框元素之后重复自身:rggr/rggr/...
    • 对于像这样简单的背景更改,您还可以放弃第二个选择器并为.box添加默认颜色,只留下.box:nth-child(4n), .box:nth-child(4n + 1)
    【解决方案2】:

    您可以将您的选择器与+ * 选择器配对

    body,
    * {
      box-sizing: border-box;
    }
    
    .container {
      width: 200px;
      margin: 0px auto;
    }
    
    .box {
      width: 100px;
      height: 100px;
      float: left;
      background-color: blue;
    }
    
    .box:nth-child(4n+4),
    .box:nth-child(4n+4)+* {
      background-color: blue;
    }
    
    .box:nth-child(4n+2),
    .box:nth-child(4n+2)+* {
      background-color: red;
    }
    <div class="container">
      <div class="box">1</div>
      <div class="box">2</div>
      <div class="box">3</div>
      <div class="box">4</div>
      <div class="box">5</div>
      <div class="box">6</div>
      <div class="box">7</div>
    </div>

    【讨论】:

      猜你喜欢
      • 2014-08-01
      • 2014-03-07
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 2018-06-13
      • 2021-05-18
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多