Sub topic 12
The !important flag
This chapter assumes you can calculate specificity as an (IDs, classes, types) triple and that you know source order breaks ties between equal rules.
What it does
Adding !important after a value moves that declaration into a higher-priority group. The browser compares importance before it looks at specificity.
#box { color: blue; }
.box { color: red !important; }An element with id="box" and class="box" is red. The ID selector has specificity (1,0,0) and the class selector has (0,1,0), but the important declaration wins before specificity is compared.
Where it sits in the cascade
The browser compares competing declarations in this order and stops at the first step that picks a winner:
- Importance and origin. Important declarations beat normal ones.
- Cascade layers.
- Specificity.
- Source order.
Two important declarations on the same element are still compared against each other. Specificity and source order decide between them as usual.
Importance reverses layer order
For normal declarations, a later layer beats an earlier one. For important declarations the order flips, so the earlier layer wins.
@layer base, overrides;
@layer base {
a { color: red !important; }
}
@layer overrides {
a { color: blue !important; }
}Links are red here. Important declarations in layers also beat important declarations that are not in any layer. This reversal lets a foundational layer, such as a reset, protect its important rules from later layers.
Common errors
- Using it to win a specificity fight. The next person needs a second
!importantwith higher specificity. Repeated escalation makes the stylesheet hard to change. - Expecting it to affect one property in a shorthand.
margin: 0 !importantmarks every longhand it sets (margin-top,margin-right, and so on) as important. - Putting it in the wrong place. The flag goes after the value and before the semicolon.
color: red; !importantis invalid. - Forgetting that inline styles lose to it. A rule with
!importantoverrides a normalstyle="..."attribute. This is one of its few justified uses.
Legitimate uses
- Utility classes such as
.hidden { display: none !important; }. They must win wherever they are applied. - Overriding inline styles you cannot edit, for example those written by a third-party script.
- Accessibility or user overrides. User stylesheets can use
!importantto enforce larger text or high contrast.
Alternatives
Try these before reaching for the flag:
- Add one class to the selector, for example
.card .titleinstead of.title. - Write the base rules with
:where()so they contribute zero specificity and are easy to override. - Place third-party CSS in an early
@layerso your unlayered rules win without extra specificity.
Exercise
Given p.note { color: green !important; } followed later by #main p.note { color: purple !important; }, both targeting the same paragraph inside #main, what color is the text and why?
Show answer
Purple. Both declarations are important, so importance does not separate them. Specificity then decides: (1,1,1) beats (0,1,1). Source order is only a tie-breaker and is not needed here.