How it works
CSS Cascading and Inheritance Level 5 describes value processing as a pipeline. Filtering finds the declarations that apply to an element: the style sheet must apply to the document, any conditional rule (@media, @supports) must be true, the selector must match, and the declaration must be syntactically valid. Those form the list of declared values for each property.
The cascade sorts that list. The spec's criteria are lexicographic, like sorting strings: compare by the first criterion; only on a tie look at the second, and so on. The output is a sorted list; its first entry is the cascaded value. Defaulting, inheritance and computation happen afterwards (see the value-stages question).
The origin and importance step uses this order, strongest first:
- Transition declarations
- Important user-agent declarations
- Important user declarations
- Important author declarations
- Animation declarations
- Normal author declarations
- Normal user declarations
- Normal user-agent declarations
Normal declarations go author > user > UA; important ones run the other way. Animations sit above all normal declarations but below every !important one, and a running transition beats everything.
Context handles shadow DOM: a component's normal styles are defaults the page can override; its !important styles are requirements the page can't. Tree contexts are ordered in shadow-including tree order.
Element-attached styles (the style attribute) win over selector-based rules of the same importance. This step comes before layers, so inline styles are not "in" any layer.
Layers split one origin into ordered sublayers. Unlayered declarations go into an implicit final layer, so they win over every named layer for normal declarations.
Specificity compares the selectors' (A, B, C) triples. Scope proximity, in the css-cascade-6 editor's draft, then compares the number of generational or sibling hops between each @scope root and the element; rules without a scope root count as infinitely far. Finally order of appearance takes the last declaration.
Edge cases
Scope proximity runs after specificity. Here the farther scope wins because its selector is more specific:
<style>
@scope (.outer) { span.x { color: blue; } } /* (0,1,1) */
@scope (.inner) { span { color: green; } } /* (0,0,1), nearer */
</style>
<div class="outer"><div class="inner"><span class="x" id="s">s</span></div></div>
<script>
console.log(getComputedStyle(s).color); // → rgb(0, 0, 255)
</script>
Chrome 155 and Firefox 158 agreed; with equal selectors, the nearer .inner root won in both, regardless of source order.
Other cases worth knowing:
- Presentational hints. HTML attributes such as
bgcolor are translated to CSS in an author presentational hint origin, between user and author. They lose to any author rule, even *, but beat normal user styles.
- Inline
!important vs layered !important. Element attachment is compared before layers, so style="color: green !important" beat an unlayered #d { color: red !important } in both browsers.
- Order of appearance.
@imported sheets count as if pasted in place; independently linked sheets are concatenated in link order; style attributes come after every style sheet, in element document order.
Real-world scenario
A team wraps a third-party component library in @layer vendor and writes their own styles unlayered. Their .button { padding: 0 } now beats the vendor's .lib .lib-button:not(.x) without a specificity fight, because the layer step decides before specificity is looked at. The same team then finds that an !important inside the vendor layer beats their own !important. That's the important-layer reversal, and the fix is to drop their !important, not to add more.
When debugging "why doesn't my rule apply", walk the criteria in order: is the winning declaration from a transition or animation, is either one !important, is one inline, which layers are they in, and only then compare specificity and source order.
Trade-offs
The fixed order gives authors coarse controls (origins, layers, inline styles) and fine ones (specificity, order). Coarse controls are predictable: a layer order declared once at the top of the CSS decides conflicts across a whole codebase. Specificity is fine-grained but couples conflict resolution to selector shape, so refactoring a selector can change which rule wins. Modern guidance is to resolve conflicts with layers and keep selectors flat, using !important only for values that must never be overridden.