Apply style to first element in a row of similar elements

前端 未结 3 1673
我寻月下人不归
我寻月下人不归 2020-12-11 13:32

I have the following list (the numbers are just for reference)

alpha1
alpha2
3条回答
  •  死守一世寂寞
    2020-12-11 13:43

    You use the :not() pseudo-class with an adjacent sibling combinator + to match an .A that is not immediately preceded by an .A:

    :not(.A) + .A
    

    You'll also need to use :first-child to select the very first .A element since it's not preceded by anything:

    .A:first-child
    

    Combine them, and you have:

    :not(.A) + .A, .A:first-child { color: red; }
    

    jsFiddle demo

提交回复
热议问题