Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 139 ยท The Cascade

!important

Learn exactly how !important changes the CSS cascade, why it reverses origins and layers, the few cases where it is the right tool, and safer alternatives.

Adding !important to the end of a declaration makes it jump ahead of normal declarations in the cascade. It is the CSS equivalent of shouting, and like shouting it works, at first. Then someone shouts back, and soon the whole stylesheet is shouting.

That doesn't mean !important is evil. It has a precise meaning and a few legitimate uses, and it plays a special role in protecting users and in cascade layers. In this lesson you will learn exactly what it does, why its behavior "reverses" in some places, and what to reach for instead.

The syntax

Put !important after the value, before the semicolon:

CSS
.notice {
  display: none !important;
}

It applies to that one declaration only, not to the whole rule. The ! is a delimiter, not a "not" operator: !important means "this is important", not "this is not important". Spaces between ! and important are allowed but unusual, and the keyword is case-insensitive.

What it actually does

Remember the first question the cascade asks: origin and importance. Each declaration is either normal or important, and important declarations are sorted into their own, stronger group. An important author declaration beats every normal author declaration, no matter how specific the normal one is, and even if the normal one is in a style attribute.

Important beats specificity and inline styles

Within the important group, the usual rules continue. If two important declarations conflict, the browser compares their layers, then their specificity, then their order, just as it does for normal declarations.

Specificity still matters between important rules

This is the escalation problem in a nutshell. Once one rule uses !important, the only way to override it is another !important with equal or higher priority. And so it spreads.

Importance reverses the origins

The cascade has three origins: the browser (user-agent), the user and you (the author). For normal declarations, author beats user beats user-agent. For important declarations, the order is reversed:

  1. Important user-agent declarations (strongest)
  2. Important user declarations
  3. Important author declarations
  4. Normal author declarations
  5. Normal user declarations
  6. Normal user-agent declarations (weakest)

Why reverse it? So people can protect their needs. Someone with low vision may set a user style such as * { font-size: 24px !important; } through a browser extension or setting. Because important user styles beat important author styles, no website can override it, however many !importants the site uses. The browser's own important declarations sit at the very top for the same reason: a handful of behaviors must never be overridden by pages.

(For completeness, two things sit outside this list. Running CSS animations override all normal declarations, but lose to important ones. Running transitions beat everything while they play.)

Importance reverses the layers

Cascade layers follow the same logic. For normal declarations, later layers beat earlier ones, and unlayered styles beat all layers. For important declarations, it flips: earlier layers win, and important declarations in any layer beat important unlayered ones.

!important reverses layer order

The box is purple. All three declarations are important, so the order reverses: the reset layer, declared first, is now the strongest, and the unlayered declaration is the weakest. Remove !important from all three and the result flips to blue, because unlayered normal styles beat layered ones.

This reversal is deliberate. A reset or base layer can mark a few declarations as truly essential, and later, more specific layers cannot override them by accident.

Legitimate uses

There are a few situations where !important is the honest tool for the job:

  • Utility classes that must always win. A class like .visually-hidden or .hidden exists to do one thing, everywhere. If .hidden lost to some component's display: flex, it would be useless.
  • User styles and accessibility tools. Browser extensions and user stylesheets rely on it, as described above.
  • Overriding third-party CSS you can't edit, such as a widget that injects inline styles. Try layers or a more specific selector first, but sometimes !important is the only option.
  • Respecting user preferences, for example forcing animations off inside @media (prefers-reduced-motion: reduce), regardless of what individual components set.
A utility class that always wins

Without !important, adding .hidden to #main-toolbar would do nothing if a more specific rule set display. With it, the class works on any element, which is exactly what a utility promises.

Why to avoid it elsewhere

Using !important to fix an ordinary conflict causes problems later:

  • It is hard to override. The next person must use !important too, with a heavier selector or a later position.
  • It hides the real problem. The rule you are fighting is usually too specific or in the wrong place. Fix that instead.
  • It breaks expectations. Readers of your CSS expect specificity and order to decide; an !important far away can make a nearby rule mysteriously fail.
  • JavaScript can't win easily. element.style.color = "red" sets a normal inline style, which loses to any important author rule.

Better alternatives

Before you type !important, try these:

  1. Check that your selector matches and is valid. Use DevTools.
  2. Lower the specificity of the rule that wins, perhaps with :where(). See Specificity.
  3. Move your rule later in the stylesheet, or load your stylesheet after the library.
  4. Put the library in a cascade layer. Unlayered styles beat all layered ones, so @import url("library.css") layer(library); makes any of your normal rules win.
  5. Match the existing specificity with a single extra class rather than escalating to IDs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. An important declaration uses the selector p. A normal declaration uses #intro and targets the same paragraph. Which wins?

    Importance is part of the very first cascade step. Specificity is only compared between declarations of the same importance.
  2. 2. A site uses font-size: 14px !important. A user style sets font-size: 24px !important. Which wins?

    For important declarations the order is reversed, so important user styles beat important author styles. This protects user needs.
  3. 3. @layer base, theme; Both layers contain an important color declaration for the same element. Which wins?

    For normal declarations later layers win; for important declarations earlier layers win.
  4. 4. Which of these is a legitimate use of !important?

    Single-purpose utility classes are expected to win everywhere. Ordinary component conflicts should be solved with specificity, order or layers.

Summary

  • !important moves a declaration into a stronger group that beats all normal declarations, including inline styles.
  • Between important declarations, layers, specificity and source order still apply.
  • Importance reverses origin order (user beats author) and layer order (earlier layers win, layered beats unlayered).
  • The reversal protects users and lets base layers lock down essential rules.
  • Good uses: utility classes, accessibility overrides, user styles, unavoidable third-party conflicts.
  • Elsewhere, fix specificity, order or layering instead of escalating.

Next: Cascade Layers โ€” organizing your CSS so conflicts resolve the way you intend.