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

后端 未结 3 1605
南旧
南旧 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:42

    Demo

    http://jsfiddle.net/mykhA/1/

    HTML

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

提交回复
热议问题