Skip to content
YouTurkey Tech
All CSS topics

The cascade: which rule wins?

Beginner

When several CSS rules set the same property on an element, how the browser picks one: origin and importance, then specificity, then source order, and inheritance when no rule applies.Builds on How the browser builds the DOM

Tree

Empty

Console

Empty

0/12Press Play, or step through with the arrows, to begin.
Step 0 of 12

Explanation

3 min read

On this page

What is the cascade in CSS?

Many rules can set the same property on the same element: your styles, a framework's, the browser's defaults. The is the algorithm that picks one value. For each element and each property, the browser collects every declaration that applies, sorts them, and keeps the first. The result is the element's computed value, which is what DevTools shows under Computed.

How does the browser decide which rule wins?

It compares the declarations step by step, and only goes to the next step when there is a tie:

  1. Origin and importance. Your styles beat the browser's defaults (the user-agent stylesheet). An !important declaration jumps above every normal one; among important declarations the order of origins is reversed, so the browser's and the user's !important beat yours.
  2. Cascade layers. Styles in @layer blocks lose to unlayered styles, and earlier layers lose to later ones.
  3. Specificity. Each selector scores (a, b, c): a counts IDs, b classes, attributes and pseudo-classes, c element names and pseudo-elements. Scores are compared a first, then b, then c, so one ID beats any number of classes. Inline style attributes beat every selector.
  4. Source order. If everything else ties, the declaration written last wins, whether later in the same file or in a stylesheet loaded later.

When no declaration sets a property, inheritance may fill it in. Inherited properties, mostly text ones like color, font-family and line-height, take the parent's computed value. The others, like margin, border and background, fall back to their initial value.

What mistakes do people make with the cascade and !important?

Mini example

Both rules have specificity (0, 1, 0), so the one written last wins and the button is green. Swap the two lines and it turns red:

<style>  .button { color: red; }  .primary { color: green; }</style><button class="primary button">Save</button>

Quick check

In the mini example, what color is the button?

Further reading

All steps as text
  1. Step 1:

    The browser has built the DOM. Now it must give every element a value for every property, starting with the link’s color.

    Lines 8–12

  2. Step 2:

    First the browser collects every rule whose selector matches the link. All four rules in the stylesheet do, and all four set color.

    Lines 2–5

  3. Step 3:

    The browser’s own stylesheet matches too: links are blue by default. That rule comes from another origin, the user agent.

    Line 9

  4. Step 4:

    Origin and importance come first. No rule is !important, and your rules beat the browser’s defaults, so the blue drops out.

  5. Step 5:

    Next, specificity: count the IDs, the classes and the element names in each selector, as (a, b, c).

    Lines 2–5

  6. Step 6:

    The numbers are compared a first, then b, then c. (0, 1, 1) is the highest, so a and .note lose.

    Lines 2–5

  7. Step 7:

    .card a and a.note tie at (0, 1, 1). When everything else is equal, the rule written last wins.

    Lines 3–5

  8. Step 8:

    a.note comes later in the stylesheet, so it wins.

    Line 5

  9. Step 9:

    The cascade is done: green becomes the link’s computed color.

    Line 9

  10. Step 10:

    Now the em. None of your rules matches it, and the browser’s em rule only makes it italic. Nothing sets its color.

    Line 10

  11. Step 11:

    color is an inherited property: with no rule of its own, the em takes its parent’s computed color, green.

    Lines 9–10

  12. Step 12:

    Origin and importance, then specificity, then source order. And when no rule sets an inherited property, it comes from the parent.