CSS !important

Within the world of cascading styles, there will be occasions where we encounter unexpected scenarios where styles are prevented from applying, no matter how diligent our efforts are. CSS !important is a valuable tool to override derivative rules – and this should be used with care, restraint, and in moderation.

What is !important in CSS?

The !important declaration signals that a style rule operates at the ultimate priority. When a style is given !important, it will override any other style declarations - even inline styles or more specific selectors - unless they also have an !important.


          p {
             color: red !important;
          }
     

In this example, the paragraph text will be red, regardless of any other color rules assigned to it anywhere in the stylesheet or HTML.

Why Use !important?

There are occasions where !important has value, such as:

Example: Overriding a Framework Style

Let's say a button class from a CSS framework is automatically giving your button a gray background but you want it to be blue:


          .btn {
              background-color: gray ;  /* From Framework */
          }

          .custom-btn {
              background-color: blue !important ;
          }

Now, adding !important to .custom-btn class allows you to ensure that your rule will prevail against the framework's styling rules.

When Not to Use !important

While it's tempting to use !important to force styles, doing so can cause more harm than good in large projects:

Instead, aim for better specificity, cleaner selectors, or restructuring styles properly.

Specificity vs. !important

CSS uses specificity to determine which style, to apply. Here's the order:

  1. Browser default
  2. External stylesheet
  3. Internal stylesheet
  4. Inline styles
  5. !important overrides all

So even if an inline style has higher specificity, a style marked with !important will win.

Overriding !important

The only way to override a style that uses !important is to use another rule with !important and a higher specificity.


           /* Original */
          p {
              color: red !important ;
          }

           /* Original */
          #main-content p {
                  color: blue !important ;
          }

In this case, the second rule applies because it more specificity (uses an ID).

Real World Use Cases

Here are some good examples of when !important could be appropriate:

Tips for Using !important Wisely

Conclusion

CSS !important is both a hero and villain. It can rescue you from style conflicts quickly, but if overused, it turns your stylesheets into a tangled mess. Use it wisely, and only when no better solution exists.A disciplined developer is aware of when to enforce rules over others, and when to just let the natural cascade take over.

So the next time you're stuck in a brawl with obnoxiously stubborn styles, ask yourself—do I really need !important, or is there a more dan intelligent way to resolve my conflict?

Try it Yourself