BeginnerLesson 8 of 305 min read

The CSS cascade, specificity and inheritance

Learn how CSS chooses between conflicting rules: order, specificity, importance and inheritance.

In this lesson you will

  • Predict which rule wins
  • Score selector specificity
  • Understand inheritance
  • Debug with developer tools

About 15 minutes

Why your style is being ignored

You write a rule to make a paragraph red. The paragraph stays blue. You add it again. Still blue. You start doubting yourself, then the browser, then the computer. Almost every beginner has this moment, and the answer is always the same: another rule won. This lesson teaches you how CSS decides the winner, so you can predict it instead of fighting it.

The name gives it away: Cascading Style Sheets. Styles cascade, like water down steps, from several sources, and when they disagree, a clear set of rules chooses one.

Rule 1: the last one wins, if all else is equal

p { color: red; }
p { color: blue; }   /* wins: it comes later */

If two rules have the same weight, the one written later in the stylesheet wins. That applies across files too: a stylesheet linked after another can override it.

Rule 2: more specific beats less specific

Specificity measures how precisely a selector points at an element. The more specific rule wins, no matter where it sits in the file.

p        { color: red; }     /* a plain element: low */
.intro   { color: green; }   /* a class: medium */
#hero    { color: blue; }    /* an id: high */

Think of it as scoring. Count three kinds of things in a selector and read them as three digits:

  • Ids are worth 1,0,0.
  • Classes, attributes and pseudo-classes (like :hover) are worth 0,1,0.
  • Elements (like p) are worth 0,0,1.
p                  /* 0,0,1 */
.note              /* 0,1,0 */
p.note             /* 0,1,1 */
nav .link          /* 0,1,1 */
#menu a:hover      /* 1,1,1 */

Compare from the left. A single id (1,0,0) beats any number of classes, and one class beats any number of elements. The inline style attribute beats them all, and the universal selector * counts for nothing.

The paragraph is red because an id rule beats the class rule. Without using !important, make the green rule win by making its selector more specific (hint: combine the id and the class).

<style>
 #intro { color: red; }
 .lead { color: green; }
</style>
<p id="intro" class="lead">I should be green.</p>

An id scores 1,0,0 and a class 0,1,0. Write a selector that scores 1,1,0: #intro.lead

<style>
 #intro { color: red; }
 #intro.lead { color: green; }
</style>
<p id="intro" class="lead">I should be green.</p>

Rule 3: the origin and !important

Styles come from three origins: the browser’s built-in defaults, the site author (you), and the visitor’s own settings. Your styles beat the browser defaults. Adding !important to a declaration lifts it above normal rules.

.warning { color: red !important; }
Common mistake

Reaching for !important whenever a style does not apply. It works, but it starts a fight: the next time you need to override it, you need another !important, and the stylesheet becomes unmanageable. First check specificity and order. Use !important only for rare utilities, or to override third-party code you cannot edit.

Inheritance: styles that flow down

Some properties pass from a parent to its children automatically. If you set color and font-family on body, every paragraph, heading and list inside it uses them, without writing a rule for each. That is inheritance.

body {
  font-family: Arial, sans-serif;
  color: #14112E;
  line-height: 1.6;
}

Text-related properties such as color, font-*, line-height and text-align are inherited. Box-related properties such as margin, padding, border and background are not, which makes sense: you do not want every child to grow a border just because its parent has one.

You can control this with special keywords. inherit forces a property to take the parent’s value, initial resets it to the CSS default, and unset does the natural thing (inherit if inheritable, otherwise initial).

Debugging with the developer tools

Do not guess which rule is winning. Press F12, click the element, and look at the Styles panel. Every rule that applies is listed in order of priority. Losing declarations are crossed out. You can untick a declaration to test, or type a new value live. The tools tell you exactly which selector won and in which file and line it lives.

  1. Right-click the element and choose Inspect.
  2. Look for your declaration in the Styles panel. Is it crossed out?
  3. If yes, find the rule above it that is overriding. Check its selector and file.
  4. Decide: reorder, make your selector a little more specific, or remove the other rule.
  5. Reload and confirm.

Habits that keep the cascade friendly

  • Keep selectors short and flat: mostly single classes.
  • Write general styles first and specific ones later in the file.
  • Avoid ids in CSS, and avoid long chains such as div#main .box ul li a.
  • Name classes by purpose so you can predict what each affects.
  • Never use !important as the first fix.

Set the font-family, colour and line-height once on body, and see all the text inherit it. Then add a rule so that only the note keeps a different colour.

<style>
 body { /* set inherited text styles here */ }
 .note { /* a different colour */ }
</style>
<h2>Inherited heading</h2>
<p>A paragraph that inherits.</p>
<p class="note">A note with its own colour.</p>

body { font-family: Arial; color: #14112E; line-height: 1.6; } .note { color: #D63384; }

<style>
 body { font-family: Arial, sans-serif; color: #14112E; line-height: 1.6; }
 .note { color: #D63384; }
</style>
<h2>Inherited heading</h2>
<p>A paragraph that inherits.</p>
<p class="note">A note with its own colour.</p>

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

If two rules are equally specific, which wins?

  1. The first one
  2. The last one written
  3. The shorter one
  4. Neither

Later rules override earlier ones when specificity is tied.

Which selector is most specific?

  1. p
  2. .note
  3. #intro
  4. p.note

An id outranks classes and elements.

Which property is inherited?

  1. margin
  2. border
  3. color
  4. padding

Text properties such as color pass down to children.

What is a risk of overusing !important?

  1. It is slow
  2. It makes later overrides harder and the CSS messy
  3. It is invalid
  4. It breaks HTML

It starts an arms race that leads to unmanageable styles.

Where do you check which rule is winning?

  1. The Styles panel in developer tools
  2. The page title
  3. Your email
  4. A zip file

Crossed-out declarations show which rules lost.

Summary

  • Equal specificity: the last rule wins.
  • Ids beat classes beat elements.
  • Text properties inherit; box properties do not.
  • Use the developer tools instead of guessing.

Next lesson

Next: style links, lists and buttons, with hover and focus states.

Questions

Why does my class not override another style?

The other selector is more specific, or it appears later. Inspect the element to see.

Should I ever use !important?

Rarely, for utilities or to override code you cannot edit.

What does the cascade mean?

It is the algorithm that chooses a winner when several rules apply to the same element.

Progress is saved in this browser.