Frontend / The cascade and inheritance

The cascade and inheritance

Several CSS rules often try to style the same element. Follow one link through the tests that pick a winner, then see what happens when no rule applies at all.

Intermediate7 steps~7 min
Declaration — one property: value pair in a rule
Browser — built-in default styles
User — the reader’s own settings
Author — the site’s stylesheet
// THE SHORT VERSION

In 30 seconds

When several CSS rules set the same property on one element, the browser runs the cascade: a fixed series of tie-breaks that leaves exactly one value.

It asks where each rule comes from (browser, user or site author), which cascade layer it sits in, how specific its selector is, and finally which was written last.

If no rule sets a property, inheritance takes over: text properties such as color are copied from the parent, while box properties such as border start from their default. The keywords inherit, initial and unset let you choose either behaviour yourself.

The cascade and inheritance, step by step

STEPS01 / 07

Simplified: inline style attributes, animations, shadow DOM and @scope also have places in the real cascade order, and the browser’s actual link rule is longer than a { color: blue }.

STEP 01 / 07 Many rules, one value
THE MATH
a.btn { color: ? } 4 declarations, 1 value
  1. Step 1: Many rules, one value. A link with the class btn. One rule from the browser’s built-in stylesheet and three from the site’s CSS all try to set its color. A property can hold only one value, so the browser needs a fixed procedure for choosing. That procedure is the cascade.
  2. Step 2: Origin: who wrote the rule. First test: where a rule comes from. There are three origins: the browser’s defaults, the user’s own settings, and the author’s stylesheet for the site. Normally author beats user beats browser, so the browser’s blue is out. Declarations marked !important rank in the opposite order, so a user can still override a site.
  3. Step 3: Specificity. Rules from the same origin are compared by specificity: how precisely the selector points at the element. Count the IDs, then the classes, then the element names, and compare from the left. .btn scores 0,1,0 and beats a at 0,0,1. One class outweighs any number of element names.
  4. Step 4: Source order: last wins. Two declarations are still tied: same origin, same specificity. The final tie-break is order of appearance: whichever comes later in the CSS wins. The rule on line 20 beats the one on line 9, so the link is red.
  5. Step 5: Cascade layers. Cascade layers add a test between origin and specificity. An author names layers with @layer, and their order sets their strength: later layers beat earlier ones, and styles outside any layer beat them all. Here theme wins over base even though its selector is the less specific one.
  6. Step 6: Inheritance. When no declaration sets a property on an element, the cascade has nothing to pick from. Some properties then inherit: the element takes its parent’s value. Text properties such as color and font-family inherit. Box properties such as border, margin and padding do not, and fall back to their initial value.
  7. Step 7: inherit, initial, unset. Three keywords set this behaviour by hand, on any property. inherit copies the parent’s value. initial resets to the default in the CSS specification, not the browser’s stylesheet, so display: initial on a div gives inline. unset acts as inherit for properties that normally inherit and as initial for the rest.
VALUES SO FAR
targeta.btn colorquestion
origin····test
specif.····test
order····test
color····result
layers····test
color····rule
border····rule
unset····keyword

Spotted a mistake on this page?