css nth child from element with class

后端 未结 2 1346
渐次进展
渐次进展 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题