Select nth-child across multiple parents

后端 未结 2 1537
故里飘歌
故里飘歌 2020-11-29 13:20

I have the following markup:

  • one
  • two
&
相关标签:
2条回答
  • 2020-11-29 13:41

    You can't do that with CSS selectors alone. :nth-child() and sibling combinators are limited to children/siblings sharing the same parent only, as implied by their names, and CSS selectors cannot account for such variations in parent-child structure, nor is there anything like an :nth-grandchild() selector (even :nth-match() from Selectors 4 limits itself to elements sharing the same parent only).

    Of course with something like jQuery it becomes trivial: $('.foo li:eq(0), .foo li:eq(2)') Otherwise you'll have to mark the first and third li elements explicitly using classes or IDs, and then select them.

    0 讨论(0)
  • 2020-11-29 13:53

    You could use the even and odd selectors.

    li:nth-child(odd) {
      color: red;
    }
    li:nth-child(even) {
      color: white;
    }
    
    0 讨论(0)
提交回复
热议问题