CSS nth-child apply odd-even rule but switch every 4 items

后端 未结 3 1599
南旧
南旧 2020-12-19 20:42

I have a list of divs that appear 4 in a row with a class and I would like to create a checkerboard background style, meaning:

  • Apply a different
相关标签:
3条回答
  • 2020-12-19 21:29

    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.

    0 讨论(0)
  • 2020-12-19 21:40

    You may also use only 4 selectors to switch the background-color. (answer similar to @MichałMiszczyszyn => shorter approach)

    The repeating pattern goes on 2 rows of 4 elements, the choice of :nth-child(8n) is indeed the basic pattern to work on, example :

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

    The pattern will repeat itself every 8 elements.

    Notice that you can spare the initial background-color of squares and use the parent background.

    0 讨论(0)
  • 2020-12-19 21:42

    Demo

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

    0 讨论(0)
提交回复
热议问题