CSS Cascade Layers vs Specificity: How Conflicts Resolve
This article explains how the modern CSS cascade reconciles conflicts
between cascade layer ordering and standard selector specificity.
Cascade layers introduce an explicit structural mechanism to the cascade
algorithm, allowing developers to control priority independently of
selector weight. Under standard rules, layer order takes absolute
precedence over specificity, meaning a selector with low specificity in
a higher-priority layer will override an ID-heavy selector in a
lower-priority layer. However, this interaction changes dramatically
when !important is applied, as the cascade deliberately
inverts layer priorities to maintain author control.
The Position of Layers in the Cascade Algorithm
To understand how conflicts are resolved, it is necessary to see where layers fit within the full CSS cascade sorting algorithm. When resolving conflicting property declarations on the same element, the browser evaluates criteria in the following order:
- Origin and Importance (User-agent, user, and author styles combined
with normal or
!importantstatus) - Scope Proximity
- Cascade Layer Order
- Selector Specificity
- Order of Appearance (Source order)
Because cascade layers are evaluated at step 3 and specificity is evaluated at step 4, the cascade never compares selector specificity between different layers. Specificity is only used as a tie-breaker among competing rules that exist within the exact same layer.
Normal Declarations: Layer Order Trumps Specificity
For standard, non-!important declarations, layers
declared later in the stylesheet take precedence over layers declared
earlier. Furthermore, unlayered styles automatically take precedence
over all layered styles.
When two rules target the same element from different layers, the browser checks the layer precedence first:
- Between Different Layers: The rule inside the later
layer wins, regardless of selector complexity. A simple element selector
like
buttonin a higher-priority layer will defeat a highly specific selector like#main-nav .menu-item buttonin a lower-priority layer. - Layered vs. Unlayered Styles: Any unlayered rule
automatically defeats a layered rule. A basic class selector
.btnoutside any@layerwill override#submit.btndeclared inside an@layerblock. - Within the Same Layer: If two competing rules reside in the same layer, the layer priority is equal. Only then does the browser proceed to step 4, using standard selector specificity (IDs, classes, elements) to determine the winner.
The !important
Inversion Rule
The interaction between cascade layers and specificity changes
completely when rules contain !important. To prevent
low-level utility or design-system layers from being permanently
overridden by external or feature-level !important rules,
CSS inverts the layer hierarchy for important declarations.
When resolving conflicts between !important
declarations:
- Layered overrides Unlayered: Layered
!importantdeclarations override unlayered!importantdeclarations. - Earlier Layers Win: The earliest declared layer takes the highest priority, inverting the normal layer hierarchy.
For example, if Layer A is declared before Layer B, a normal rule in
Layer B wins over Layer A. However, an !important rule in
Layer A will win over an !important rule in Layer B, as
well as an !important rule declared outside any layer.
Just as with normal styles, selector specificity does not bridge
across layers for !important rules. Specificity only
resolves conflicts between two !important declarations that
reside within the same layer.
Summary of Conflict Resolution
- Layer priority is evaluated before specificity: Specificity cannot break out of a layer to beat a higher-priority layer.
- Specificity is local: Specificity functions strictly as an internal sorting mechanism within each individual layer or within the unlayered style pool.
- Normal styles favor later layers and unlayered rules: Unlayered > Later Layers > Earlier Layers.
- Important styles favor earlier layers: Earlier
Layers (
!important) > Later Layers (!important) > Unlayered (!important).