Style every third element?

前端 未结 8 1651
小蘑菇
小蘑菇 2020-12-15 05:11

How can I style every third element with plain CSS.

I have seen questions like this, but they involve javascript. I want a plain CSS solution. I know I can use

8条回答
  •  没有蜡笔的小新
    2020-12-15 05:39

    section div:nth-child(3n+3) {  
        color: #ccc;
    }
    

    selected elements :

    (3xn)+3 :
    (3 x 0) + 3 = 3 = 3rd Element
    (3 x 1) + 3 = 6 = 6th Element
    (3 x 2) + 3 = 9 = 9th Element
    etc.
    

    you can read it as follow :

    (select all third element) starting at 3

    here you have all what you need as DEMO

    http://css-tricks.com/examples/nth-child-tester/

    read more here http://css-tricks.com/useful-nth-child-recipies/

    http://css-tricks.com/how-nth-child-works/

    Select Only Odd or Even:

    section div:nth-child(odd) {
    
    }
    
    section div:nth-child(even) {
    
        }
    

    Select Only The First Five

    div:nth-child(-n+5) {
    
    }
    

提交回复
热议问题