Sub topic 5
Unlayered styles win over layered styles
This chapter assumes you know selector specificity and source order, can read a basic CSS rule, and can open browser DevTools to see which declaration is applied. It builds on the earlier chapters about block @layer rules, the order statement, and why layer order beats specificity.
The rule in one sentence
For normal declarations (those without !important), any declaration that is not inside a layer beats every declaration that is inside a layer. This holds whatever the selectors, specificity, or source positions are.
Think of the styles that are not in any layer as one more layer. The browser puts that implicit layer after every named layer. For normal declarations, the last layer wins, so unlayered styles win.
@layer components {
button { background: gray; }
}
button { background: green; }The button rule outside the layer sets the background to green. The layered rule has the same specificity, but that is not the reason it loses. Here is a stronger version of the same conflict:
@layer components {
#toolbar button.primary:hover { background: gray; }
}
button { background: green; }The bare button selector has the lowest possible specificity. It still wins over the layered selector, because layer membership is compared before specificity.
Where this sits in the cascade
The cascade compares competing declarations in a fixed sequence of steps. The first step that finds a difference decides the winner, and later steps are skipped.
- Origin and importance: user-agent, user, and author styles, with
!importantreversing the usual order. - Context (for example shadow DOM).
- Element-attached styles (the
styleattribute). - Layers: unlayered styles and layer order.
- Specificity.
- Order of appearance.
Your site's stylesheets are all in the author origin. Inside that origin, layer comparison happens before specificity. That is why a one-class unlayered selector beats an ID selector in a layer.
Why the design works this way
The rule makes layers safe to adopt in an existing codebase. Suppose you have years of unlayered CSS and you start putting a framework in a layer. Your own unlayered rules keep winning over the framework without you changing a selector. Adding a layer only makes the layered code weaker.
The rule also gives you a convenient escape hatch. If a layered rule is causing trouble in one place, you can write an unlayered override and know that specificity will not matter.
A worked example
Here is a small page with a reset, some component styles, and one unlayered rule left over from older code.
@layer reset, base, components, utilities;
@layer reset {
* { margin: 0; box-sizing: border-box; }
}
@layer base {
a { color: royalblue; }
}
@layer components {
.card a { color: black; }
}
@layer utilities {
.text-red { color: red; }
}
a { color: seagreen; }Consider this markup:
<div class="card">
<a class="text-red" href="/docs">Read the docs</a>
</div>The link matches four rules for color. Three are layered (base, components, utilities) and one is unlayered. The utilities layer is the last layer, so among layered rules .text-red would win. But the unlayered a { color: seagreen; } beats all of them. The link is seagreen.
This surprises people who expect utility classes to always win. The utilities layer only wins against other layers. A stray unlayered rule can override it.
| Rule | Layer | Specificity | Result |
|---|---|---|---|
| `a { color: royalblue; }` | base | 0,0,1 | Loses |
| `.card a { color: black; }` | components | 0,1,1 | Loses |
| `.text-red { color: red; }` | utilities | 0,1,0 | Loses |
| `a { color: seagreen; }` | none | 0,0,1 | Wins |
What counts as unlayered
A declaration is unlayered when no @layer block or layered import contains it. Common sources include:
- Ordinary rules in a stylesheet that does not use
@layerat all. - Rules in a
<style>element or linked stylesheet that are not wrapped in an@layerblock. - Rules imported with a plain
@importthat has nolayer()function. - Styles from a library you load with a
<link>tag, since a<link>cannot place a stylesheet in a layer by itself.
The last item matters in practice. If you load a vendor stylesheet through <link>, it is unlayered, so it beats all of your layered code. To fix that, import it into a layer, or put your own code in layers and the vendor code in an earlier layer. A later chapter covers @import ... layer() in detail.
Inline styles in a style attribute are also outside layers, and they rank above author stylesheet rules in the cascade anyway.
Unlayered styles do not need an order
Layers have an order that you set with the order statement. Unlayered styles have a fixed position, after all layers, so nothing you write can place a named layer after them.
You also cannot name the unlayered styles. There is no @layer unlayered that refers to them. A layer named unlayered is just an ordinary layer with that name, and it still loses to truly unlayered rules.
Specificity and order still apply among unlayered rules
Inside the unlayered group, the usual rules continue to work. Two unlayered rules are compared by specificity, and then by source order.
@layer base {
p { color: gray; }
}
p { color: black; }
.note p { color: navy; }A paragraph inside .note is navy. Both of the last two rules are unlayered, so the cascade skips the layer step and compares specificity. .note p is more specific than p, so it wins. The layered p rule never enters the contest.
The common mistake: leaving old CSS unlayered
Many teams adopt layers gradually. They create layers for new code and leave legacy CSS where it is. Then the layered code appears to be ignored.
@layer components {
.btn { padding: 0.5rem 1rem; }
}
/* legacy.css, loaded without a layer */
button { padding: 0; }A <button class="btn"> gets padding: 0. The .btn selector is more specific, but the unlayered rule wins. DevTools shows the .btn declaration crossed out, with the layer name next to it.
You have three ways to fix this:
- Move the legacy CSS into a layer that comes before
components. - Move the legacy CSS into a layer after
componentsif you want it to keep winning, but make that choice on purpose. - Remove the conflicting legacy rule.
The first option is usually the best. For example:
@layer legacy, components;
@import url("legacy.css") layer(legacy);
@layer components {
.btn { padding: 0.5rem 1rem; }
}Now legacy comes before components, so .btn wins as you expected.
Important declarations are different
Everything above applies to normal declarations. For !important declarations, the order flips. Layered !important declarations beat unlayered !important declarations, and earlier layers beat later layers. A later chapter covers this reversal, so for now keep one point in mind: unlayered styles win for normal declarations and lose for important ones.
@layer base {
button { color: red !important; }
}
button { color: blue !important; }The button is red. The unlayered rule would have won without !important, but important declarations reverse the order, so the layered one wins.
Checking it in DevTools
When a layered rule does not apply, look for these signs in the Styles pane:
- The winning rule has no layer label, which means it is unlayered.
- Layered rules show their layer name, such as
@layer components. - Crossed-out declarations from layers can be defeated by an unlayered rule even when their specificity is much higher.
Do not try to fix the problem by raising the specificity of the layered selector. That has no effect on the layer comparison. Find the unlayered rule and decide whether to layer it, remove it, or keep it as an intended override.
Summary of behavior
| Competing declarations | Winner (normal) | Winner (`!important`) |
|---|---|---|
| Unlayered vs layered | Unlayered | Layered |
| Layered vs layered, different layers | Later layer | Earlier layer |
| Same layer or both unlayered | Higher specificity, then later source | Higher specificity, then later source |
Exercise
A page has @layer base, components; followed by @layer components { #app .btn { color: white; } } and, in a separate unlayered stylesheet, .btn { color: black; }. What color is a <button class="btn"> inside #app, and why? What single change to the unlayered rule's location would make the button white?
Show answer
The button is black. Both rules are normal declarations, and unlayered styles beat layered styles before specificity is compared, so the ID selector's higher specificity does not help. Moving the unlayered .btn rule into a layer that comes before components (for example, a base layer) would make the button white, because components would then be the later layer.
Exercise
You load vendor.css with a <link> tag. It contains a { color: red; }. Your own @layer site { a { color: blue; } } loads afterward. What color are links, and how would you make your site layer win without changing specificity?
Show answer
Links are red, because the <link> stylesheet is unlayered and unlayered normal declarations beat layered ones. To fix it, declare a layer such as @layer vendor, site; and load the stylesheet with @import url("vendor.css") layer(vendor);. Then site comes after vendor, and the blue link rule wins.