How does adding !important change which CSS cascade layer wins?
answer
- the order flips for important
- earliest layer becomes strongest
- unlayered important is the weakest bucket
- same trick origins already play
- a low layer can lock a value
basics
~10 sFor !important author declarations the layer order is reversed: the earliest-declared layer wins, and important unlayered declarations become the weakest of all. So !important inside a reset layer beats !important in a later layer.
solid answer
~50 sLayers do not simply carry `!important` along with them — they invert for it. Among normal author declarations, later layers win and unlayered CSS wins over everything. Among `!important` author declarations, that whole sequence is read backwards: the **first**-declared layer wins, later layers lose, and important declarations in unlayered CSS rank lowest of all. It is the same principle that already governed origins, where `!important` flips user-agent and user styles above the author's, applied one level down to layers. The practical consequence is that `!important` stops being a universal trump card: an important rule you wrote in `utilities` can be beaten by an important rule in `reset`. Used deliberately, this is how a base or theme layer locks a value that no downstream layer can override — and it is also why `!important` inside layered code is now a design decision rather than a last resort.
code
css · 9 lines@layer base, overrides;
@layer base {
.btn { color: red !important; }
}
@layer overrides {
.btn { color: blue !important; }
}go deeper
Recall the headline fact: with !important, the layer order reverses, so the earliest layer wins instead of the latest. Do not claim !important simply beats everything.
Explain both lists — later-wins for normal declarations, earlier-wins for important ones, with unlayered at the strong end of the first and the weak end of the second — and predict the winner from a snippet.
Demonstrate the production use: locking a value from a low layer, beating an uneditable vendor !important from a layer you control, and recognising leftover !important in a high layer as a fix that should be a layer move.
Own the policy question: whether !important is permitted at all once layers exist, which layers may use it, and how you keep important declarations from spreading across layers into an order nobody can read backwards.
## The two lists The cleanest way to hold this is to picture two ordered lists per element, both built from the same layer order, read in opposite directions. Normal author declarations, weakest to strongest: ``` first layer → … → last layer → unlayered ``` `!important` author declarations, weakest to strongest: ``` unlayered → last layer → … → first layer ``` Everything about this topic falls out of those two lines. `!important` does not lift a declaration to the top of the normal list; it moves it into a different list that is ordered the other way. ## A worked example ```css @layer base, overrides; @layer base { .btn { color: red !important; } } @layer overrides { .btn { color: blue !important; } } ``` The button is **red**. Strip both `!important` keywords and it turns blue. That reversal is the single most surprising thing about layers, and it is what the question is really testing. ## Why the spec inverts it The cascade already worked this way one level up: `!important` inverts origin precedence, so an important user-agent or user declaration outranks an important author one, protecting accessibility overrides from being stomped by page CSS. Layers reuse that logic. The intent behind an important declaration is "this must not be overridden by ordinary means", so the layers that sit *lowest* in the normal order — resets, base, theme — are the ones whose important declarations are hardest to displace. A low-priority layer is exactly where you would put a value that must survive: a forced-colors adjustment, an accessibility floor, a print rule. ## Unlayered !important is weak The corollary catches people out. Unlayered normal CSS is the strongest normal bucket, so the instinct is that unlayered `!important` must be the strongest important bucket. It is the weakest one. If a third-party widget ships an unlayered `!important` rule you cannot edit, you can beat it with an important declaration inside any named layer you control: ```css @layer fixes; @layer fixes { .vendor-modal { position: fixed !important; } } ``` That is a genuinely useful escape hatch, and it did not exist before layers. ## What is still above all of it An `!important` declaration in an element's `style` attribute is not reachable by this mechanism; it sits above all important author declarations, layered or not. And transitions and animations have their own places in the cascade, so `!important` interacts with them by different rules. Within plain author CSS, though, the two-list model is complete. ## Using it on purpose A deliberate pattern in design systems: ```css @layer tokens, reset, base, components, utilities; @layer tokens { :root { --focus-ring: 2px solid Highlight; } } @layer reset { :focus-visible { outline: var(--focus-ring) !important; } } ``` The focus ring is locked from the earliest layer, so no component or utility can quietly remove it with its own important rule — while ordinary, non-important component styling still behaves normally. That is a defensible use of `!important`; using it in `utilities` to "win harder" is not, because with layers the correct tool for winning is layer position. ## The review heuristic When you see `!important` in a layered codebase, ask which of two things it is. If it is a lock in a low layer, deliberately placed so later layers cannot override it, that is the mechanism working as designed. If it is in the highest layer trying to force a value, it is a leftover reflex from the pre-layer era, and moving the rule to a later layer — or a different layer entirely — is almost always the better fix. The failure mode teams actually hit is important declarations scattered across several layers, at which point the winner is determined by layer order that nobody is reading in reverse, and debugging gets genuinely hard.
- A vendor stylesheet you cannot edit ships an unlayered !important rule. Can layers help you override it?Yes. Important unlayered declarations are the weakest important author bucket, so an `!important` declaration inside any named layer you control will beat it. Create a small `fixes` layer, put the important override there, and you win without touching the vendor file — something that was impossible before layers, when the only answer was a more specific important selector.
- Where would you deliberately place an !important declaration in a layered architecture?In one of the earliest layers — a reset, tokens, or accessibility layer — because important declarations there are the hardest for later layers to displace. That is how you lock a focus outline or a forced-colors adjustment. Putting `!important` in the last layer is the anti-pattern: if you need to win there, the right lever is layer position, not importance.
- Does the inversion also apply to an !important declaration in an element's style attribute?No. An important declaration in the `style` attribute sits above all important author declarations regardless of layer, so layer inversion never reaches it. Layers sort rules within author CSS; the style attribute is resolved outside that comparison in both the normal and important cases.
saying these in an interview costs you the question
- Says !important always wins regardless of layers
- Assumes important declarations follow the same later-wins layer order
- Thinks unlayered !important is the strongest author declaration
- Claims !important removes a rule from its layer
- Believes only specificity separates two important declarations in different layers