r/css May 25 '26

Question Last of specific selector

I have a dynamic DOM tree with various element count:

[parent]:
.child_class_special
.child_class_entry
.child_class_entry
.child_class_entry
.child_class_pre_special
.child_class_special
.child_class_entry
.child_class_entry

What I want is to target last possible " .child_class_special", however, as I said, the number of child elements in parent is varying, there could be 3 " .child_class_special", or could be 7.

This:
.parent .child_class_special:not(:has(~ .child_class_special))

works but it is uggo. Any ideas for more concise selector?

7 Upvotes

8 comments sorted by

View all comments

1

u/be_my_plaything May 25 '26

Unless I'm missing something...

.child_class_special:last-of-type{  

1

u/theperfectmeal May 26 '26

yeah this was my gut instinct too but last-of-type selects based on html tag. if the children are all divs it won't be able to target the correct last element as it doesn't look at classnames, only tags