The cascade problem

Imagine you're styling a button. You write a CSS rule that makes it blue. Then later, you add another rule that makes it red. Which color does the button get?

The browser doesn't just pick randomly โ€” it follows a set of rules called the cascade. These rules determine which styles win when multiple CSS rules target the same element.

Core concept:
Cascade isn't chaos. It's a predictable system. Understanding it means your styles won't surprise you.

Cascade order: Where CSS comes from

Not all CSS is created equal. The browser applies styles in this order (lowest to highest priority):

  1. Browser defaults โ€” The browser's built-in styles (e.g., links are blue and underlined)
  2. Author styles โ€” Your CSS in <style> tags or external .css files
  3. Inline styles โ€” CSS written directly in the style attribute
  4. !important โ€” Overrides everything (use sparingly!)

Within author styles, there's another layer: specificity. That's where most of the action happens.

cascade-order.css Priority from low to high

Specificity: How the browser scores selectors

Specificity is a number that represents how "specific" a CSS selector is. The more specific, the higher the priority. The browser scores selectors like this:

Selector Type Points Example
Inline style (style attribute) 1000 <div style="color: blue">
ID selector 100 #header
Class, attribute, or pseudo-class selector 10 .btn, [type="text"], :hover
Element or pseudo-element selector 1 div, p, ::before
The 4-number system:
Specificity is written as a-b-c-d where a=inline, b=IDs, c=classes, d=elements. Examples: 0-0-0-1 for div, 0-1-0-0 for #header, 0-0-2-1 for .card.active p.

Specificity scoring examples

specificity-examples.css Calculate specificity for each selector

A higher specificity always wins, even if a rule appears later in the stylesheet. For example:

#header { color: blue; } โ€” Specificity 0-1-0-0 โœ“ WINS
p { color: red; } โ€” Specificity 0-0-0-1 (appears later, but loses)

Even though the second rule appears later, the first rule wins because 0-1-0-0 > 0-0-0-1.

Try it yourself: Specificity battle

In the editor below, you'll see two CSS rules targeting the same element. The more specific rule wins โ€” see which one takes effect! Try adding more specific selectors to the loser rule to make it win.

โš”๏ธ Specificity Battle โ€” Which Rule Wins?
Preview

Cascade order when specificity is equal

If two selectors have the same specificity, which rule wins? The one that appears last in the stylesheet.

cascade-tie.css Equal specificity โ€” last one wins

This is why the order of rules in your stylesheet matters when specificity is equal. Cascade down the file โ€” later rules override earlier ones.

!important โ€” The nuclear option

!important is a declaration that forces a rule to have the highest priority, overriding even inline styles. It looks like this:

important.css !important override example

Why you should avoid !important:

  • It starts a specificity war. If someone else uses !important, you're stuck in an arms race.
  • It makes debugging harder. You can't figure out why a style isn't applying.
  • It violates CSS principles. The cascade is supposed to be predictable and overridable.
  • There's always a better solution. Increase specificity, restructure your HTML, or reorganize your CSS.
Rule of thumb:
If you're about to use !important, stop. Reconsider your CSS architecture. Nine times out of ten, the real problem is poor specificity planning, not a need for !important.

Inheritance: Which properties pass to children?

Not all CSS properties are inherited. Some properties automatically pass from parent to child elements. Others don't.

Inherited properties (most text properties):

  • color, font-size, font-weight, line-height, font-family
  • text-align, text-transform, letter-spacing
  • opacity, visibility

NOT inherited (most layout properties):

  • width, height, padding, margin, border
  • background, display, position
  • box-shadow, transform, filter
inheritance.css Inherited vs non-inherited

Control inheritance: inherit, initial, unset, revert

CSS gives you four keywords to control whether a property is inherited:

