Aral Code

Selectors & the Cascade Lesson 27 of 33

CSS Specificity, the Cascade and Inheritance

Find out why a style "doesn't work" — how the cascade decides which rule wins, how specificity is counted, how inheritance works, and why to avoid !important.

Sooner or later, every student writes a CSS rule that "doesn't work". The rule is correct, but the element ignores it. Almost always, the reason is that another rule is winning. This lesson explains the three ideas that decide which rule wins: the cascade, specificity and inheritance.

The cascade

When two rules set the same property on the same element, the browser picks a winner by asking these questions, in order:

  1. Importance — does one of them use !important? (More on that later.)
  2. Specificity — which selector is more specific?
  3. Order — if specificity is equal, the rule that comes later wins.
Example Order: the later rule wins
<style>
  p { color: blue; }
  p { color: green; }
</style>
<p>I'm green — same selector, so the later rule wins.</p>

Specificity: the selector "score"

Specificity is like a score for each selector. A more specific selector beats a less specific one, no matter the order. Count three kinds of things:

ColumnCountsExample
A — idseach #id#menu
B — classeseach .class, [attribute], :pseudo-class.active, :hover
C — elementseach element name and ::pseudo-elementli, ::before

Write the score as A-B-C and compare from left to right, like comparing versions or scores in a game. Any id beats any number of classes, and any class beats any number of elements.

SelectorA-B-C
p0-0-1
nav a0-0-2
.btn0-1-0
nav a.active0-1-2
a:hover0-1-1
#header .logo1-1-0
*0-0-0
Example Specificity beats order
<style>
  .note { color: green; }   /* 0-1-0 — wins, even though it comes first */
  p { color: red; }         /* 0-0-1 */
</style>
<p class="note">I'm green. The class selector is more specific than p, even though the p rule comes later.</p>
<p>I'm red.</p>

Inline styles

An inline style="..." attribute beats any selector in a style sheet. Think of it as a score above all three columns. That's one reason to avoid inline styles: they're hard to override later.

A typical "it doesn't work" story

Example Why isn't my link white?
<style>
  #nav a { color: #f2b33d; }     /* 1-0-1 */
  .menu-link { color: white; }  /* 0-1-0 — loses! */
  #nav { background: #0f5c4d; padding: 12px; font-family: Arial, sans-serif; }
</style>
<nav id="nav">
  <a class="menu-link" href="#">Home</a>
  <a class="menu-link" href="#">About</a>
</nav>

The student wanted white links and wrote .menu-link { color: white; }. But an earlier rule with an id, #nav a, has a higher score (1-0-1 vs. 0-1-0), so the links stay yellow. The fix is to make the selectors' specificity balanced, for example by changing the first rule to .nav a and using classes everywhere.

Keep specificity low

Professional developers try to keep specificity low and even:

  • Style mostly with single classes (.card, .btn-primary).
  • Avoid ids in CSS.
  • Avoid long chains like body div.main ul li a. They're hard to override and break when the HTML changes.

When everything has about the same specificity, the simple "later rule wins" ordering decides, and your CSS becomes predictable.

!important

Adding !important after a value makes it beat normal rules, regardless of specificity:

.price { color: red !important; }

It's tempting to use it whenever something "doesn't work", but it's a trap. The only way to beat an !important is another !important with higher specificity, and soon your CSS becomes a war of !importants. Use it only as a last resort, for example to override styles from a library you can't edit.

Inheritance

Some properties are inherited: if you set them on a parent, its children get the same value automatically. That's why setting font-family on body changes the font of the whole page.

  • Inherited (mostly text-related): color, font-family, font-size, line-height, text-align, list-style, visibility
  • Not inherited (mostly box-related): margin, padding, border, background, width, height, display, position
Example Inheritance
<style>
  .card {
    color: #0f5c4d;           /* inherited by everything inside */
    font-family: Georgia, serif;
    border: 2px solid #0f5c4d; /* NOT inherited by children */
    padding: 12px;
  }
</style>
<div class="card">
  <h3>Mabuhay!</h3>
  <p>This paragraph is green and in Georgia, inherited from the card. But it has no border of its own.</p>
</div>

Inheritance is the weakest source of all: any rule that targets the element directly, even a plain p { }, beats an inherited value.

Forcing inheritance

The value inherit makes a property copy its parent's value. A common use: links and buttons don't inherit fonts and colors by default.

button, input, select, textarea { font: inherit; }
a.card-link { color: inherit; text-decoration: none; }

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which selector has the highest specificity?
2. Two rules have equal specificity. Which one wins?
3. Which property is inherited by child elements?
4. Your .btn color isn't applying because #header a wins. What is the best fix?

By Sir Reyel · Last updated 2026-12-04