MarzleyTech Learn

Home / Learn / CSS / The cascade, specificity and inheritance

The cascade, specificity and inheritance

"Why isn't my CSS working?" Nine times out of ten, another rule is winning. The browser decides the winner with three ideas: specificity, order and inheritance. Learn these and you'll debug CSS in seconds.

1. Specificity: the more specific selector wins

Think of specificity as a score with three columns: (IDs, classes, elements).

SelectorIDsClasses / attributes / pseudo-classesElementsScore
p0010-0-1
.price0100-1-0
p.price0110-1-1
.card .price0200-2-0
#total1001-0-0
a:hover0110-1-1

Compare from the left: one ID beats any number of classes; one class beats any number of elements.

HTML · runs live in the interactive lesson
<style>
  #total { color: green; }     /* 1-0-0 wins */
  .price { color: blue; }      /* 0-1-0 */
  p { color: red; }            /* 0-0-1 */
</style>
<p class="price" id="total">KSh 1,200: which colour am I?</p>

Inline styles (style="...") beat all selectors. !important beats even that, but it makes CSS very hard to maintain: avoid it except as a last resort.

2. Order: when specificity ties, the last rule wins

HTML · runs live in the interactive lesson
<style>
  .note { color: blue; }
  .note { color: purple; }  /* same specificity, comes later: wins */
</style>
<p class="note">I am purple.</p>

This is also why your own stylesheet should load after any framework CSS.

3. Inheritance: some properties pass down to children

Text-related properties inherit: color, font-family, font-size, line-height, text-align. Box properties do not: margin, padding, border, background, width.

