Cascading Style Sheets (CSS) is the declarative language that powers visual presentation on the web. Despite its apparent simplicity, CSS is governed by a strict mathematical algorithm called The Cascade, which decides which style declaration wins when multiple competing rules target the same element.
The Cascade Priority Algorithm
When multiple declarations compete for the same CSS property on an element, the browser evaluates conflicts in this strict sequential order:
1. Origin & Importance (User Agent < Author < User !important < Author !important)
|
v
2. Specificity Score (IDs vs Classes vs Tags)
|
v
3. Scoping & Proximity (@scope rules)
|
v
4. Order of Appearance (Last declared rule wins)
- Origin and Importance:
- Browser default styles (User Agent stylesheets) have the lowest priority.
- Normal author styles (your stylesheets) override browser defaults.
- User styles (accessibility overrides) override normal author styles.
!importantdeclarations invert this hierarchy: Author!importantoverrides normal styles, and User!importantoverrides everything.
- Specificity: A calculated three-part integer vector determining selector weight.
- Source Order: If origin and specificity are identical, the rule declared latest in the stylesheet wins.
Specificity Calculation Formula (A, B, C)
Specificity is calculated as a three-component vector (A, B, C). It is not a base-10 number (a score of (0, 15, 0) does not become (1, 5, 0) and will never beat a single ID selector of (1, 0, 0)):
| Component | Selector Types Included | Example | Weight |
|---|---|---|---|
| A | ID Selectors | #navbar, #hero-cta | (1, 0, 0) |
| B | Classes, Attributes, Pseudo-classes | .btn, [type="checkbox"], :hover, :nth-child(2) | (0, 1, 0) |
| C | Type/Element Selectors & Pseudo-elements | div, section, p, ::before, ::after | (0, 0, 1) |
/* Specificity: (0, 0, 1) -> Low */
p {
color: #1e293b;
}
/* Specificity: (0, 1, 0) -> Beats 'p' */
.lead-paragraph {
color: #334155;
}
/* Specificity: (0, 1, 1) -> Beats '.lead-paragraph' */
p.lead-paragraph {
color: #0f172a;
}
/* Specificity: (0, 2, 1) -> Two classes + one element */
main .lead-paragraph.featured {
color: #2563eb;
}
/* Specificity: (1, 0, 0) -> ID selector: Beats any number of classes! */
#hero-text {
color: #dc2626;
}
Modern Pseudo-Class Specificity Quirks
Modern CSS introduces functional pseudo-classes that handle specificity differently:
:is(selector...): Takes the specificity of its most specific argument in the list.:where(selector...): Specificity is ALWAYS (0, 0, 0)! Ideal for CSS resets and UI component libraries because any author style can override it easily.:has(selector...): The parent selector takes the specificity of its most specific inner argument.
/* :where() has ZERO specificity! Any custom class will override it effortlessly: */
:where(h1, h2, h3, h4) {
margin-block-end: 0.75rem;
font-weight: 700;
}
/* :is() takes the highest specificity argument (#cta -> (1, 0, 0)) */
:is(section, aside, #cta) p {
line-height: 1.6; /* Specificity is (1, 0, 1) even when matching <section>! */
}
Exercises
- Calculate the specificity vector (A, B, C) for
nav ul li.active a:hover. - Rewrite a high-specificity selector using
:where()to make it easily overridable in downstream theme stylesheets. - Explain why using
!importantin production code causes specificity wars and architectural debt.