CSS last-child selector: select last-element of specific class, not last child inside of parent?

前端 未结 6 710
夕颜
夕颜 2020-11-22 01:45
6条回答
  •  长情又很酷
    2020-11-22 02:24

    I guess that the most correct answer is: Use :nth-child (or, in this specific case, its counterpart :nth-last-child). Most only know this selector by its first argument to grab a range of items based on a calculation with n, but it can also take a second argument "of [any CSS selector]".

    Your scenario could be solved with this selector: .commentList .comment:nth-last-child(1 of .comment)

    But being technically correct doesn't mean you can use it, though, because this selector is as of now only implemented in Safari.

    For further reading:

    • https://drafts.csswg.org/selectors-4/#the-nth-child-pseudo
    • http://caniuse.com/#search=nth-child

提交回复
热议问题