HTML · runs live in the interactive lesson
<style>
  .card { color: #0b1b35; font-family: Georgia, serif; border: 2px solid #ffb800; padding: 10px; }
</style>
<div class="card">
  <p>This paragraph inherits the colour and font from .card...</p>
  <p>...but not the border or padding.</p>
</div>

You can force inheritance with inherit, e.g. a { color: inherit; } makes links match the surrounding text.

Debugging: use DevTools

  1. Right-click the element → Inspect.
  2. In the Styles panel, rules are listed from most to least specific.
  3. Crossed-out properties are losing. Hover to see which rule wins.

Habits that prevent specificity wars

  • Style mostly with single classes (.card-title), not long chains or IDs.
  • Keep specificity low and flat, so later rules can override easily.
  • Avoid !important.
  • Load your CSS in a sensible order: reset → base → components → utilities.

Why the cascade matters

"Why isn't my CSS working?" is the most common question in web design, and the answer is nearly always the cascade: another rule is winning. On real projects you combine your styles with a theme, a framework like Bootstrap, WordPress plugin styles and browser defaults. Knowing exactly how the browser decides which rule wins lets you fix problems in minutes instead of piling on !important.

The full order the browser uses

When several rules set the same property on the same element, the browser compares them in this order, stopping at the first difference:

  1. Origin and importance: user-agent (browser defaults) < your styles < !important styles (and inline styles beat normal stylesheet rules).
  2. Cascade layers (@layer): later layers win over earlier ones; unlayered styles beat layered ones.
  3. Specificity: IDs > classes/attributes/pseudo-classes > elements/pseudo-elements.
  4. Order of appearance: the last rule wins.

Inheritance only applies when no rule sets the property on the element itself.

Calculating specificity

SelectorIDsClasses, attributes, pseudo-classesElements, pseudo-elementsScore
p0010-0-1
.price0100-1-0
nav a:hover0120-1-2
input[type="email"]0110-1-1
#cart .item p1111-1-1
:where(.card) p0010-0-1 (:where counts zero)
:is(#a, .b) p1011-0-1 (:is takes its most specific argument)
*0000-0-0

Compare from left to right: one ID beats any number of classes.

Cascade layers: organising big stylesheets

@layer lets you decide which groups of styles win, regardless of specificity:

HTML · runs live in the interactive lesson
<style>
  @layer reset, framework, components, utilities;

  @layer framework {
    #main .btn { background: gray; color: #fff; padding: 10px 16px; border: 0; border-radius: 6px; }
  }
  @layer components {
    .btn { background: #0b1b35; }
  }
  @layer utilities {
    .bg-gold { background: #f59e0b; color: #0b1b35; }
  }
</style>
<div id="main">
  <button class="btn">Components layer wins over a more specific framework rule</button>
  <button class="btn bg-gold">Utilities layer wins over both</button>
</div>

Even though #main .btn is more specific, the components layer is declared later, so it wins. This is how modern teams keep third-party CSS from overpowering their own.

Inheritance in detail

Usually inheritedNot inherited
color, font-*, line-height, text-align, letter-spacing, visibility, cursor, list-stylemargin, padding, border, background, width, height, display, position

You can control inheritance explicitly:

HTML · runs live in the interactive lesson
<style>
  .box { color: #1d4ed8; border: 2px solid #f59e0b; padding: 8px; font: 14px system-ui; margin: 6px 0; }
  .box button { color: inherit; font: inherit; border: inherit; }   /* buttons don't inherit fonts by default */
  .reset { all: unset; cursor: pointer; text-decoration: underline; }
</style>
<div class="box">Parent text <button>Inherits colour, font and border</button></div>
<div class="box"><button class="reset">all: unset removes default button styling</button></div>
KeywordMeaning
inheritTake the parent's value
initialUse the CSS specification's default
unsetinherit if the property inherits, otherwise initial
revertGo back to the browser's default style

Form elements (button, input, select) don't inherit fonts by default, which is why many resets include button, input { font: inherit; }.

!important: when (rarely) to use it

CSS
/* Acceptable: utility classes that must always win */
.hidden { display: none !important; }

/* Acceptable: overriding a third-party style you can't edit */
.plugin-widget .title { color: #0b1b35 !important; }

/* Bad habit: using it to win every argument */
.price { color: red !important; }   /* the next developer now needs !important too */

Each !important makes the next change harder. Prefer fixing specificity, using a layer, or adding one class.

Debugging step by step

  1. Right-click the element → Inspect.
  2. In the Styles panel, crossed-out declarations lost to another rule; the winning rule is listed above.
  3. The Computed tab shows the final value and, when expanded, which rule set it.
  4. Check for typos (a misspelled property shows a warning icon), invalid values and the wrong selector.
  5. Toggle checkboxes next to declarations to test changes live, then copy the fix into your CSS file.

Strategies teams use

StrategyIdea
BEM naming (.card__title--large)One class per element; flat, low specificity
Utility-first (Tailwind)Small single-purpose classes in HTML
CSS Modules / scoped stylesTools make class names unique per component
LayersControl priority between reset, framework, components and utilities

All of them aim for the same thing: low, predictable specificity.

Common mistakes

MistakeFix
Styling with IDs (#header)Use classes; save IDs for links and JavaScript
Long selectors (body div.main ul li a)One or two classes
Fighting a framework with !importantLoad your CSS after it, use a layer, or one more class
Expecting padding/border to inheritSet them directly or use inherit
Forgetting buttons don't inherit fontsbutton { font: inherit; }

Practice

  1. Write the specificity of ul#menu li.active > a:hover.
  2. Create three layers (base, components, utilities) and show a utility overriding a component.
  3. Restyle a button so it looks like a link using all: unset plus a few properties.
  4. Take a stylesheet that uses !important five times and rewrite it without any.
Think about it: Bootstrap sets .btn-primary { background-color: #0d6efd } and your .btn { background: #0b1b35 } doesn't apply. Name two clean fixes.Show answer

Your .btn has the same specificity but may load earlier, or is less specific than Bootstrap's combined selectors. Fixes: give your rule equal or greater specificity and load it after Bootstrap (for example .btn.btn-primary), override Bootstrap's CSS variables (like --bs-btn-bg), or put Bootstrap in an earlier @layer so your unlayered styles win.

Check yourself

  1. Which wins: #total or .price .amount .value?

    Show answer

    #total

  2. If two rules have the same specificity, which wins: the first or the last?

    Show answer

    last

  3. Does padding inherit from a parent? (yes or no)

    Show answer

    no

  4. Does color inherit from a parent? (yes or no)

    Show answer

    yes

  5. What is the specificity score of .card .price? Write it like 0-2-0.

    Show answer

    0-2-0

  6. Which CSS at-rule groups styles into priority levels independent of specificity?

    Show answer

    @layer

  7. Which keyword returns a property to the browser's default style?

    Show answer

    revert

  8. Do form buttons inherit font styles by default? (yes or no)

    Show answer

    no

Exercise

The paragraph is red because p.intro is more specific. Add a rule using the id #lead to make it green.

Do this exercise in the live editor

Lesson 9 of 21 in CSS · Written by · Course notes