The !important
rule in CSS is used to add more importance to a property/value than normal.
In fact, if you use the !important
rule, it will override ALL previous styling rules for that specific property on that element!
The only way to override an !important
rule is to include another !important
rule on a declaration with the same (or higher) specificity in the source code – and here
the problem starts! This makes the CSS code confusing and the debugging will be hard, especially if you have a large style sheet!
Here we have created a simple example. It is not very clear, when you look at the CSS source code, which color is considered most important:
One way to use !important
is if you have to override a style that cannot be overridden in any other way.
This could be if you are working on a Content Management System (CMS) and cannot edit the CSS code.
Then you can set some custom styles to override some of the CMS styles.
!important
is: Assume you want a special look for all buttons on a page.
Here, buttons are styled with a gray background color, white text, and some padding and border:
The look of a button can sometimes change if we put it inside another element with higher specificity, and the properties get in conflict. Here is an example of this:
To "force" all buttons to have the same look, no matter what, we can add the !important rule to the properties of the button, like this: