css nth child from element with class

后端 未结 2 1344
渐次进展
渐次进展 2021-01-23 01:28

I there a way of selecting (using css) the nth child from an element with a certain class. For example, for the following, how could I select the li element with th

相关标签:
2条回答
  • 2021-01-23 02:24

    You use combinators to select an element relative to another element (which I'll refer to here as the reference element).

    In this case, as you want the second sibling after li.selected, you need to step forward by two elements, using two + sibling combinators:

    li.selected + li + li
    

    As mentioned, you will need to repeat + li n times to reach the nth following sibling of your reference element (see also this related answer). There is no nth-sibling combinator, and :nth-child() is not designed to work with relative selectors.

    0 讨论(0)
  • 2021-01-23 02:28

    Actually you could do with + selector. It is a bit dirty, but works in your case. All you need is to know the exact position of element you need.

    .selected + li + li (adding + li as much times as you need)

    0 讨论(0)
提交回复
热议问题