Sub topic 8
How !important Reverses Layer Order
This chapter assumes you know the following:
- Selector specificity and source order.
- The
@layerblock and statement forms. - That layer order is set by the first appearance of each layer name.
- That unlayered normal declarations beat layered normal declarations.
Earlier chapters showed that a later layer beats an earlier one, and that unlayered styles beat all layers. Both statements are true only for normal declarations. Add !important and the order flips. Many layer bugs come from that flip.
The rule
For normal declarations, the layer that comes later wins, and unlayered styles win over every layer.
For !important declarations, the layer that comes earlier wins. Unlayered !important declarations come last, so they are the weakest of the important ones.
Any !important declaration from the author beats any normal author declaration, whatever the layer.
A worked example
This is the example from the topic plan. It declares two layers and puts a conflicting !important rule in each.
@layer base, overrides;
@layer base {
button { color: red !important; }
}
@layer overrides {
button { color: blue !important; }
}Buttons are red. Without !important, overrides is later and would win, so buttons would be blue. With !important, the order reverses and base wins.
The selectors here are identical, but that does not matter. Specificity is only compared between declarations in the same layer. Layer position settles the conflict first.
The full order for author styles
The list below runs from weakest to strongest, for the author origin only.
- Normal declarations in the first layer declared.
- Normal declarations in each later layer, up to the last one.
- Normal unlayered declarations.
- Important unlayered declarations.
- Important declarations in the last layer.
- Important declarations in each earlier layer, down to the first one.
Entries 4 to 6 are the reversal. The first layer is weakest for normal declarations and strongest for important ones.
Other origins fit around this chain. The browser's own important declarations (user agent origin) beat the user's important declarations, and user important declarations beat author important declarations. Layers use the same idea inside the author origin. The first, most foundational party gets the final say on important rules.
Comparing the cases
The table shows the winner when two rules set the same property on the same element. Layer a is declared before layer b.
| Rule 1 | Rule 2 | Winner |
|---|---|---|
| `a`, normal | `b`, normal | `b` |
| `a`, normal | unlayered, normal | unlayered |
| `a`, important | `b`, important | `a` |
| `a`, important | unlayered, important | `a` |
| `b`, normal | `a`, important | `a` |
| unlayered, normal | `b`, important | `b` |
| unlayered, important | `a`, normal | unlayered |
The last three rows show that importance comes before layer order. A layered important declaration beats an unlayered normal one, which would otherwise win.
Unlayered !important is the weakest important
Here a developer tries to force a color with !important in unlayered code.
@layer base {
a { color: royalblue !important; }
}
a { color: crimson !important; }Links are royal blue. For normal declarations the unlayered rule would win. For important declarations the layered rule wins, because unlayered important sits at the weak end.
Adding !important to an unlayered override therefore does not defeat an important declaration in a layer. This also affects third-party code. If a vendor stylesheet is imported into a layer and uses !important, your unlayered !important cannot override it. You need an important declaration in an earlier layer than the vendor's.
The same applies to inline styles. A style attribute is treated as unlayered. A normal inline declaration beats everything normal, but an inline !important declaration still loses to a layered !important declaration.
Ties inside one layer
Reversal only concerns which layer wins. Inside a single layer, important declarations compete by the usual rules: specificity, then source order.
@layer components {
.card p { color: black !important; }
p { color: gray !important; }
}Both rules are in components and both are important. .card p has higher specificity, so paragraphs inside .card are black. Layers do not change how rules within one layer compare.
Why the browser works this way
The reversal lets an earlier layer set rules that later layers cannot change. Suppose a design-system team owns the base layer and a product team owns components. The design-system team can mark a rule important, for example an accessibility focus outline, and no later layer can override it.
This is also why the !important flag works as a contract here. The flag does not mean "make this win over everything". It means "this is a rule that earlier, more foundational layers want to keep". Teams that use !important to win fights in the latest layer get the opposite result.
@layer reset, base, components, utilities;
@layer reset {
[hidden] { display: none !important; }
}
@layer utilities {
.flex { display: flex; }
}Here an element with both hidden and class="flex" stays hidden. The reset layer is first, so its important rule wins. The .flex utility is normal, so it loses regardless of layer.
A common pitfall: the utility that stops working
Utility classes are often written with !important so they beat component styles. Inside layers this is usually unnecessary, since utilities is already last for normal declarations. It can also hurt.
@layer base, components, utilities;
@layer components {
.btn { background: navy !important; }
}
@layer utilities {
.bg-red { background: red !important; }
}An element with class="btn bg-red" gets a navy background. The component's important declaration is in an earlier layer, so it wins. If the !important is removed from both rules, .bg-red wins as expected. The fix is to remove !important from the component, and from the utility too unless it is truly needed.
Debugging important declarations
When a rule you expect to win does not apply, check these in order:
- Is a competing declaration marked
!important? If so, compare only important declarations with each other. - Which layers do the important declarations belong to? The earliest layer wins. Unlayered important is last.
- Is the layer order what you think it is? The first mention of a layer name fixes its position. A late
@layerblock does not move a layer that was named earlier. - If the declarations are in the same layer, compare specificity, then source order.
In browser DevTools, struck-through declarations in the Styles pane show which rules lost. Recent browsers also label rules with their layer name. Use those labels to check your answers to steps 2 and 3.
To make an important rule defeatable, remove !important from the rule you want to win or lose. If the rule must stay important, move it to an earlier layer or to a layer you control. Do not escalate with another !important in a later layer. Adding it will not work, because later layers lose among important declarations.
Summary
- Normal declarations: later layer beats earlier layer, and unlayered beats all layers.
- Important declarations: earlier layer beats later layer, and unlayered important is weakest.
- Any author important declaration beats any author normal declaration, regardless of layer.
- Specificity and source order only break ties within one layer.
Exercise
Given @layer a, b;, the rules @layer a { p { color: red !important; } }, @layer b { p { color: green !important; } } and the unlayered rule p { color: blue !important; }, what color are paragraphs? What color would they be if all three !important flags were removed?
Show answer
With the flags, paragraphs are red. Among important declarations the earliest layer wins, and unlayered important is weakest. Without the flags, paragraphs are blue, because unlayered normal declarations beat all layered ones.
Exercise
A page loads a vendor stylesheet with @import url("vendor.css") layer(vendor);, declared after @layer vendor, site;. The vendor file contains .modal { z-index: 10 !important; }. You add .modal { z-index: 50 !important; } in @layer site. Which value applies, and what is the least invasive change to make yours win?
Show answer
The vendor value, 10, applies, because vendor is the earlier layer and important declarations favor earlier layers. To make yours win, declare site before vendor in the order statement (@layer site, vendor;), which also reverses the normal order for vendor rules. Otherwise, put your important rule in a layer that comes before vendor.