Recently I came across lot of code using CSS !important declaration. I had known that this declaration existed but I didn’t knew what it was and had never used it. So than I did my research on it and decided to write a post about it. In this article I will discuss what it is, how it is used, when to use it, etc
What is !important?
The !important is a key word which is added to the end of the CSS value to give it more weight. In CSS the rules are applied to elements in a cascading manner.
- Find all the declarations that apply to the element in question.
- The order in which the styles are applied based on importance and origin. The weight of style sheets are, from least weight to highest weight, first user agent styles, than user, than author style sheets, !important author styles and the height weight is for the !important user styles.
- The more specific a selector is, the higher weight it will get. e.g., a style on “div.main p” will have a higher precedence than one just on the “p” tag.
- The rules work in top to bottom order. Rules that are defined later in the document tree have higher precedence than those defined earlier. Also, if rules are imported from style sheet then they are considered before the rules which are directly in the style sheet.
The !important is used to force the style to an element and not be overridden by the cascading CSS. When the !important is used that rule gets more weight than it naturally has.
How to use !important?
The !important declaration is a keyword that can be added at the end of any CSS property/value.
#example{ color:#000 !important; } #container #example{ color:#666; }
In this example we have first have a rule to make the color of text for the element with id example to be black (#000) with !important. Then we have another rule to make the color of text for the element (#example) to be grey(#666). So now the color of the text of the element (#example) would be black and not grey because of the !important declaration. In case we didn’t use the !important then the color would have been grey.
When to use it?
I think the !important declarations should not be used unless its is absolutely necessary. Using !important breaks the natural cascading effect of the style sheets. It also will make you code difficult to maintain. I have never used !important declarations. However, there are certain conditions where you will have to use !important.
If users can add content using the WYSIWYG editors then they can format the content. Also, if you allow the users to add html code they can also specify inline styles to the text. However, each user can make it different and if would not be consistent with your site. So to prevent certain formatting (like background, text color, size etc) you can use !important in your style sheet and as the !important declaration in author style sheet overrides the inline style your content would be consistently displayed for all users-generated content.
If there is some CSS issue in your code and you want to make sure that the a particular style is applied you can use !important to temporarily fix the issue on the live site until you find out a better way(which might take some time). After adding the !important on live site you should work on modifying your rules in order to avoid !important and once you have the fix you can update the code on live site.
The !important declaration can also be used in a print style sheets to make sure that the styles will be applied and not overridden by anything else.
Conclusion
!important declarations should be avoided as long as possible until absolutely necessary and there is no other alternative. You should not use !important tags without giving it a forethought. It should be used only in special cases and not always. I personally have never had to use !important declarations.