Sub topic 8
How !important reverses layer order
This chapter assumes you know selector specificity, source order, and how to declare layers with @layer blocks and an order statement. You should also know that unlayered normal styles beat layered normal styles.
The normal rule, and where it flips
For normal declarations (those without !important), a later layer beats an earlier layer. Unlayered styles beat every layer.
For !important declarations, the order reverses. An earlier layer beats a later layer. Unlayered !important declarations are the weakest of the important ones, so any layered !important declaration beats them.
This matches how the cascade already treats origins. Important declarations from a lower-priority origin beat important declarations from a higher-priority origin. Layers follow the same pattern within the author origin.
A worked example
@layer base, overrides;
@layer base {
button { color: red !important; }
}
@layer overrides {
button { color: blue !important; }
}The overrides layer is declared later, so it wins for normal declarations. Both declarations here are important, so the order flips and base wins. Buttons are red.
The reversal lets an early layer protect a rule. A reset or base layer can mark a declaration !important and no later layer can override it by using !important too.
The full priority order
From weakest to strongest, within the author origin:
- Normal declarations in layers, earliest layer first and latest layer last.
- Normal unlayered declarations.
- Important layered declarations, latest layer first and earliest layer last.
- Important unlayered declarations are not last in this list. They sit below all important layered declarations.
The list above is easier to read as a table that puts the important declarations in their own rows.
| Declaration | Priority (1 = weakest) |
|---|---|
| Normal, in a later layer | 2 |
| Normal, in the earliest layer | 1 |
| Normal, unlayered | 3 |
| Important, unlayered | 4 |
| Important, in a later layer | 5 |
| Important, in the earliest layer | 6 |
Specificity and source order only matter between declarations that sit at the same priority, in the same layer.
Normal and important together
Consider this case, where one layer has both kinds of declaration.
@layer base, utilities;
@layer base {
a { color: navy !important; }
}
@layer utilities {
a { color: crimson; }
}The link is navy. The utilities layer is later, which would normally win. But the base declaration is important, and important declarations beat normal ones regardless of layer. The result is navy.
A common surprise follows from this. A utility class such as .text-red in a late layer cannot override an important rule in an early layer. To change that value, you must either remove the !important or add your own important declaration in an earlier layer.
Guidance for using !important with layers
- Avoid
!importantin ordinary component and utility code. Layer order already gives you control. - Use important declarations in early layers only for rules that nothing later should override, such as accessibility settings.
- When an important rule does not apply, check the layer order first, then check whether the winning declaration is in an earlier layer.
- Remember that inline styles with
!importantstill beat all author layers.
Exercise
Given @layer a, b;, a rule p { color: red !important; } in layer a, and a rule p { color: blue !important; } in layer b, what color is the paragraph, and why?
Show answer
Red. Both declarations are important, so layer order is reversed and the earlier layer a wins.
Exercise
A project declares @layer reset, base, components, utilities;. A normal rule in utilities sets display: none, and an important rule in base sets display: block for the same element. Which one applies?
Show answer
display: block. Important declarations beat normal ones, so the important rule in base wins even though utilities is the later layer.