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:
- Delete third-party or external stylesheets (via frameworks like Bootstrap)
- Quick fixes during debugging
- Forcing a specific style on dynamically injected content
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:
- It breaks the natural cascading and specificity rules of CSS
- It can create conflicts with other styles using
!important - It makes debugging harder over time
- It encourages poor CSS architecture
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:
- Browser default
- External stylesheet
- Internal stylesheet
- Inline styles
!importantoverrides 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:
- Overriding styles in CMS themes like WordPress or Shopify
- Styling email templates where inline styles are often enforced
- Adjusting legacy code where rewriting everything is not feasible
Tips for Using !important Wisely
- Use !important only when absolutely necessary
- Document your use of
!importantin comments - Always try and increase specificity first, prior to using
!important
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?