How CSS Actually Works: The Cascade, Specificity, and Inheritance Explained
CSS cascade specificity inheritance explained: how the browser sorts declarations, scores selectors, and falls back when nothing matches.

You write .button { color: red; }, reload, and the button is still blue. DevTools shows your rule with a line through it. Most developers react by adding a parent class, then an ID, then !important. The style finally applies, and the stylesheet gets a little harder to change every time someone repeats the trick.
The CSS cascade is the algorithm the browser uses to pick one winning value when several declarations set the same property on the same element. Specificity is one step of that algorithm: a score computed from the selector. Inheritance is what happens when no declaration sets a property at all, so the element takes the value from its parent. All three are defined in the W3C CSS Cascading and Inheritance Level 5 specification, and every browser implements the same rules.
My position: specificity is the step people obsess over, and it is the least important one. Origin, layers and source order settle most conflicts before specificity is ever consulted. If you learn the full order, you stop fighting specificity and start designing around it.
The three questions the browser asks for every property
Each element has a value for every CSS property, all few hundred of them, even when you never wrote a rule. To get there, the browser answers three questions in order.
- Which declarations apply? Every rule whose selector matches the element, and whose media or container conditions are true, contributes its declarations.
- If several apply, which one wins? The cascade sorts them and keeps the first.
- If none apply, what then? Inherited properties take the parent’s computed value. Everything else takes the property’s initial value.
The result of these three steps is the cascaded value or, when nothing applied, the specified value. The browser then resolves it to a computed value (relative units such as em become pixels where possible), then a used value after layout (percentages of widths become pixels), and finally an actual value after rounding to the device. The Computed tab in DevTools shows the computed value, which is why it sometimes looks different from what you typed.
The cascade sort order, step by step
When two or more declarations target the same property on the same element, the cascade compares them on these criteria, in this order. It moves to the next criterion only on a tie.
- Origin and importance: who wrote the declaration (browser, user or page author) and whether it is marked
!important. - Context: for shadow DOM, styles from the outer tree and from inside the component are compared here.
- Element-attached styles: declarations in a
style=""attribute beat rule-based declarations of the same origin and importance. - Cascade layers: declarations in later
@layerblocks beat earlier ones, and unlayered styles beat all layers. - Specificity: the selector with the higher score wins.
- Order of appearance: the declaration that comes last in the combined stylesheet order wins.
Read that list again and notice where specificity sits. It is fifth. A one-class selector in a later layer beats an ID selector in an earlier layer. An inline style beats both. A user’s !important font-size preference beats everything the page author writes without !important.
Origins and importance in detail
There are three origins. The user-agent origin is the browser’s default stylesheet, which makes h1 large and a blue. The user origin covers styles the reader configures, such as a minimum font size or a high-contrast extension. The author origin is your CSS. Animations and transitions get their own slots too. The full precedence, highest first, is:
| Rank | Origin and importance | Example |
|---|---|---|
| 1 | Transitions | A transition mid-flight |
| 2 | Important user-agent | Rare browser rules that must not be overridden |
| 3 | Important user | A reader forcing a minimum font size |
| 4 | Important author | Your !important declarations |
| 5 | Animations | Values from running @keyframes |
| 6 | Normal author | Almost all of your CSS |
| 7 | Normal user | Reader preferences without !important |
| 8 | Normal user-agent | The browser’s default styles |
The flip in the middle is deliberate. Without !important, authors beat users, so your design wins over casual preferences. With !important, the order reverses, so a reader who needs large text can always get it. !important was designed as an accessibility escape hatch for users. Authors borrowed it as a weapon.
Specificity: how the score is calculated
Specificity is a three-part score written as (A, B, C). You count, for each selector:
- A: ID selectors, such as
#header. - B: class selectors (
.card), attribute selectors ([type="email"]) and pseudo-classes (:hover,:focus-visible). - C: type selectors (
div,a) and pseudo-elements (::before).
The universal selector * and the combinators (space, >, +, ~) add nothing. Scores are compared column by column, left to right. There is no carrying: eleven classes, (0, 11, 0), still lose to one ID, (1, 0, 0). The old “add up the points in base 10” explanation is wrong for exactly this reason.
| Selector | Specificity | Why |
|---|---|---|
p |
(0, 0, 1) | One type selector |
ul li a:hover |
(0, 1, 3) | Three types, one pseudo-class |
.btn.btn-primary |
(0, 2, 0) | Two classes on one element |
a[href^="https"] |
(0, 1, 1) | Attribute selectors count like classes |
#nav .link |
(1, 1, 0) | One ID beats any number of classes |
.card::before |
(0, 1, 1) | Pseudo-elements count like types |
:is(#main, .content) p |
(1, 0, 1) | :is() takes its most specific argument |
:where(#main, .content) p |
(0, 0, 1) | :where() always contributes zero |
li:nth-child(2 of .done) |
(0, 2, 1) | The pseudo-class plus its most specific argument |
The functional pseudo-classes change the math
Four functional pseudo-classes behave differently, and they matter in modern CSS. :is(), :not() and :has() take the specificity of their most specific argument, whether or not that argument is the one that matched. :where() always counts as zero. That last one is the most useful tool you have for writing base styles that are easy to override:
/* Base styles with zero specificity: any single class overrides them */
:where(.prose) a {
color: var(--link);
text-decoration-thickness: 1px;
}
/* (0, 1, 0) beats (0, 0, 1) without a fight */
.footer-link {
color: inherit;
}
The trap is :is() with an ID in its list. :is(#sidebar, .widget) a scores (1, 0, 1) even when it matches inside a plain .widget. If you see an unexpectedly strong selector in DevTools, check for an ID buried inside :is() or :not(). I cover the related :has() rules in the :has() parent selector guide.
Inline styles and nesting
A style attribute is not part of the specificity score at all. It wins one step earlier, at “element-attached styles”. That is why no selector can beat it, and only an author !important rule can. Native CSS nesting, by contrast, does change specificity: a nested rule behaves like :is(parent) child, so the parent selector’s weight is added. The native CSS nesting guide walks through why that surprises people coming from Sass.
Order of appearance: the final tie-breaker
When origin, layer and specificity all tie, the declaration that appears last wins. “Last” means the order of the full stylesheet set: <link> and <style> elements in document order, with @imported sheets expanded in place.
<link rel="stylesheet" href="components.css"> <!-- .btn { color: blue; } -->
<link rel="stylesheet" href="theme.css"> <!-- .btn { color: red; } -->
<!-- Same specificity, theme.css comes later: buttons are red -->
This is also the reason bundlers can change your visual output. If code splitting reorders two CSS chunks, rules with equal specificity can swap winners between development and production. Cascade layers fix that class of bug, because layer order is declared once and does not depend on file order. The details are in the cascade layers guide.
Inheritance: what happens when nothing matches
If no declaration sets a property on an element, the browser checks whether the property is inherited. Inherited properties take the parent’s computed value. Non-inherited properties take their initial value from the spec.
| Usually inherited | Not inherited |
|---|---|
color, font-family, font-size, font-weight, line-height, letter-spacing, text-align, visibility, cursor, direction, custom properties (--*) |
margin, padding, border, background, width, height, display, position, overflow, opacity, transform |
The rule of thumb: text-related properties inherit, box-related properties do not. If borders inherited, every child of a bordered card would draw its own border. MDN lists “Inherited: yes/no” in the formal definition of every property when you need to check.
Children inherit computed values, not what you typed
This is the detail that explains the classic line-height bug.
.article { font-size: 16px; line-height: 1.5em; } /* computes to 24px */
.article h2 { font-size: 32px; } /* inherits 24px: lines overlap */
.article { font-size: 16px; line-height: 1.5; } /* unitless: the factor is inherited */
.article h2 { font-size: 32px; } /* line-height becomes 48px */
With 1.5em, the parent computes the value to 24px, and every descendant inherits 24px regardless of its own font size. A unitless 1.5 computes to the number itself, so each child multiplies it by its own font size. Use unitless line heights everywhere.
The defaulting keywords
Five keywords let you control the cascade explicitly. Every property accepts all of them.
| Keyword | Result | Typical use |
|---|---|---|
inherit |
Parent’s computed value, even for non-inherited properties | border-color: inherit on a nested element |
initial |
The spec’s initial value | Rarely what you want: display: initial is inline |
unset |
inherit for inherited properties, initial otherwise |
Clearing a property back to “as if never set” |
revert |
Rolls back to the previous origin, usually the browser default | all: revert to restore native form controls |
revert-layer |
Rolls back to the previous cascade layer | Opting out of a component layer’s styles |
The initial trap catches people all the time. Writing display: initial on a div makes it inline, because the spec’s initial value for display is inline, not block. The block behavior comes from the browser stylesheet. If you meant “go back to how a div normally looks”, you want revert.
Debugging the cascade in DevTools
Every major browser shows the cascade result directly. In the Styles pane, rules are listed from strongest to weakest, and overridden declarations are struck through. Three habits save hours:
- Start from the Computed tab. Find the property, expand it, and you get every declaration that tried to set it, with the winner first and a link to its source.
- Hover the selector. Chromium DevTools shows the specificity triple in a tooltip, which ends the mental arithmetic.
- Look for layer and scope labels. Rules inside
@layershow the layer name, so you can see when a layer, not specificity, decided the outcome.
Frequently Asked Questions
What is the difference between the cascade and specificity in CSS?
The cascade is the whole algorithm that decides which declaration wins when several set the same property. Specificity is one step inside it, used only after origin, importance, inline styles and cascade layers have tied. Source order breaks any remaining tie.
How is CSS specificity calculated?
Count IDs, then classes, attributes and pseudo-classes, then types and pseudo-elements, and write the counts as (A, B, C). Compare left to right; the first column that differs decides. The universal selector and combinators add nothing, and :where() always adds zero.
Does an inline style always win in CSS?
An inline style attribute beats every normal author rule regardless of selector, because it wins before specificity is checked. It loses to author rules marked !important, to user !important rules, and to running transitions.
Which CSS properties are inherited?
Mostly text-related ones: color, font properties, line-height, letter-spacing, text-align, visibility, cursor and all custom properties. Box properties such as margin, padding, border, background, width and display are not inherited unless you set them to inherit.
Learn the order, then stop fighting it
Almost every “CSS is broken” moment is the cascade doing exactly what the spec says. Keep selectors at one class, put intent into layer order rather than selector weight, and let inheritance carry typography down the tree. Most layout bugs begin one step later, in the box model, display, and positioning.
Rule of thumb: if you are adding a selector to win, you are solving the wrong problem.
Originally posted on 6 March 2025 and post last modified on 6 March 2025