myth one, "ten classes beat one id". no. specificity is not one number, it is three, (A, B, C). ids go in A, classes and attributes and pseudo-classes go in B, tags and pseudo-elements go in C. browser compares A first. so one id is (1, 0, 0) and ten classes is (0, 10, 0) and the id wins, doesn't matter how many classes you stack. make sure of this okay, it is the one people get wrong most.
myth two, "if i write :where(#main) it is an id so it is heavy". opposite actually. :where() always counts zero, whatever is inside it. that is why people use it for defaults, easy to override later. but :is() and :not() and :has() take the score of the most specific thing inside, so :is(.a, #b) counts as an id even when it was .a that matched. confusing, ya.
myth three, "!important is just a very big specificity". not really, it is a different layer, the score does not even come into it first. and if two rules tie on the score then the one written last wins, that is the boring tie rule and it explains like half of the "why is my css not working" questions.
one more that i liked, :nth-child(2n+1 of .visible) counts as a pseudo-class plus whatever comes after the "of", so that one is (0, 2, 1) with the li in front. the universal selector and combinators like > and + add nothing.
i built a small calculator for this on omnikit, you paste selectors one per line and it colours each part by the column it adds to and tells you who wins. i built it so ya i'm biased, but try your ugliest selector on it and see if the score matches what you guessed. runs in the browser, nothing is sent anywhere.
https://theomnikit.com/dev/css-specificity-calculator?utm_source=devto&utm_medium=article&utm_campaign=devto-specificity-myths
i used an AI assistant to help draft this, the facts are from the tool page.
Top comments (0)