Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS :not selector confusing behavior

This is with reference to this codepen here https://codepen.io/jamesbarnett/pen/vlpkh?editors=1100

I'm was looking for some Pure CSS Star Rating system. I got really confused with the statement .rating:not(:checked) > label:hover

Can someone explain me how is it possible?? A .rating is a fieldset element. How can we even apply a :not(:checked) selector to it. It's never going to be checked. Had this been .rating input:not(:checked), it would have made sense to me.

Is it like it is somehow selecting the inputs inside it. Cause I am not able to find any documentation supporting this behavior of CSS :not selector.

I'm not able to understand how it is working surprisingly.

like image 886
Param Singh Avatar asked Apr 22 '26 13:04

Param Singh


1 Answers

Since the element with class 'rating' is not a checkable input element the :checked pseudo class will never match.

Conversely, the not(:checked) will always match (demo) - making it functionally redundant (except for added specificity) and can therefore safely be omitted

Notice that you can remove the pseudo class from that selector and the demo still works the same (updated codepen).

like image 152
Danield Avatar answered Apr 24 '26 08:04

Danield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!