inheritance-keywords.css inherit, initial, unset, revert keywords
Keyword What it does When to use
inherit Forces a property to take its parent's value You want a child to match the parent (e.g., margin: inherit)
initial Sets property to CSS specification default You want the standard CSS default
unset Removes all styles for that property Generic reset (works differently for inherited vs non-inherited)
revert Reverts to browser default (newest standard) You want to undo custom styling but keep browser defaults

Debug specificity conflicts with DevTools

The browser's Developer Tools show you exactly which CSS rules apply to an element and why.

  1. Open your browser's DevTools (F12 or Cmd+Opt+I on Mac)
  2. Right-click an element and select "Inspect" or "Inspect Element"
  3. In the Styles panel, you'll see:
    • Green checkmarks โ€” Applied styles
    • Crossed-out rules โ€” Overridden by a more specific rule
    • The filename โ€” Where each rule comes from
  4. Scroll down to see the specificity score for each rule (some tools show it explicitly)

DevTools is your secret weapon for hunting down CSS bugs. If a style isn't applying, DevTools will show you why โ€” there's probably a more specific rule overriding it.

Challenge: Debug & Fix Specificity Conflicts

Task: Below is a CSS stylesheet with 5 specificity conflicts causing wrong colors. Your job: identify each conflict and fix it by adjusting selector specificity. You cannot use !important.

The Problems:

Problem 1: .btn should be green, but it's red (due to button rule) Problem 2: .alert.error should be dark red, but it's light red Problem 3: #success should be dark green, but #status overrides it Problem 4: .card.highlight should have 20px padding, but base .card wins Problem 5: .badge-active should be gold, but .badge wins

In the Try It Yourself editor below, the left side shows the broken CSS and the right side shows the preview. Fix the CSS so all colors and styles match the intended design:

  • .btn should be green (#22c55e)
  • .alert.error should be dark red (#7f1d1d)
  • #success should be dark green (#065f46)
  • .card.highlight should have padding: 20px
  • .badge-active should be gold (#d97706)

Hint: Review the specificity rules above. Remember: ID > class > element. To make .btn beat button, try adding an ID or using a more specific class. For equal specificity, the rule that appears LAST wins. Reorder your CSS rules if needed.

๐Ÿ”ง Fix It โ€” Debug Specificity Conflicts
Preview

Practical CSS specificity tips

  • Use classes, not IDs for styling. IDs have specificity 100, which makes them hard to override. Reserve IDs for JavaScript.
  • Avoid deeply nested selectors. .header .nav .menu .item a is too specific. Keep selectors shallow: .nav-link.
  • Use the cascade to your advantage. More specific rules override general ones โ€” structure your CSS from general to specific.
  • Never use !important in author CSS. Save it only for utility frameworks or third-party overrides.
  • Use a naming convention. BEM (Block Element Modifier) makes specificity predictable: .card__title--active.
  • Test with DevTools. If a style isn't applying, inspect the element and see what's overriding it.

Key takeaways

  • Cascade order (from lowest to highest): browser defaults โ†’ author CSS โ†’ inline styles โ†’ !important
  • Specificity scoring: inline (1000) > ID (100) > class/attribute/pseudo-class (10) > element (1)
  • When specificity ties, the rule that appears last in the stylesheet wins.
  • Some properties are inherited (color, font-size, text-align), others aren't (margin, padding, width).
  • Use inherit, initial, unset, revert to control inheritance explicitly.
  • DevTools is your debugging friend. Inspect elements to see which rules apply and why.
  • Avoid !important. It's almost never the right solution. Fix specificity instead.

Test your knowledge

Question 1 of 5
Which has higher specificity: .class or #id?
Question 2 of 5
What is the specificity of div.card.active?
Question 3 of 5
Why should you avoid !important?
Question 4 of 5
What does the inherit keyword do?
Question 5 of 5
If two CSS selectors have equal specificity, which rule wins?
0
/5
Great work! You understand CSS specificity.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You've mastered CSS specificity.
Build pixel-perfect designs next.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’