Hacker News new | past | comments | ask | show | jobs | submit login

Sorry stupid question, why are we using nth-last-child vs nth-child ?



I was wondering the same. I suspect it has to do with the initial hack needing it since it relies on the general sibling selector (~) which selects all following siblings but no preceding siblings, so it is important to catch the very first element and all its following siblings:

    li:nth-last-child(n + 5),
    li:nth-last-child(n + 5) ~ li
I don’t think :has has such limitation and I think a simple :has(:nth-child(5)) would yield the same results as :has(:nth-last-child(n + 5)). :nth-last-child(n + 5) will select the first element among five or more siblings, and :nth-child(5) will simply select the fifth element if it exists. I see no reason to write the former over the latter when you’re just wrapping it inside a :has.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: