Skip to main content

CSS Specificity Calculator

Calculate selector specificity and understand which CSS rules win.
Enter a selector to see its specificity breakdown.

Common Selector Examples

Understanding CSS Specificity

CSS specificity determines which styles are applied when multiple rules target the same element. It's calculated as a tuple of three values: (IDs, Classes, Elements).

A

IDs

ID selectors like #header have the highest specificity weight.

B

Classes

Classes (.btn), attributes ([type]), and pseudo-classes (:hover).

C

Elements

Type selectors (div, p) and pseudo-elements (::before).

Specificity Rules

  • 1.Compare from left to right: IDs beat any number of classes, classes beat any number of elements.
  • 2.Universal selector (*) and combinators (>, +, ~) have no specificity.
  • 3.:where() has zero specificity. :is(), :not(), and :has() use the specificity of their most specific argument.
  • 4.Equal specificity? The rule that appears later in the stylesheet wins.

Overriding Specificity

!important

The !important declaration overrides normal specificity rules. Use sparingly as it makes CSS harder to maintain.

/* This wins over any specificity */
.button {
color: red !important;
}

Inline Styles

Inline styles (written directly in HTML) have higher specificity than any selector. Only !important can override them.

Tips for Managing Specificity

  • Keep specificity low and consistent - prefer classes over IDs
  • Use BEM or similar naming conventions to avoid specificity wars
  • Avoid !important except for utility classes
  • Use CSS layers (@layer) for better control
  • Avoid deeply nested selectors that increase specificity