On this page
Cascade, Specificity & Inheritance
Prev: tutorials | Up: tutorials | Next: Box Model & Formatting Contexts
Rel: Layers, Scope & Custom Properties, Pseudo-Classes, Critical Rendering Path
Ref: MDN Web Docs — Introducing the CSS Cascade
Tags: #public #web #css #tutorials #cascade #specificity
Contents
- > The CSS Value Resolution Pipeline
- > Cascade Precedence Order (Highest to Lowest)
- > Specificity Tuple `(A, B, C)`
- > Inheritance & Explicit Reset Keywords
The CSS Value Resolution Pipeline
For every element and every CSS property, the browser computes a final value in six stages:
- Declared value — All matching declarations from author, user, and browser (user-agent) stylesheets.
- Cascaded value — The single winning declaration after applying the Cascade rules below.
- Specified value — The cascaded value; or if none matched, the inherited value (for inherited properties like
color,font-family) or the property's initial value (for non-inherited properties likeborder,margin). - Computed value — Relative units (
em,rem,currentColor) resolved as far as possible without layout. - Used value — Layout-dependent values (
auto,%widths,frtracks) resolved to concrete pixels. - Actual value — Final rendered value after device-pixel rounding.
Cascade Precedence Order (Highest to Lowest)
- Transitions (active CSS transitions)
- Origin & Importance (note that
!importantreverses origin and layer priority!):- User-agent
!important - User
!important - Author
!important(earlier@layerbeats later@layer; layered!importantbeats unlayered!important) - Animations (
@keyframes) - Author normal: unlayered styles beat later
@layer, which beats earlier@layer - User normal
- User-agent (browser default) normal
- User-agent
- Inline
style="..."attribute (beats any selector in the same origin/layer) - Specificity
(A, B, C) - Source order (later rule wins on an exact tie)
Specificity Tuple (A, B, C)
A: ID selectors (#header)B: Class selectors (.btn), attribute selectors ([type="text"]), pseudo-classes (:hover)C: Type selectors (button), pseudo-elements (::before)- Zero specificity
(0, 0, 0):*, combinators (>,+,~), and:where(...). :is(...),:not(...),:has(...): Take the specificity of their most specific argument.
/* (0, 0, 0) — zero specificity */
:where(nav a) { color: gray; }
/* (0, 1, 1) — 1 class, 1 type */
nav a.active { color: oklch(55% 0.2 250); }
/* (1, 0, 1) — 1 ID, 1 type */
#primary-nav a { color: navy; }
Inheritance & Explicit Reset Keywords
inherit: Forces inheritance from the parent's computed value (even on non-inherited properties).initial: Resets to the CSS spec default (not the browser's UA stylesheet default).unset: Acts likeinheritfor inherited properties andinitialfor non-inherited ones.revert: Rolls back to the previous cascade origin (restoring browser defaults).revert-layer: Rolls back to the previous@layer.
Linked from
- tuts/css/at_rules
- tuts/css/at_rules/layers_and_scope
- tuts/css/properties/box_model
- tuts/css/properties/layout
- tuts/css/properties/motion
- tuts/css/properties/positioning
- tuts/css/properties/typography
- tuts/css/selectors
- tuts/css/selectors/basic_and_combinator_selectors
- tuts/css/selectors/pseudo_classes
- tuts/css/tutorials
- tuts/css/tutorials/box_model_and_formatting_contexts
- tuts/css/tutorials/cascade_specificity_and_inheritance
- tuts/html
- tuts/web/rendering
- tuts/web/rendering/critical_rendering_path
Related notes
- tuts/css/at_rules/layers_and_scope Linked here; links to this page; shared tags: #cascade, #css, #web
- tuts/css/tutorials Linked here; links to this page; shared tags: #css, #tutorials, #web
- tuts/css/tutorials/box_model_and_formatting_contexts Linked here; links to this page; shared tags: #css, #tutorials, #web
- tuts/css/selectors/pseudo_classes Linked here; links to this page; shared tags: #css, #web
- tuts/web/rendering/critical_rendering_path Linked here; links to this page; shared tags: #web
- tuts/css/at_rules links to this page; shared tags: #css, #web
- tuts/css/properties/box_model links to this page; shared tags: #css, #web
- tuts/css/properties/layout links to this page; shared tags: #css, #web
- tuts/css/properties/motion links to this page; shared tags: #css, #web
- tuts/css/properties/positioning links to this page; shared tags: #css, #web