Sub topic 3
How `!important` Reverses Layer Order
Assume you know CSS selectors, specificity, source order, stylesheet imports, and how to inspect rules in DevTools. This chapter focuses on layer precedence and common debugging mistakes.
Important declarations reverse layer order
For normal declarations, a later layer wins. For !important declarations, the order reverses: an important declaration in an earlier layer wins over one in a later layer.
Important declarations in named layers also beat important declarations outside all layers. This protects foundational rules such as accessibility or security overrides.
@layer base, overrides;
@layer base {
button {
color: red !important;
}
}
@layer overrides {
button {
color: blue !important;
}
}
/* Buttons are red. */The base layer comes first, so its important declaration has higher priority.
Containing third-party CSS
Import external CSS into an early layer. Your later site layers can then override it without competing on specificity.
@layer vendor, site;
@import url("framework.css") layer(vendor);
@layer site {
.button {
border-radius: 0.25rem;
}
}The @layer order statement should appear before the import. Keep your own unlayered rules out of the project unless you deliberately want them to outrank normal layered rules.
Nested layers and dot notation
A layer can contain child layers. Dot notation refers to a nested layer directly.
@layer framework {
@layer base, theme;
@layer base {
p {
margin: 0;
}
}
@layer theme {
p {
color: #333;
}
}
}
@layer framework.theme {
p {
color: navy;
}
}The second framework.theme block reopens the existing nested layer. It does not create a separate layer.
Rolling back with `revert-layer
revert-layer removes the current layer's contribution for a property. The browser then uses the value from an earlier layer.
@layer base, components;
@layer base {
button {
padding: 1rem;
}
}
@layer components {
button {
padding: 2rem;
}
button.plain {
padding: revert-layer;
}
}A .plain button receives 1rem from base. This differs from revert, which can roll back beyond author styles.
Anonymous layers
An anonymous layer has no name:
@layer {
.legacy {
color: gray;
}
}It still participates in layer order, but you cannot reopen or reference it later. Use anonymous layers for isolated, one-time groups. Use named layers when other files must add rules to the same layer.
Debugging common pitfalls
- Check the layer column in DevTools before comparing specificity.
- Confirm the first layer-order statement. Later statements append new layers.
- Remember that unlayered normal rules beat layered normal rules.
- Remember that important layer order is reversed.
- Check whether an import was actually placed inside the intended layer.
- Inspect nested names such as
framework.themerather than treating them as unrelated layers.
Exercise
A vendor layer contains .card { color: black !important; }, and a later site layer contains .card { color: red !important; }. Which color wins?
Show answer
Black wins because important declarations reverse the layer order, so the earlier vendor layer has priority.