!important CSS Declarations: How and When to Use Them

Advertisement

When the CSS1 specification1 was drafted in the mid to late 90s, it introduced !important declarations that would help developers and users easily override normal specificity when making changes to their stylesheets. For the most part, !important declarations have remained the same, with only one change in CSS2.1 and nothing new added or altered in the CSS3 spec in connection with this unique declaration.

Adding !important in Developer Tools

Let’s take a look at what exactly these kinds of declarations are all about, and when, if ever, you should use them.

A Brief Primer on the Cascade

Before we get into !important declarations and exactly how they work, let’s give this discussion a bit of context. In the past, Smashing Magazine has covered CSS specificity2 in-depth, so please take a look at that article if you want a detailed discussion on the CSS cascade and how specificity ties in.

Below is a basic outline of how any given CSS-styled document will decide how much weight to give to different styles it encounters. This is a general summary of the cascade3 as discussed in the spec:

  • Find all declarations that apply to the element and property
  • Apply the styling to the element based on importance and origin using the following order, with the first item in the list having the least weight:
    • Declarations from the user agent
    • Declarations from the user
    • Declarations from the author
    • Declarations from the author with !important added
    • Declarations from the user with !important added
  • Apply styling based on specificity, with the more specific selector “winning” over more general ones
  • Apply styling based on the order in which they appear in the stylesheet (i.e., in the event of a tie, last one “wins”)

With that basic outline, you can probably already see how !important declarations weigh in, and what role they play in the cascade. Let’s look at !important in more detail.

Syntax and Description

An !important declaration provides a way for a stylesheet author to give a CSS value more weight than it naturally has. It should be noted here that the phrase “!important declaration” is a reference to an entire CSS declaration, including property and value, with !important added (thanks to Brad Czerniak4 for pointing out this discrepancy). Here is a simple code example that clearly illustrates how !important affects the natural way that styles are applied:

#example {
	font-size: 14px !important;	
}

#container #example {
	font-size: 10px;
}

In the above code sample, the element with the id of “example” will have text sized at 14px, due to the addition of !important.

