This question already has answers here:
How to select a range of elements in repeated pattern
                                
                                    (2个答案)
                                
                        
                                在8个月前关闭。
            
                    
我需要通过CSS为每四个替代元素设置背景色。我怎样才能做到这一点?

我找到了替代两个元素的解决方案,但我不了解如何在四个元素中实现。
在这里找到每两个元素:-

How to select every two elements alternating?

最佳答案

li:nth-of-type(4n) {
    color: red;
}


更新:选择四个一组

li:nth-of-type(8n+1),
li:nth-of-type(8n+2),
li:nth-of-type(8n+3),
li:nth-of-type(8n+4)
{
    color: red;
}

li:nth-of-type(8n+5),
li:nth-of-type(8n+6),
li:nth-of-type(8n+7),
li:nth-of-type(8n+8)
{
    color: blue;
}


看到这支笔[更新]
https://codepen.io/prny/pen/GarEMK

W3S https://www.w3schools.com/cssref/sel_nth-of-type.asp中的更多信息

10-05 20:55
查看更多