← Back to Chapters

CSS !important Rule

⚠️ CSS !important Rule

? Quick Overview

The CSS !important rule is used to give a specific property the highest priority in the cascade. When multiple CSS rules target the same element and property, the one marked with !important will win—even over inline styles or more specific selectors. Because it is so powerful, it should be used carefully, only when you really need to force a style.

? Key Concepts

  • Overrides normal cascade: !important beats normal rules, even if they are more specific.
  • Inline vs external: A rule with !important can override inline styles without !important.
  • Specificity still matters: If two rules both use !important, the one with higher specificity wins.
  • Harder to debug: Overusing !important can make your CSS confusing and difficult to maintain.
  • Best used rarely: Good CSS architecture (proper selectors, ordering, and components) usually removes the need for !important.

? Rule Priority Snapshot

  • Normal external/internal CSS: Lowest priority
  • Inline CSS (style=""): Higher priority than normal CSS
  • External/internal with !important: Very high priority
  • Inline with !important: Highest priority (if specificity is equal)

? Syntax & Theory

The !important keyword is written at the end of a property value, just before the semicolon. It works with any CSS property and tells the browser, “apply this value even if other rules try to override it.”

? View Code Example
/* Basic syntax for the !important rule */
selector {
property: value !important;
}

Under the hood, the browser still uses the normal cascade and specificity rules, but !important acts like an extra boost. When multiple !important rules apply to the same property, specificity and source order are used to break the tie.

? Example 1 — Overriding a Normal Rule

In this example, both rules target the same paragraph. The second rule uses !important, so it wins even though the first rule appears later in the stylesheet.

? View Code Example
/* First rule: sets paragraph text to blue */
p {
color: blue;
}

/* Second rule: !important makes the text red instead */
p {
color: red !important;
}

? What You Will See

Even though the first rule sets the text to blue, the paragraph will appear red in the browser because the second rule uses !important.

? Example 2 — Overriding Inline Style

Normally, inline styles have higher priority than external or internal stylesheets. But a stylesheet rule with !important can even override inline styles that do not use !important.

? View Code Example
/* Stylesheet rule with !important */
p {
color: red !important;
}

<!-- HTML with inline style trying to set blue color -->
<p style="color: blue;">This paragraph will look red when the CSS above is applied.</p>

?️ Live Output Concept

In this standalone page, this text is blue because only the inline style is active. In a real project, if you also add the red !important rule from the code example, the paragraph would be rendered in red despite the inline style="color: blue;".

⚔️ Example 3 — When Two !important Rules Clash

If two rules for the same element and property both use !important, then specificity and order in the stylesheet decide the winner.

? View Code Example
/* Class selector with !important */
.title {
color: green !important;
}

/* ID selector with !important (higher specificity) */
#mainTitle {
color: orange !important;
}

<!-- HTML element using both the ID and class -->
<h1 id="mainTitle" class="title">CSS !important Example</h1>

? Explanation

Both rules use !important, but the ID selector #mainTitle is more specific than the class selector .title. So the heading text will be orange.

? Smart Use of !important

  • Use !important only when you cannot change the original CSS (e.g., third-party libraries or legacy code).
  • Prefer fixing selector specificity and stylesheet order before reaching for !important.
  • Keep all !important rules documented or grouped so others know why they exist.
  • To override an existing !important rule, use another !important rule with higher specificity or place it later in the stylesheet.
  • Avoid using !important in global selectors like body or *, as that can break theming and reuse.

? Practice Tasks

  1. Create two CSS rules for a heading: one normal and one with !important. Change colors and see which one applies.
  2. Write an inline style on a paragraph, then use an external CSS file with !important to override the inline style.
  3. Design a button where the background color can only be changed by editing a single !important rule.
  4. Experiment with two !important rules (one using a class, one using an ID) and observe how specificity decides the winner.