Favorites

Understanding CSS !important: When and How to Use It

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
CSS Important

What does !important do?

When two CSS rules try to set the same property on the same element, the browser has to choose one. The process it uses to decide is called the cascade (the "C" in CSS). Adding !important to the end of a declaration tells the browser that this declaration should win over normal declarations, whatever else is going on:

selector {
  property: value !important;
}

For example:

.button {
  background-color: #be185d !important;
}

It applies to that one declaration only, not to the whole rule. Other declarations in the same rule behave normally.

Where !important fits in the cascade

To understand !important, it helps to know the main steps the cascade goes through. Simplified, the browser checks these in order, and moves to the next step only when there's a tie:

  1. Importance: is the declaration marked !important?
  2. Inline styles: was it set in the HTML style attribute?
  3. Cascade layers: which @layer is it in?
  4. Specificity: how specific is the selector? IDs count more than classes, and classes more than element names.
  5. Order: which rule comes last?

The first box below has two rules: a more specific one setting blue, and a less specific one setting pink. Specificity wins, so it's blue. In the second box, the less specific rule adds !important, and it wins:

/* More specific: wins in a normal contest */
.demo .swatch { background: #1d4ed8; }

/* Less specific, but important: wins anyway */
.swatch { background: #be185d !important; }
Results
Normal contest
The more specific rule wins.Blue: specificity wins
With !important
The important rule wins anyway.Pink: !important wins

Inline styles

Styles in an HTML style attribute normally beat any rule in a stylesheet. An !important declaration in a stylesheet beats a normal inline style, though:

<div class="swatch" style="background: #1d4ed8;">…</div>

.swatch { background: #be185d; }             /* loses to the inline style */
.swatch { background: #be185d !important; }  /* beats the inline style */
Results
Inline style vs normal rule
The inline style wins.Blue: inline style wins
Inline style vs !important
The important rule wins.Pink: !important wins

This is one of the few situations where !important is genuinely useful, as we'll see next.

When !important is a good idea

  • Utility classes that must always work. A class like .visually-hidden or .hidden has one job, and it should do it wherever it's used, regardless of other styles on the element:
    .hidden {
      display: none !important;
    }
  • Overriding inline styles you can't change. Some third-party widgets and scripts write styles straight into the style attribute. !important is the only way to override those from your stylesheet.
  • Respecting reduced motion. A global reset that switches off animations for people who've asked for less motion needs to beat every other animation rule on the site:
    @media (prefers-reduced-motion: reduce) {
      *,
      *::before,
      *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
      }
    }
  • Quick debugging. Temporarily adding !important can show whether a style isn't applying because something else is overriding it. Remove it once you've found the cause.

Good to know: people can apply their own styles to websites, through browser settings or extensions, for example to enlarge text or increase contrast. For !important declarations, the cascade deliberately reverses the usual priority, so a visitor's important styles beat yours. It's a safeguard that makes sure people can always adjust a site to suit their needs.

When to avoid it

The trouble with !important is that the only way to override it is with another !important. Once a stylesheet has a few, each new fix tends to need one too, until important declarations are everywhere and the cascade no longer works as intended. This is often called a specificity war.

  • Responsive design rarely needs it. The original version of this post suggested using !important for responsive styles. A media query's rules already override earlier rules with the same specificity, as long as the media query comes later in the stylesheet. If they don't apply, check the order and the selectors first.
  • Don't use it to fix your own code. If your own styles are fighting each other, simplifying the selectors or the order is a lasting fix. !important just hides the problem.
  • Don't use it in code other people will build on, such as a theme or component library. They'll be forced to use !important to customise it.

Better alternatives

Match the specificity, then rely on order

If your rule has the same specificity as the one you're overriding and comes later, it wins. Often the simplest fix is to copy the selector that's winning (your browser's developer tools show it) and put your rule after it.

Raise specificity a little

If you need a slightly stronger selector, you can add a parent class or repeat a class. Repeating a class is a neat trick: .button.button matches exactly the same elements as .button, but counts as two classes:

.button.button {
  background: #be185d;  /* beats any single-class .button rule */
}

Lower specificity with :where()

The :where() pseudo-class has zero specificity, however many selectors are inside it. It's perfect for default styles that should be easy to override later:

/* Specificity of zero: any class will override this */
:where(.card) h2 {
  color: #1f2937;
}

/* So this simple rule wins, without !important */
.title-accent {
  color: #1d4ed8;
}
SelectorSpecificity (IDs, classes, elements)
h20, 0, 1
.card h20, 1, 1
:where(.card) h20, 0, 1
:is(.card, #main) h21, 0, 1 (the most specific item inside counts)
.button.button0, 2, 0
#header .nav a1, 1, 1

Use cascade layers

Cascade layers, created with the @layer rule, are the modern answer to most !important problems. They let you group styles into layers and decide which layer wins, regardless of specificity:

/* The order here sets priority: later layers win */
@layer reset, library, components, utilities;

@layer library {
  #app .nav .button { background: #6b7280; }  /* very specific */
}

@layer components {
  .button { background: #1d4ed8; }  /* wins: it's in a later layer */
}

Even though the library rule has a much more specific selector, the components layer comes later, so its rule wins. You can also load third-party CSS straight into a layer, so your own styles can override it without any specificity battles:

@import url("library.css") layer(library);

Styles that aren't in any layer beat all layered styles, so existing CSS keeps working when you start using layers.

!important and cascade layers

There's one surprising rule to know. For normal declarations, later layers win. For !important declarations, the order reverses, and earlier layers win:

@layer base, theme;

@layer base {
  .swatch { background: #15803d !important; }  /* green: wins */
}

@layer theme {
  .swatch { background: #be185d !important; }  /* pink: loses */
}
Results
Normal declarations
The later layer (theme) wins.Pink: theme layer wins
!important declarations
The earlier layer (base) wins.Green: base layer wins

This is deliberate. It lets a foundational layer, such as a reset or accessibility layer, mark a few declarations as important and guarantee that nothing later can override them. The same reversal applies to styles outside any layer: their important declarations lose to important declarations inside layers.

Gotchas

  • It goes at the end of the value, before the semicolon: color: red !important;. Anywhere else, the whole declaration is ignored.
  • It's ignored inside @keyframes. Any declaration marked !important within a keyframe is thrown away.
  • Important declarations beat running animations. If an element has opacity: 1 !important, an animation can't change its opacity.
  • On a shorthand, it applies to every part. margin: 0 !important makes all four margins important.
  • An important inline style can't be overridden from a stylesheet. style="color: red !important" wins over every stylesheet rule on the page.
  • JavaScript has its own syntax. To set an important style from JavaScript, pass 'important' as a third argument: element.style.setProperty('color', 'red', 'important').

A quick checklist

Before reaching for !important, ask yourself:

  1. Can I find the rule that's winning in my browser's developer tools, and match or beat its selector?
  2. Could my rule simply come later in the stylesheet?
  3. Could :where() make the existing rule easier to override?
  4. Could cascade layers organise these styles so the right ones win?
  5. Is this a utility class, a reduced-motion reset, or an override for inline styles I can't change?

If you do use !important, add a comment explaining why, so the next person (or future you) knows it's deliberate:

.chat-widget {
  /* !important needed: the widget's script sets inline styles */
  bottom: 80px !important;
}

Wrapping up

!important doesn't add specificity. It moves a declaration into a separate, stronger tier of the cascade that's checked first. That makes it useful for utility classes, reduced-motion resets and overriding inline styles you can't change, but troublesome as an everyday fix. Understand specificity, use :where() and cascade layers to organise your styles, and save !important for the few cases that really need it.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee