CSS !important
- type
- declaration flag
- applies to
- any declaration
- inherits
- n/a
- common values
- property: value !important;
Where it goes
At the end of a declaration, before the semicolon. Honest uses: .hidden { display: none !important; }, overriding a widget's inline styles, print styles. Not for making a rule work.
Example
/* legitimate: a utility that must always win */
.visually-hidden { position: absolute !important; clip: rect(0 0 0 0) !important; }
/* smell: fix the selector instead */
.title { color: red !important; }Common mistakes
- Reaching for it when a rule does not apply. The rule is losing for a reason: wrong selector, lower specificity, an earlier rule. Find that. A better selector is the fix.
- Stacking it. Once one rule has !important, the next override needs it too, and the stylesheet becomes a war nobody wins. One is a smell. Three is a rewrite.
- Putting the flag before the value. It is value !important; with the flag last.