The Cascade
The cascade is how CSS resolves conflicting style rules. Specificity, order, and importance determine which styles apply. Understanding the cascade is essential to writing maintainable CSS.
How the Cascade Works
CSS
/* Later rules override earlier ones (when specificity is equal) */
.button {
background-color: blue;
}
.button {
background-color: red;
/* Red overrides blue because it comes later */
}
/* Higher specificity overrides lower specificity */
div {
color: black;
}
.important {
color: red;
/* Class selector has higher specificity */
}
/* !important has highest priority (avoid using!) */
.button {
background-color: blue;
}
.button {
background-color: red !important;
/* !important overrides everything */
}Cascade Order
Source | Priority | Example |
|---|---|---|
Browser default | Lowest | Blue for links |
External/internal CSS | Low | Your stylesheets |
Inline styles | High | style="color: red" |
!important | Highest | color: red !important |
CSS
/* Cascade example */
/* 1. Browser default (lowest priority) */
/* Links are usually blue */
/* 2. External stylesheet */
a {
color: purple;
}
/* 3. Later rule in same file */
a {
color: green;
/* Green overrides purple */
}
/* 4. More specific selector */
a.active {
color: red;
/* More specific than just 'a' */
}
/* 5. Inline style (high priority) */
/* <a style="color: orange;">Link</a> */
/* 6. !important (highest, avoid!) */
a {
color: black !important;
}Warning
Avoid !important. It breaks the cascade and makes CSS hard to maintain. If you need !important, reconsider your CSS architecture.
Note
The cascade resolves style conflicts through specificity and order. Later rules override earlier ones with equal specificity. Higher specificity always wins. Avoid !important.
Next
Specificity calculation: [Calculating Specificity](/css/specificity-calculation).