</> ConvertAtlas Developer Tools
Level 2 · Intermediate — Layout & Responsive Lesson 15 Intermediate 13 min

Understanding the Cascade and Specificity

Learn exactly which rule wins when several apply — the mechanic behind every CSS surprise.

Why did your style not apply when you clearly wrote it? The answer is almost always the cascade and specificity — the rules that decide which CSS wins. Understanding this turns CSS from guesswork into control.

The cascade

When multiple rules target the same element, CSS resolves conflicts in order: importance, then specificity, then source order. If everything else ties, the rule written later wins. That is the "cascade" in Cascading Style Sheets.

Specificity

Specificity is a score for how specific a selector is. Roughly: inline styles beat ids, ids beat classes, classes beat type selectors. A more specific selector wins even if it is written earlier.

SelectorStrength
style="..." (inline)Strongest
#idStrong
.class, :hover, [attr]Medium
element (p, h1)Weak
* (universal)Weakest
HTML
<p id="x" class="note">Which colour wins?</p>
CSS
p { color: green; }
.note { color: blue; }
#x { color: red; }
Result
The id wins → red. Try deleting the #x rule.

Inheritance

Some properties (like color and font-family) are inherited by children automatically; others (like margin and border) are not. You can force inheritance with the <code>inherit</code> keyword when needed.

Avoiding specificity wars

Common mistake
Don't
#main .list li a.link { color: red; }
Do
.link { color: red; }

Escalating specificity (or using !important) to override styles. Each escalation makes the next override harder. Keep specificity flat and low.

Best practice

Style with single classes wherever possible. Flat, low specificity keeps CSS predictable and easy to override — the foundation of scalable CSS you will meet at the advanced level.

Try it yourself

Write three rules targeting one element (a type, a class and an id selector) with different colours. Predict which wins, then verify in the preview.

Quick quiz

1. If specificity ties, which rule wins?

2. Which is most specific?

3. Which property is inherited by default?

Key takeaways
  • The cascade resolves conflicts by importance, specificity, then order.
  • Specificity: inline > id > class > element > universal.
  • Some properties inherit (color, font); box properties do not.
  • Keep specificity low and flat to avoid override wars.