css-almanac
css-almanac copied to clipboard
How much are specificity hacks used?
trafficstars
By specificity hacks I mean selector rewritings or additions with the sole purpose of increasing or decreasing specificity. Things like:
[id=foo].foo.foo.foo:not(#nonexistent) .actual-selectoretc
I suppose BEM could also be considered a specificity hack.
Also, html or html body at the beginning of a selector, to just bump it up a little.
Algorithm:
Walk selectors, and count:
- ids in attribute equality
- compound selectors with the same class name more than once
- Descendant selectors with
htmlor:rootas the first one. - Descendant selectors where the first one is
:not(#id). This is meaningless in terms of selector logic, so it has to be a specificity hack. - Selectors that are entirely classes that look like BEM (
/^(?=.+--|.+__)[a-z0-9-]+(__[\w-]+)?(--[\w-]+)?$/i) or descendants and such classes