- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- Cascade & Inheritance
CSS Foundations
Cascade & Inheritance
Understand how the browser resolves competing declarations and which values pass from parent elements to their children. You will learn the exact order the browser follows, how inherited values fill unresolved properties, and how to trace a declaration without guessing.
Every element ends up with exactly one value for every CSS property. The cascade is the set of rules that picks that value when several declarations apply. Inheritance is what happens when none do: some properties are copied from the parent, others are not. Almost every "my CSS isn't working" bug is one of these two.
Try it
Three rules target the same paragraph. Predict the colour before you press Run, then read on to see why it is what it is.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cascade</title>
<style>
p { color: gray; } /* 0,0,1 */
.lead { color: royalblue; } /* 0,1,0 */
.post p { color: darkorange; } /* 0,1,1 */
#intro { color: seagreen; } /* 1,0,0 ← wins */
</style>
</head>
<body>
<article class="post">
<p id="intro" class="lead">Which colour am I?</p>
</article>
</body>
</html>The cascade, in the order the browser checks
When more than one declaration sets the same property on the same element, the browser compares them on these criteria, top to bottom, and stops at the first one that separates them:
- Origin and importance. There are three origins: the browser's own stylesheet (user agent), the user's stylesheet, and yours (author). Normally author beats user beats browser.
!importantflips that order: an important browser rule beats an important user rule beats an important author rule. This is why you cannot!importantyour way past everything. - Cascade layers. If you use
@layer, later layers beat earlier ones, and unlayered styles beat all layers. Layers are compared before specificity, which is their whole point: a reset in an early layer can never accidentally beat a component style. - Specificity. A three-part score: (IDs, classes/attributes/pseudo-classes, elements/pseudo-elements). Compare left to right; the first part that differs decides.
#introis (1,0,0) and beats.post pat (0,1,1) no matter how many classes you add to the second: a hundred classes never equal one ID. - Source order. Everything equal? The declaration that appears last wins. This is the only rule most people know, and it is the one checked last.
Specificity cheat sheet
| Selector | Score | Notes |
|---|---|---|
| *, +, >, ~ | 0,0,0 | Combinators and the universal selector add nothing |
| p, ::before | 0,0,1 | Elements and pseudo-elements |
| .lead, [type=email], :hover | 0,1,0 | Classes, attributes, pseudo-classes |
| #intro | 1,0,0 | IDs |
| style="…" | above all selectors | Inline styles beat any selector; only !important beats them |
| :is(.a, #b), :not(#b) | the most specific argument | :where() is always 0,0,0 |
Inheritance
If no declaration at all applies to a property on an element, the browser has two fallbacks. For inherited properties it uses the parent's computed value. For everything else it uses the property's initial value. Which is which is not arbitrary: the properties that inherit are the ones about text, because you want a paragraph's words to look like its container's words.
| Inherited (text-ish) | Not inherited (box-ish) |
|---|---|
| color, font-*, line-height, letter-spacing | margin, padding, border |
| text-align, text-indent, text-transform, white-space | background, width, height |
| visibility, cursor, list-style | display, position, overflow, opacity |
You can force either behaviour with four keywords: inherit (take the parent's value), initial (the spec default), unset (inherit if the property normally inherits, otherwise initial), and revert (whatever the browser's stylesheet would have done). all: unset is the nuclear option that resets every property on an element.
A realistic example: why buttons ignore your font
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form controls and inheritance</title>
<style>
.card { font-family: Georgia, serif; font-size: 18px; color: #1e3a8a; padding: 16px; }
.fixed { font: inherit; color: inherit; }
</style>
</head>
<body>
<div class="card">
<p>This paragraph inherits the card's font.</p>
<button>This button does not</button>
<button class="fixed">This one does</button>
</div>
</body>
</html>font and color inherit: so why is the first button in the system font? Because the browser's own stylesheet sets font and color directly on <button>, <input>, <select> and <textarea>. A declaration always beats inheritance, even a low-priority one from the user-agent origin. font: inherit on form controls is in nearly every CSS reset for exactly this reason.
Common mistakes
1. Reaching for !important
It works once. Then the next override needs !important too, and the one after that needs a more specific selector and !important. Fix the specificity or the order instead. The legitimate uses are rare: utility classes that must always apply, and overriding a third-party inline style you cannot edit.
2. Styling with IDs
#sidebar a is (1,0,1). To override it for one link you now need another ID. Use classes for styling and keep IDs for anchors and JavaScript; a stylesheet whose selectors are all single classes is the easiest kind to reason about.
3. Expecting border or background to inherit
.card { border: 1px solid } does not put a border on every child, and a background set on <body> does not "inherit" to <div>s: the divs are simply transparent, so you see through them. If you want the same border on all children, select the children.
4. Loading order surprises
Source order only matters between declarations of equal specificity. Moving your stylesheet <link> below the framework's does not let .btn beat the framework's .btn.btn-primary. Check specificity first, order second.
5. Universal selector resets that kill inheritance
* { font-size: 16px } sets an explicit value on every element, so an <h1> no longer gets its larger size and nested <em>s no longer scale with em units. Resets should target what they mean to reset: * { box-sizing: border-box; margin: 0 } is fine because those properties do not inherit anyway.
6. Inline styles that cannot be overridden
style="color: red" beats every selector in your stylesheet. If a library sets inline styles, your only options are !important or changing the library's configuration. Do not write inline styles yourself except from JavaScript for values that change at runtime.
Debugging it
Open DevTools, select the element, look at the Styles pane. Declarations that lost the cascade are shown struck through, in the order the browser considered them, and hovering a selector shows its specificity. The Computed tab shows the final value of every property and, for inherited ones, which ancestor it came from. Ten seconds there beats ten minutes of guessing.
Related chapters
- CSS specificity: the scoring in full
- Cascade layers:
@layer - Combinators: how
.post pis built - Debugging CSS
Cascade & Inheritance exercises
Color passed down
Make the card navy with one rule for the card only. The heading and the paragraph should take the color from it.
The HTML your code works on
<div class="card">
<h2>Tea</h2>
<p>A hot drink. <a href="#">Order</a></p>
<button>Buy</button>
</div>Result
