Sub topic 12
The !important Flag
This chapter assumes you can write basic CSS rules, know how class, ID, and type selectors work, and can calculate a specificity triple such as (1,1,1). It also assumes you know the cascade compares origin, specificity, and source order. The !important flag changes that comparison in ways that surprise many developers.
What !important does
!important is a flag you add to a single declaration, after the value and before the semicolon. It marks that declaration as important. Important declarations are compared with each other first, before specificity is considered at all.
.box { color: red !important; }The flag applies to one declaration, not the whole rule. Other declarations in the same rule stay normal.
.card {
color: red !important; /* important */
margin: 1rem; /* normal */
}Importance beats specificity
The cascade checks origin and importance before it checks specificity. A normal declaration therefore loses to an important one, even when the normal one has a much stronger selector.
#box { color: blue; }
.box { color: red !important; }The ID selector scores (1,0,0) and the class selector scores (0,1,0). The element still gets red text. Importance is decided first, and specificity is only used to break ties between declarations of the same kind.
Competing important declarations
When two important declarations conflict, the usual tie-breakers apply to them. Specificity decides first. If specificity is equal, source order decides.
#box { color: blue !important; } /* (1,0,0) */
.box { color: red !important; } /* (0,1,0) */Both are important, so the ID selector wins and the text is blue. In the next example the specificity is equal, so the later rule wins.
.alert { color: orange !important; }
.alert { color: crimson !important; }The text is crimson.
Origins reverse for important declarations
The cascade has three main origins:
- User-agent: the browser's default styles.
- User: styles set by the person using the browser, such as accessibility settings.
- Author: the styles you write in your stylesheets.
For normal declarations, author styles beat user styles, and user styles beat user-agent styles. For important declarations, the order is reversed. User-agent important declarations beat user important declarations, and user important declarations beat author important declarations.
| Rank | Normal declarations | Important declarations |
|---|---|---|
| Lowest | User-agent | Author |
| Middle | User | User |
| Highest | Author | User-agent |
The reversal exists so that a user who needs, for example, large text or high contrast can force it with !important. Your !important cannot override it. Using !important in your own stylesheet does not let you defeat a user's important settings.
Inline styles and !important
A style attribute normally beats any selector in a stylesheet, because inline styles outrank selector specificity. An important declaration in a stylesheet beats a normal inline declaration.
<p class="note" style="color: green;">Hello</p>.note { color: red !important; }The paragraph is red. Only an inline declaration that is also marked !important could beat this rule, which is why !important is a common way for stylesheets to override styles injected by scripts or third-party widgets.
Important declarations and cascade layers
Cascade layers, created with @layer, are covered in their own chapter. One detail belongs here because it involves !important. For normal declarations, a later layer beats an earlier one. For important declarations, the order of layers is reversed, so an earlier layer beats a later one.
@layer base, overrides;
@layer base {
p { color: navy !important; }
}
@layer overrides {
p { color: tomato !important; }
}Both declarations are important, so the earlier layer, base, wins and the text is navy. This lets a foundational layer, such as a reset or a design-system base, protect its important rules from being overridden by later layers.
Legitimate uses
Use !important sparingly. These cases are reasonable:
- Utility classes whose job is to always apply, such as
.hidden { display: none !important; }. The class name promises one behavior, so it should not lose to a component rule. - Overriding inline styles you cannot edit, such as those set by a third-party script.
- Print or accessibility stylesheets where a rule must hold regardless of the page's other styles.
- Short-lived debugging, to test whether a rule is the one causing a problem. Remove it afterward.
.hidden { display: none !important; }
.visually-hidden {
position: absolute !important;
width: 1px !important;
height: 1px !important;
overflow: hidden !important;
}Why overuse causes problems
An important declaration can only be overridden by another important declaration with equal or higher specificity, or by an earlier layer. Each use raises the bar for the next change, so a team that fixes one conflict with !important tends to need it again later.
.menu a { color: gray !important; }To change that link color later, you must either edit this rule or write a competing rule that is also important and has specificity of at least (0,1,1). That escalation is hard to reverse. Once many rules are important, the original problem has not gone away. The stylesheet has lost the normal ordering that made it predictable.
The usual causes of reaching for !important are:
- A selector that is more specific than necessary elsewhere in the stylesheet.
- Rules in the wrong order.
- Uncertainty about which rule is applying.
Fixing the cause is almost always better than adding the flag.
Common mistakes
- Writing the flag in the wrong place. The flag goes after the value:
color: red !important;. Writing!importantbefore the value, or omitting the semicolon before the next declaration, produces an invalid declaration that the browser drops. - Expecting it to affect inheritance. The flag does not make a value inherit. A child that has its own rule for the property still uses that rule, because inheritance loses to any direct rule on the child, important or not. The flag on a parent declaration only affects that parent's computed value.
- Applying it to shorthand without noticing the effect.
margin: 0 !important;marks all four longhand margin properties as important. Later rules formargin-topwill then fail unless they are also important. - Using it to fix an ordering problem. If the issue is that one rule appears too early, move the rule or restructure the selectors instead.
.panel { margin: 0 !important; }
.panel { margin-top: 2rem; } /* ignored: margin-top is already important */Alternatives to try first
Before adding the flag, consider these options:
- Move the rule later in the stylesheet when specificity is equal.
- Match the existing selector's specificity instead of exceeding it by a wide margin.
- Lower the specificity of the rule you want to override, for example by wrapping a selector in
:where(). - Put overridable defaults in an earlier cascade layer.
- Raise specificity modestly, for example from
.linkto.nav .link, instead of using an ID or the flag.
Finding important declarations in developer tools
In browser developer tools, the Styles pane lists matching rules and shows !important next to flagged declarations. A struck-through declaration is losing the cascade. If a crossed-out normal declaration sits below an important one for the same property, the flag is the reason. Searching your stylesheets for !important is also a quick way to audit how many forced rules exist.
Exercise
A page has #sidebar a { color: blue; } and .link { color: red !important; }. Both match the same anchor. What color is the link, and what changes if you also add #sidebar a { color: green !important; }?
Show answer
Without the third rule, the link is red, because the important declaration beats the normal one even though the ID selector has higher specificity. With the third rule, both declarations are important, so specificity decides. #sidebar a scores (1,0,1) and .link scores (0,1,0), so the link becomes green.
Exercise
An author stylesheet has @layer base, theme; with p { color: navy !important; } in base and p { color: tomato !important; } in theme. Which color does the paragraph get, and why is that different from the normal-declaration result?
Show answer
The paragraph is navy. For important declarations the layer order is reversed, so the earlier layer (base) wins. For normal declarations, the later layer (theme) would win.