How does the CSS cascade choose the winning declaration?
Reason through origin, importance, cascade layers, specificity, scoping proximity, and source order.
You add a CSS rule, inspect the element, and discover that another rule wins. The browser is not simply choosing the selector with the most IDs. It follows the cascade: origin, importance, layers, specificity and source order all contribute to the result. Understanding that order is more useful than escalating selector complexity.
You add a CSS rule, inspect the element, and discover that another rule wins. The browser is not simply choosing the selector with the most IDs. It follows the cascade: origin, importance, layers, specificity and source order all contribute to the result. Understanding that order is more useful than escalating selector complexity.
The quick version
The cascade compares declarations in ordered stages. Importance and origin, cascade layer order, specificity, scoping proximity where applicable, and source order determine the winner. Specificity is only one stage.
Build the mental model
Normal declarations in later layers win over earlier layers; important declarations reverse layer precedence. Unlayered normal styles outrank layered normal styles. This is why adding a more specific selector does not always solve a cascade-layer conflict.
See it in code
@layer reset, components, utilities;
@layer components {
.button {
color: navy;
}
}
@layer utilities {
.text-red {
color: crimson;
}
}
Where it gets tricky
Cascade layers are ordered before specificity is compared, so a low-specificity selector in a later normal layer can beat a highly specific selector in an earlier layer. Important declarations reverse layer precedence. Establish layer order once near the application entry point; otherwise independently imported styles can create surprising precedence.
A common misunderstanding
The cascade is not simply “the most specific selector wins.”
What this changes in an application
Use layers to establish predictable ownership and reduce specificity escalation in large applications.
Questions worth exploring next
- How do important declarations reverse layer order?
- What is the role of
:where()?
References and next steps
- Introduction to the CSS cascade — primary or authoritative documentation for the underlying behavior.
- MDN Web Docs — use the relevant API reference for adjacent browser details.