liveBook · Manning

Cascade

As explained in Web Design Playground, Second Edition

The CSS cascade is the algorithm that resolves competing style rules from different sources to determine the style applied to an element.

The CSS cascade resolves competing style rules and decides which style reaches an element. Think of styles taking different routes downhill: browser defaults, user preferences, and your styles can all arrive at once. When they overlap, the browser chooses one according to its rules. section 1.2

Inheritance describes what happens when styles pass from parent elements to descendants; the cascade describes how those styles are propagated and prioritized. Together with specificity, it keeps a tangled collection of rules from becoming total CSS mayhem. section 19.13 section 19.11

The cascade compares style sources in ascending order of importance:

  • Browser default styles, called the user agent style sheet. section 19.13
  • Custom styles supplied by the browser user, called the user style sheet. section 19.13
  • External and internal author style sheets. section 19.13
  • Inline styles, which outrank the other listed sources. section 19.13

When red meets blue

An internal style sheet sets a div's text to red, while an inline style sets it to blue. The inline declaration wins because its source has greater weight, so the text appears blue. If competing declarations come from the same source, specificity lets the more specific selector win. section 19.13 section 19.15

<style>                        #1
    div {                      #1
        color: red;            #1
    }                          #1
</style>                       #1
<div style="color: blue;">     #2
    What is the color of this text?
</div>

Use the compact listing to inspect a direct conflict between an internal div rule and an inline color declaration. section 19.13

How Web Design Playground, Second Edition covers it

How deep it goes
Explains source priority, inheritance, and specificity, but defers the cascade's full complexity until advanced CSS work.
Examples
Uses a red internal div rule competing with a blue inline declaration.
What it assumes
Assumes basic HTML and CSS rules while distinguishing structure, presentation, inheritance, and three authoring locations.
What you can do after
Diagnose/debug with it by predicting which competing CSS declaration the browser will apply.
How it connects
Connects inheritance, specificity, selectors, inline styles, and author style sheets.

Also explained in

Compare how 5 books explain cascade →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.