# The Cascade, Specificity & Inheritance

> When two rules disagree, the cascade picks a winner using origin, specificity, and source order.

Subject: CSS | Lesson 5 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/cascade-specificity-inheritance
Previous: [Pseudo-classes & Pseudo-elements](https://codewithanimation.in/css/pseudo-classes-and-elements.md) | Next: [The Box Model & box-sizing](https://codewithanimation.in/css/box-model-and-box-sizing.md)

## Three layers of conflict resolution

CSS properties are declarations, and multiple declarations can target the same element. The **cascade** is the algorithm that decides which one wins. It applies three criteria in order: **origin and importance**, then **specificity**, then **source order**.

**Inheritance** is separate but related: some CSS properties flow down the DOM tree by default (like `color` and `font-size`), so child elements can pick up values from their ancestors without being explicitly targeted.

## Predictable styles require understanding all three

Specificity is the most common source of 'why is my style not applying?' bugs. The fix is rarely `!important`; it is usually writing a more specific selector or restructuring the stylesheet order.

Understanding inheritance tells you which properties you can set once on a parent (`<body>`) and rely on children to pick up, versus which you must repeat on every targeted element.

## How each layer works

**Origin:** The browser has its own default stylesheet (User Agent). Author styles (your CSS) override UA styles. `!important` reverses this: `!important` author beats `!important` user, but should be a last resort because it breaks the normal cascade.

**Specificity:** Each selector earns a score. Inline `style=` = (1,0,0,0). An id `#x` = (0,1,0,0). A class `.x`, attribute `[x]`, or pseudo-class `:hover` = (0,0,1,0). A type `div` or pseudo-element `::after` = (0,0,0,1). Universal `*` and combinators contribute 0. Add up all the scores; higher tuple wins. You cannot 'overflow' from type to class: ten classes (0,0,10,0) still lose to one id (0,1,0,0).

**Source order:** When origin and specificity are equal, the declaration that appears later in the stylesheet wins. This is why a `<style>` block placed after a `<link>` can override it.

**Inheritance:** Properties like `color`, `font-family`, `font-size`, `line-height`, `text-align` inherit by default. Layout properties like `margin`, `padding`, `background`, `border`, `width` do not. Use `inherit` to force inheritance, `initial` to reset to the CSS spec default, or `unset` to get the natural default (inherit if inheritable, else initial).

## Common mistake: Reaching for !important instead of fixing specificity

Adding `!important` when a style is not applying hides the real problem (specificity or order) and escalates it: now you need `!important` everywhere to override your own override. Diagnose with DevTools instead: the Styles panel shows which declaration is active and why the others are crossed out.

## Tip: Keep specificity as flat as possible

Write mostly class-level selectors (0,0,1,0) and avoid id selectors in CSS. When everything is the same specificity tier, source order is all that matters, which is much simpler to reason about. Specificity wars happen when selectors from different tiers collide.

## Try it: Try it

Watch the specificity scoreboard light up, see conflicting rules resolved, and observe which properties inherit down the tree.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/cascade-specificity-inheritance

## Check yourself

### Question 1

Two rules both set `color` on the same `<p>`. Rule A uses `.card p` (0,0,1,1). Rule B uses `p` (0,0,0,1). Which wins?

- A. Rule A wins because `.card p` has higher specificity
- B. Rule B wins because it is the simpler selector
- C. Source order decides
- D. The `<p>` type selector always wins over class selectors

Correct answer: A

Specificity is compared tuple by tuple. `.card p` scores (0,0,1,1) vs `p` at (0,0,0,1). The first differing position is the class slot: 1 > 0. Rule A wins.

- Why not B: Specificity is calculated by score, not simplicity. A higher score always wins regardless of how complex the selector looks.
- Why not C: Source order is the tiebreaker when specificity is EQUAL. Here Rule A has higher specificity, so source order is irrelevant.
- Why not D: The opposite is true: a class (0,0,1,0) beats a type (0,0,0,1). `.card p` is (0,0,1,1) vs `p` (0,0,0,1), so Rule A wins clearly.

### Question 2

Which CSS keyword forces an element to inherit a property from its parent, even if that property does not inherit by default?

- A. `inherit`
- B. `initial`
- C. `unset`
- D. `revert`

Correct answer: A

`inherit` explicitly tells the browser to use the parent element's computed value for this property, regardless of whether the property inherits by default. For example, `border: inherit` will pull the border from the parent.

- Why not B: `initial` resets the property to the CSS specification's initial value, not the parent's value.
- Why not C: `unset` behaves like `inherit` only for properties that inherit by default. For non-inherited properties it behaves like `initial`.
- Why not D: `revert` rolls back to the browser's User Agent default, not the parent's computed value.

### Question 3

What is the specificity score for this selector: `#nav .link:hover`?

- A. (0,1,2,0)
- B. (0,1,1,0)
- C. (1,0,2,0)
- D. (0,1,1,1)

Correct answer: A

`#nav` = id (0,1,0,0). `.link` = class (0,0,1,0). `:hover` = pseudo-class (0,0,1,0). Total: (0, 1+0+0, 0+1+1, 0) = (0,1,2,0).

- Why not B: `:hover` is a pseudo-class, which scores (0,0,1,0). The full selector has one id (0,1,0,0) + one class (0,0,1,0) + one pseudo-class (0,0,1,0) = (0,1,2,0).
- Why not C: The (1,0,0,0) slot is only for inline `style=` attributes. `#nav` is an id, scoring (0,1,0,0).
- Why not D: There is no type selector in `#nav .link:hover`. Type selectors like `div` or `a` contribute to the fourth slot (0,0,0,1).

## Remember this

- Cascade order: origin/importance first, then specificity, then source order.
- Specificity tuple: (inline, id, class/attr/pseudo-class, type/pseudo-element). Higher = wins.
- Properties like `color` and `font-size` inherit by default. Layout props like `margin` do not.
- Use `inherit`, `initial`, `unset` to control inheritance explicitly. Avoid `!important` except as a last resort.