Without the use of !important, there are two reasons why the second declaration block should naturally have more weight than the first: The second block is later in the stylesheet (i.e. it’s listed second). Also, the second block has more specificity (#container followed by #example instead of just #example). But with the inclusion of !important, the first font-size rule now has more weight.

Some things to note about !important declarations:

  • When !important was first introduced in CSS15, an author rule with an !important declaration held more weight than a user rule with an !important declaration; to improve accessibility, this was reversed in CSS26
  • If !important is used on a shorthand property, this adds “importance” to all the sub-properties that the shorthand property represents
  • The !important keyword (or statement) must be placed at the end of the line, immediately before the semicolon, otherwise it will have no effect (although a space before the semicolon won’t break it)
  • If for some particular reason you have to write the same property twice in the same declaration block, then add !important to the end of the first one, the first one will have more weight in every browser except IE6 (this works as an IE6-only hack, but doesn’t invalidate your CSS)
  • In IE6 and IE7, if you use a different word in place of !important (like !hotdog), the CSS rule will still be given extra weight, while other browsers will ignore it

When Should !important Be Used?

As with any technique, there are pros and cons depending on the circumstances. So when should it be used, if ever? Here’s my subjective overview of potential valid uses.

Never

!important declarations should not be used unless they are absolutely necessary after all other avenues have been exhausted. If you use !important out of laziness, to avoid proper debugging, or to rush a project to completion, then you’re abusing it, and you (or those that inherit your projects) will suffer the consequences.

If you include it even sparingly in your stylesheets, you will soon find that certain parts of your stylesheet will be harder to maintain. As discussed above, CSS property importance happens naturally through the cascade and specificity. When you use !important, you’re disrupting the natural flow of your rules, giving more weight to rules that are undeserving of such weight.

If you never use !important, then that’s a sign that you understand CSS and give proper forethought to your code before writing it.

That being said, the old adage “never say never” would certainly apply here. So below are some legitimate uses for !important.

To Aid or Test Accessibility

As mentioned, user stylesheets can include !important declarations, allowing users with special needs to give weight to specific CSS rules that will aid their ability to read and access content.

A special needs user can add !important to typographic properties like font-size to make text larger, or to color-related rules in order to increase the contrast of web pages.

In the screen grab below, Smashing Magazine’s home page is shown with a user-defined stylesheet overriding the normal text size, which can be done using Firefox’s Developer Toolbar:

User Style Sheet Added to Smashing Magazine

In this case, the text size was adjustable without using !important, because a user-defined stylesheet will override an author stylesheet regardless of specificity. If, however, the text size for body copy was set in the author stylesheet using an !important declaration, the user stylesheet could not override the text-size setting, even with a more specific selector. The inclusion of !important resolves this problem and keeps the adjustability of text size within the user’s power, even if the author has abused !important.

To Temporarily Fix an Urgent Problem

There will be times when something bugs out in your CSS on a live client site, and you need to apply a fix very quickly. In most cases, you should be able to use Firebug or another developer tool to track down the CSS code that needs to be fixed. But if the problem is occurring on IE6 or another browser that doesn’t have access to debugging tools, you may need to do a quick fix using !important.

After you move the temporary fix to production (thus making the client happy), you can work on fixing the issue locally using a more maintainable method that doesn’t muck up the cascade. When you’ve figured out a better solution, you can add it to the project and remove !important — and the client will be none the wiser.

To Override Styles Within Firebug or Another Developer Tool

Inspecting an element in Firebug or Chrome’s developer tools allows you to edit styles on the fly, to test things out, debug, and so on — without affecting the real stylesheet. Take a look at the screen grab below, showing some of Smashing Magazine’s styles in Chrome’s developer tools:

Overriding Styles in Chrome's Developer Tools

The highlighted background style rule has a line through it, indicating that this rule has been overridden by a later rule. In order to reapply this rule, you could find the later rule and disable it. You could alternatively edit the selector to make it more specific, but this would give the entire declaration block more specificity, which might not be desired.

!important could be added to a single line to give weight back to the overridden rule, thus allowing you to test or debug a CSS issue without making major changes to your actual stylesheet until you resolve the issue.

Here’s the same style rule with !important added. You’ll notice the line-through is now gone, because this rule now has more weight than the rule that was previously overriding it:

Adding !important in Developer Tools

To Override Inline Styles in User-Generated Content

One frustrating aspect of CSS development is when user-generated content includes inline styles, as would occur with some WYSIWYG editors in CMSs. In the CSS cascade, inline styles will override regular styles, so any undesirable element styling that occurs through generated content will be difficult, if not impossible, to change using customary CSS rules. You can circumvent this problem using an !important declaration, because a CSS rule with !important in an author stylesheet will override inline CSS.

For Print Stylesheets

Although this wouldn’t be necessary in all cases, and might be discouraged in some cases for the same reasons mentioned earlier, you could add !important declarations to your print-only stylesheets to help override specific styles without having to repeat selector specificity.

For Uniquely-Designed Blog Posts

If you’ve dabbled in uniquely-designed blog posts7 (many designers take issue8 with using “art direction” for this technique, and rightly so), as showcased on Heart Directed9, you’ll know that such an undertaking requires each separately-designed article to have its own stylesheet, or else you need to use inline styles. You can give an individual page its own styles using the code presented in this post10 on the Digging Into WordPress blog.

The use of !important could come in handy in such an instance, allowing you to easily override the default styles in order to create a unique experience for a single blog post or page on your site, without having to worry about natural CSS specificity.

Conclusion

!important declarations are best reserved for special needs and users who want to make web content more accessible by easily overriding default user agent or author stylesheets. So you should do your best to give your CSS proper forethought and avoid using !important wherever possible. Even in many of the uses described above, the inclusion of !important is not always necessary.

Nonetheless, !important is valid CSS. You might inherit a project wherein the previous developers used it, or you might have to patch something up quickly — so it could come in handy. It’s certainly beneficial to understand it better and be prepared to use it should the need arise.

Do you ever use !important in your stylesheets? When do you do so? Are there any other circumstances you can think of that would require its use?

Further Resources

Footnotes

  1. 1 http://www.w3.org/TR/REC-CSS1-961217
  2. 2 http://www.smashingmagazine.com/2010/04/07/css-specificity-and-inheritance/
  3. 3 http://www.w3.org/TR/CSS21/cascade.html#cascade
  4. 4 http://www.smashingmagazine.com/2010/11/02/the-important-css-declaration-how-and-when-to-use-it/#comment-491663
  5. 5 http://www.w3.org/TR/REC-CSS1-961217#important
  6. 6 http://www.w3.org/TR/CSS2/cascade.html#important-rules
  7. 7 http://www.smashingmagazine.com/the-death-of-the-blog-post/
  8. 8 http://www.alistapart.com/articles/art-direction-and-design/
  9. 9 http://heartdirected.com/
  10. 10 http://digwp.com/2010/02/custom-css-per-post/
  11. 11 http://www.w3.org/TR/CSS2/cascade.html#important-rules
  12. 12 http://reference.sitepoint.com/css/importantdeclarations
  13. 13 http://www.smashingmagazine.com/2010/04/07/css-specificity-and-inheritance/
  14. 14 http://www.impressivewebs.com/everything-you-need-to-know-about-the-important-css-declaration/
  15. 15 http://stackoverflow.com/questions/3706819/what-are-the-implications-of-using-important-in-css/

↑ Back to top Tweet itShare on Facebook

Louis Lazaris is a freelance web developer and author based in Toronto, Canada. He blogs about front-end code on Impressive Webs and curates Web Tools Weekly, a weekly newsletter for front-end developers.

Advertising
  1. 1

    Thank you for given main idea ./

    0
  2. 102

    Thanks, you just saved me :)

    0
  3. 203

    I wrote a little jQuery animation for an HTML div with CSS min- and max-height properties and overflow:auto. Unfortunately, calling the jquery animate() method seems to reset the overflow of this div to hidden. The simplest and probably the best way to fix this is to force the animation to leave the overflow at it’s original setting by specifying overflow:auto !important

    -1
  4. 304

    I really don’t like the !important syntax.

    Whoever first invented “!important” for overwriting CSS definitions in CSS1 wasn’t a programmer. Why? In many popular programming languages (perl,javascript,php,..) “!something” means “not something”. So whenever I switch to CSS coding !important reads for me like “not important ” or “This CSS part is not important.” BAM!

    Logically weird and I don’t like such stupid productive thinking stoppers in my mind.

    Was the first author and the collegues in the CSS1 consortium accepting it from spain because in their languages they use inverted “!”-sign in front of sentences like “!blabla blabla!” and in questions “?blabla blabla?” the inverted question character?

    Further this syntax is redundant:

    ! means in sentences imporant.
    important means -well- imporant. ;-)

    So it is “important important”.

    Stupid, huh?!

    0
  5. 405

    I know this article is quite old, but today, one of the first times, I have a situation where I can not find a way around !important.
    My company created a module for different shop-systems (Magento, Shopware, XTCom.. etc.) and we deliver one single SDK for all of them.
    The idea is to have the same JS-Architecture (A BackboneJS APP) and CSS (except of minor adjustments) for all shop-systems to make it maintainable.

    Now, all shop-systems have different styles of written CSS and (of course) some selectors overwrite the ones of my module-CSS.
    If I would only write for one shop-system, I could just extend the selectors, to overwrite the shop-CSS.
    But, as selectors for different shop-systems are different, this is not a solution at this time.

    So I guess, here we have one of these less cases for !important usage which makes sense (still I don’t like it though).

    0
  6. 506

    The most confusing thing about the !important declaration to me is the placement of the exclamation point. I have a programming background and I read this as NOT important. lol

    0
  7. 607

    If you have long statements of css and you need to override a single rule, e.g. you have a set of rules that defines some buttons, and now you use these styles on another page, where you need to center align a button, then it is very smart to use !important as this will overrule only a single value e.g:

    margin: 10px 0 0 25px !important;

    0
  8. 708

    I kind of disagree with some of the user comments. I am developing on wordpress and I don’t have control over some things (styles just seem to get overridden by either themes or plugins) and therefore I can’t apply what I want for specific elements.

    I know the article does say don’t ever use it unless you have no other option (which I didn’t, of course, so I fall under that category) but I think people need to be a bit more open minded. A theme I was using would display a logo in the nav menu, I removed the logo and it then displayed the websites title in the nav menu (something I also didn’t want). I found the class being applied and did a display: none; and that was overridden by something else that was more important apparently.

    The !important tag for my display none saved me a bunch of time of perhaps having to manually download and edit the theme and its css myself then reuploading it.

    Basically all I’m saying is everything has its purpose, I think the author knows this, although maybe he would still rather prefer to do it the long way rather than using !important.

    0
  9. 809

    I’ve had to use it recently to override a framework’s baked in CSS (Bootstrap).

    I supposed Bootstrap wasn’t really a thing in 2010, but I’d imagine overriding a CSS framework’s ever-so-specific styles is probably a normal use-case these days.

    0
  10. 910

    It was my understanding that !important should be used very sparingly. I’ve found it useful for print pages where certain elements get stripped out that I don’t want printed.

    0
  11. 1011

    This is a work of art. The article is very informative and I am sure this will be a lot of help especially those who are in this kind of field. Keep it up!

    0

↑ Back to top