css-almanac icon indicating copy to clipboard operation
css-almanac copied to clipboard

How much are specificity hacks used?

Open LeaVerou opened this issue 5 years ago • 2 comments
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-selector etc

I suppose BEM could also be considered a specificity hack.

LeaVerou avatar Jul 17 '20 03:07 LeaVerou

Also, html or html body at the beginning of a selector, to just bump it up a little.

bradkemper avatar Jul 23 '20 05:07 bradkemper

Algorithm:

Walk selectors, and count:

  • ids in attribute equality
  • compound selectors with the same class name more than once
  • Descendant selectors with html or :root as 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

LeaVerou avatar Sep 16 '20 23:09 LeaVerou