CSS !important
Adding !important after a value makes that declaration override all normal declarations for the same property on the same element — regardless of specificity or source order.
.note { color: gray !important; }How it works
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; font-size: 18px; }
#special.box { background: #e53935; } /* (1,1,0) - very specific */
div { background: #43a047 !important; } /* (0,0,1) but important */
.box { padding: 16px; color: white; }
</style>
</head>
<body>
<div id="special" class="box">I'm green — !important beats the ID selector.</div>
<div class="box" style="background: #1e88e5;">Inline style loses too: still green.</div>
</body>
</html>Even the inline style attribute loses against an !important rule in a stylesheet.
When two rules are both !important
If several declarations are important, the normal rules apply again among them: higher specificity wins, then later source order.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
p { color: #e65100 !important; }
.msg { color: #6a1b9a !important; }
p { font-size: 20px; }
</style>
</head>
<body>
<p class="msg">Purple: both are !important, so the more specific .msg wins.</p>
<p>Orange: only the p rule applies here.</p>
</body>
</html>The full priority order
From weakest to strongest (simplified):
| Priority | Source |
|---|---|
| 1 | Browser default styles |
| 2 | Normal author styles (your CSS) — decided by layers, specificity, order |
| 3 | Inline style attributes |
| 4 | Running CSS animations |
| 5 | !important author styles |
| 6 | !important user styles (accessibility settings) |
| 7 | !important browser styles |
| 8 | CSS transitions in progress |
Notice that user !important styles beat author !important ones. This lets people with low vision force, for example, larger text or high-contrast colours on every site.
Why it's usually a bad idea
!important breaks the natural cascade. Once you use it, the only way to override that declaration is with another !important that is more specific or later — an escalation that makes CSS hard to maintain.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.btn { padding: 10px 16px; border: none; border-radius: 6px; color: white; background: #757575 !important; margin: 4px; }
/* Later we want a primary variant... */
.btn-primary { background: #1e88e5; } /* doesn't work */
.btn.btn-danger { background: #e53935 !important; } /* forced to escalate */
</style>
</head>
<body>
<button class="btn">Default</button>
<button class="btn btn-primary">Primary (stuck grey)</button>
<button class="btn btn-danger">Danger</button>
</body>
</html>Tip: Before reaching for
!important, check why your rule loses — DevTools shows the winning rule. Usually the fix is a slightly more specific selector, reordering rules, or reducing specificity of the other rule.
Legitimate uses
There are a few good reasons to use it:
- Utility classes that must always do exactly one thing, such as
.hidden { display: none !important; }or.visually-hidden. - Overriding third-party CSS or inline styles you can't change (e.g. from a widget or CMS).
- User stylesheets and accessibility overrides.
- Print stylesheets that must override screen styles.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.card { display: flex; gap: 8px; padding: 10px; background: #fff8e1; margin: 6px 0; border-radius: 8px; }
.hidden { display: none !important; }
</style>
</head>
<body>
<div class="card">Visible card</div>
<div class="card hidden">You can't see me: .hidden must win over .card's display: flex</div>
<div class="card">Another visible card</div>
</body>
</html>Test yourself
A rule p { color: red !important; } and an inline style="color: blue" both target a paragraph. What colour is it?
- Blue
- Red
- Purple
- Black
If two conflicting declarations are both !important, how is the winner chosen?
- The first one wins
- Specificity, then source order, among the important ones
- The browser picks randomly
- Neither applies
Which is a reasonable use of !important?
- Every rule in a stylesheet
- A utility class like
.hidden { display: none !important; } - Fixing any rule that doesn't apply
- Making selectors faster