Can CSS detect the number of children an element has?

后端 未结 7 2266
猫巷女王i
猫巷女王i 2020-11-22 00:52

I\'m probably answering my own question, but I\'m extremely curious.

I know that CSS can select individual children of a parent, but is there support to style the ch

7条回答
  •  情书的邮戳
    2020-11-22 01:09

    yes we can do this using nth-child like so

    div:nth-child(n + 8) {
        background: red;
    }
    

    This will make the 8th div child onwards become red. Hope this helps...

    Also, if someone ever says "hey, they can't be done with styled using css, use JS!!!" doubt them immediately. CSS is extremely flexible nowadays

    Example :: http://jsfiddle.net/uWrLE/1/

    In the example the first 7 children are blue, then 8 onwards are red...

提交回复
热议问题