What is CSS specificity?

Answer

CSS specificity is the algorithm browsers use to determine which CSS rule wins when multiple rules target the same element and property. It is calculated as a three-part score: [ID count, Class/Attribute/Pseudo-class count, Element/Pseudo-element count]. For example, #nav .link:hover has specificity (1, 2, 0). Higher specificity always wins, regardless of source order — only when specificity ties does the later rule win. Inline styles (style="") override all external/internal CSS. The !important declaration overrides all specificity rules and should be avoided except as a last resort. Understanding specificity is essential for debugging unexpected style overrides.