TExcelTAD
Home › CSS › CSS !important

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.

css
.note { color: gray !important; }

How it works

html · try it yourself
<!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.

html · try it yourself
<!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.

html · try it yourself
<!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.
html · try it yourself
<!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?

  1. Blue
  2. Red
  3. Purple
  4. Black

If two conflicting declarations are both !important, how is the winner chosen?

  1. The first one wins
  2. Specificity, then source order, among the important ones
  3. The browser picks randomly
  4. Neither applies

Which is a reasonable use of !important?

  1. Every rule in a stylesheet
  2. A utility class like .hidden { display: none !important; }
  3. Fixing any rule that doesn't apply
  4. Making selectors faster