Two rules that set the same property on the same element will conflict, and something has to decide which one applies.
That decision is the cascade. There is a defined order of origins, there is a score for each selector called the specificity, and there is a final tie-break based on order.
Once you know the three of them, the "why is my colour wrong" questions disappear.
The previous lesson in this module covered "Which CSS Selectors Cover Everyday Needs?", and the next lesson in this module is about "What Does Inheritance Pass Down and What Does It Not?".
What is the order that the browser applies?
The cascade compares declarations in a fixed order, and the first step that separates them decides the winner.
1. origin and importance
user-agent < user < author < animations < !important author < !important user
2. cascade layers (later layers win over earlier ones)
3. specificity (higher wins)
4. source order (later wins)Almost everything you write is author style, and that is why an inline style and a stylesheet rule compete at the next step.
The place where user styles appear is the browser's own rules, such as the default blue link colour or the default border on a field. A user who has customised their browser gets their choice over yours, and that is deliberate.
That is where the modern part comes from. The cascade layers.
Declaring a layer lets you say "these rules always beat those rules" without writing the !important and without inflating the specificity.
@layer reset, base, components, utilities;
@layer components {
.button { background: #800d4d; }
}
@layer utilities {
.button--ghost { background: transparent; }
}Within a layer, the normal rules still apply.
Between layers, a later layer wins no matter how specific its selector is.
That is exactly the escape hatch a design system needs. A utility class in a later layer can override a component class without a specificity war.
How is the specificity calculated?
Each selector gets a score with three parts, and they are compared like a version number.
IDs come first, then classes, attributes, and pseudo-classes, and then element names and pseudo-elements.
selector score example
p 0-0-1 element
.card 0-1-0 class
.card p 0-1-1 class + element
p:first-child 0-1-1 pseudo-class counts as a class
.card:hover 0-2-0 two classes
#featured 1-0-0 id
#featured .card 1-1-0 id + classThere are two details that are counter-intuitive the first time.
A pseudo-class such as the :hover counts as a class and not as an element.
And a pseudo-element such as the ::before counts as an element, so the p::before is the 0-0-2.
The :where() selector is the escape hatch. It always scores zero, whatever is inside it.
The :is() takes the score of its most specific argument, so it behaves like the most demanding selector in the list.
:where(a, button) { color: inherit; } /* 0-0-0 */
:is(.btn, .link) { padding: 0.5rem 1rem; } /* 0-1-0 */Why is my rule not applying?
There is a short list of reasons, and they come in the order that they are worth checking.
The selector may not match the element at all. You check that first, because it is the most common cause and the least obvious.
The rule may be overridden by a later rule with the same specificity.
A more specific selector may win.
An inline style may win, because inline styles beat any selector.
An !important somewhere may win.
<style>
.card { color: #222; } /* 0-1-0 */
#featured { color: #800d4d; } /* 1-0-0 — wins, and no amount of editing the class rule beats it */
</style>
<div class="card" id="featured">...</div>The fix is almost never the !important.
The fix is to use the same specificity as the rule you are fighting, and to put yours later. Or you restructure so that the question does not arise, and that is what layers are for.
What about the inline styles and the !important?
An inline style is a declaration in the style attribute of the element itself, and it beats any selector-based rule.
That is why a one-off inline value cannot be overridden by a stylesheet, and that is why inline styles are a last resort for styling.
The !important beats everything except another !important, and between those two the later one wins.
It is a genuine escape hatch for a few situations. Overriding a third-party stylesheet that you do not control, or a utility that must win against a component.
It is not a way to win an argument with your own stylesheet.
/* legitimate: your own base styles, and one utility that must win */
.card { color: #222; }
.text-danger { color: #b3261e !important; }How do you find out which rule won?
When a style does not apply, the reason is almost always one of four things, and you can check them in order without guessing.
The rule does not match the element at all. There is a typo in a class name, or there is a selector that expects a descendant where there is none.
Another rule later in the file wins on the specificity.
An inline style or an !important declaration overrides everything.
The property is inherited and not set, so the element is showing the value of its parent.
The browser can tell you which of these it is.
You open the Elements panel, you select the element, and you look at the Styles pane.
Every rule that applies is listed in order, with the winning one highlighted, and any rule that was overridden is shown greyed out with a strikethrough.
Rules that did not match at all are not shown, and that is itself the answer. If the rule you expected is missing from the list, then the selector never matched.
The Computed pane below it gives you the final value of every property, whatever route to that value happened to be.
When the Styles pane and the Computed pane disagree, the Computed pane is the truth. It is what the browser actually used.
<!-- check the resolved value of any property straight from the console -->
getComputedStyle(document.querySelector('.menu')).color
<!-- and the height the element actually took -->
document.querySelector('.menu').getBoundingClientRect().heightThere is one habit to adopt from that panel. When a rule loses, the panel tells you which rule beat it.
You read that line, and you have the answer.
Chasing the cascade from memory is what makes people reach for the !important, and one !important is far more expensive to live with than five minutes of reading the Styles pane.
Quick reference
- The cascade compares origin and importance, then layers, then specificity, and then source order.
- The specificity is three numbers. The IDs, then the classes, and then the elements. They are compared from left to right.
- A pseudo-class counts as a class, and a pseudo-element counts as an element.
- The
:where()scores zero, and the:is()scores its most specific argument. - Cascade layers let a later layer win regardless of the specificity, and that is the clean way to win.
- An inline style beats any selector, and the
!importantbeats everything but another!important.
Try it yourself: write two rules that conflict, one with a class and one with an id, and predict the winner before you reload. Then rewrite the id rule as a class that comes later, and confirm that the result is the same colour for a different reason.
Finally, put that rule in a later cascade layer, and watch the layer order take over from the specificity entirely.