Skip to the content

Lesson 16 of 296

Cascade, specificity and inheritance

What you will learn

  • What happens when two rules try to style the same thing
  • The steps of the cascade
  • How to calculate specificity
  • What !important does and why to avoid it
  • Which properties are inherited
  • The keywords inherit, initial and unset
On this page

The problem

Often more than one rule matches the same element:

p { color: black; }
.intro { color: blue; }
<p class="intro">Hello</p>

Both rules match. Which colour wins? The cascade is the set of rules the browser uses to decide. "Cascading" is the C in CSS.

The cascade: in order

The browser checks these steps one by one. It stops at the first step that gives a winner.

  1. Importance: a declaration with !important beats normal ones.
  2. Origin and layers: your styles beat the browser's default styles. (Modern CSS also has @layer, which lets you put groups of styles in a fixed order.)
  3. Specificity: the more specific selector wins.
  4. Source order: if everything else is equal, the rule that comes last wins.

In the example above, .intro is more specific than p, so the text is blue.

Specificity

Specificity is a score with three columns, written (ID, CLASS, ELEMENT).

Selector partAdds to column
Each ID (#nav)ID
Each class (.card), attribute ([type="text"]) or pseudo-class (:hover)CLASS
Each element (p) or pseudo-element (::before)ELEMENT
* and combinators (>, +, space)Nothing

Compare the columns from left to right. The first column that is bigger wins. One ID beats any number of classes.

Examples

SelectorScore
p(0, 0, 1)
.card(0, 1, 0)
p.card(0, 1, 1)
.card .title(0, 2, 0)
#main .card p(1, 1, 1)
nav ul li a:hover(0, 1, 4)
#main(1, 0, 0)

#main (1,0,0) beats .a.b.c.d.e.f (0,6,0), because the ID column is compared first.

Special cases

  • Inline styles (style="...") beat every selector in your stylesheets.
  • :is() and :not() take the score of the most specific selector inside them.
  • :where() always scores zero. This is useful for default styles that should be easy to override.

!important

.button { color: white !important; }

!important makes a declaration win over normal declarations, whatever their specificity. The only thing that beats it is another !important with higher specificity or later order.

Avoid it. Once you use it, the next fix needs another !important, and soon nothing can be changed. Acceptable uses: overriding styles from a third-party library you cannot edit, and small utility classes that must always apply.

Better fix: lower the specificity of the original rule, or make your selector a little more specific.

Inheritance

Some properties pass from a parent element to its children automatically. This is inheritance.

body {
  font-family: Arial, sans-serif;
  color: #222;
}

Every paragraph, heading and list inside body now uses Arial and the colour #222, without writing it again.

Usually inherited (mostly text)Not inherited (mostly boxes)
colormargin, padding
font-family, font-size, font-weightborder
line-heightwidth, height
text-alignbackground
visibility, cursordisplay, position

The idea: text settings make sense to share, box settings do not. Imagine if every child copied its parent's border: the page would be full of boxes inside boxes.

Controlling inheritance

KeywordMeaning
inheritUse the parent's value
initialUse the property's default value from the CSS rules
unsetinherit if the property normally inherits, otherwise initial
button { font: inherit; }

Buttons and form fields do not inherit the page font by default in most browsers. This one line makes them match the rest of the page.

Debugging: "my style does not work"

  1. Right-click the element and choose Inspect.
  2. In the Styles panel, find your rule.
  3. If it is crossed out, another rule won. The winning rule is shown above it.
  4. Check the specificity of both, and fix the cause instead of adding !important.

Common mistakes

  • Reaching for !important to win a specificity fight.
  • Long selectors like #page .main .content ul li a that are hard to override later.
  • Expecting margin or border to be inherited.
  • Forgetting that later rules win when specificity is equal, then moving files and breaking the order.

How to say it in an interview

"When rules conflict, the cascade decides: first importance, then origin and layers, then specificity, then source order, where the last rule wins. Specificity is counted as IDs, then classes, attributes and pseudo-classes, then elements. Inline styles beat stylesheets, and important beats both, but I avoid important because it makes styles hard to maintain. Text properties like color and font inherit; box properties like margin and border do not."

Key words

WordMeaning
CascadeThe rules that decide which style wins
SpecificityA score that says how specific a selector is
Source orderThe order rules appear in; later wins on a tie
!importantForces a declaration to win over normal ones
InheritanceChildren taking some property values from their parent

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.