Menu Search
Jump to the content X

!important CSS Declarations: How and When to Use Them


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 Link

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 Link

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? Link

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 Link

!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 Link

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 Link

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 Link

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 Link

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 Link

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 Link

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 Link

!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 Link

Footnotes Link

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15

↑ 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.

  1. 1

    Agree with Louis on this one, though could have a been a shorter article if Louis had simply said:
    DON’T. EVER.

  2. 2

    In my experience so far, the usage of !important can always be overcome with other measures. It’s a good article for those who are not familiar with the keyword, however for the more experienced readers it’s merely an average read.

  3. 3

    I use the !important declaration as a hack for IE6. IE6 doesn’t recognize !important. It also doesn’t work with transparent PNGs. So you can declare a transparent PNG as a background and make it important. Then you can re-declare the background as reasonable GIF replacement or nothing (if it’s an somewhat unnecessary drop shadow for example). IE6 will bypass !important and set the background to nothing. Well behaved browsers (which also support transparent PNGs) will show the PNG.

    But other than that, yeah, I’d say avoid it.

  4. 4

    This is one of the reasons I use !important sometimes, within the context of regular design:

    Another reason is generic classes that you can use in a pinch and be sure they’ll get applied, like:

    .last { margin-right: 0 !important; }

  5. 5

    Yep it’s definately useful for overriding inline CSS that you don’t have any control over.

    I also occasionally use it if I am using a class specifically for changing text colour. For example I’ll have .blue, and a .green, styles. I can add these to things to override any previously set colour. Makes it easy to change colours across the board at a later date too :)

  6. 6

    !important is awesome, it makes my styles work everywhere so I put it on everything. It even works in IE6.

  7. 7


    Like this?

    display:block !important;position:absolute;float:left;display:inline;color:blue;font-size:12pt;font-size:14pt!important;position: relative !important;width:400px;height:250px!important;line-height:1 !important;

    it’s still going left.

  8. 8

    Thanks, hacks are for chumps.

  9. 9

    Soooo no one uses the !important tag on an element with min-height to get ie6 to work (min-height: 300px, height: auto !important, height: 300px) ? Of course I now use the conditional comments to check for browser which then adds a class to the body, and target IE6 that way now. But before that, that was the only time I used the !important tag. I find it hard to believe no one did/does that anymore though.

  10. 10

    This is interesting. YUI CSS uses the * CSS hack for IE6 declarations, but the CSS doesn’t validate then. This seems like a good alternative (still a hack, but slightly better). I may have to make use of the !hotdog modifier, just too good to pass up.

  11. 11

    I personally use it all the time as a IE6 hack when using png images since IE6 ignores the declaration.

    background: transparent url(images/cool_image.png) top left no-repeat !important;
    background: transparent url(images/ie_image.gif) top left no-repeat;

    Makes it easy to have the cool png image with partial transparency and also display a less cool image to IE6 users.

  12. 12

    I agree for the most part that it should be used very little, bet as the article indicates, there are some situations that call for it. For example, I just did a small jQuery project for my corporate web site. I do not have control of all the CSS files and someone else had used it define all link hovers. I could not path to my link colors and change their hover color. So I added the !important to my specific link change to force my cascade to work correctly. Problem solved and I was specific enough to not impact the rest of the site.

    We tend to approach solutions from the egotistical point that we have complete control. On a very large site with thousands of pages, there are usually multiple people involved and you can’t go back and fix their code. So a rule I follow is to try not to use it if you are in control. Solve the problem correctly without the use of the hammer. If you have to use it, then be considerate and specific enough to not force other people coming after you to have to over use it to meet their design specifications as it can cause a long term cascade of its use until every style has to include it.

  13. 13

    The article missed the one thing I consistently use !important for: Chrome and its damn default form styles! Google, I will tell YOU what I want my forms to look like!

  14. 14

    Great article – !important feels like a hack when its being used, but once you’ve exhausted all avenues you just have to use it – especially when branding SharePoint, Microsoft loves their inline styles!

  15. 15

    Working with WordPress plug-ins, I have to admit sometimes this is the only way you can do it, without overriding the plugins styles, which can be erased after an upgrade.
    Otherwise, I avoid at all times!

  16. 16

    Nice. Never too old to learn. I’ve seen this property a million times in tutorials on how to hack browsers into compliancy but never realized that it has a designed purpose like this.

  17. 17

    Great article. I wish less people used the !important attribute=)

  18. 18

    I’ll have to second the SharePoint comment, and unless you want to re-write core.css to your exact specification and then get your changes overwrote with a service pack… you’ll just stick with !important

  19. 19

    @Its Adam – Good on you for checking out this guy. 13 instances of !important in two stylesheets on his Impresive Webs site, plus a 404 error for a linked stylesheet that doesn’t exist. Kinda sloppy work for someone lecturing on proper technique. There’s also one more !important on his Interviews site.

    I had forgotten about !important. I haven’t used it since the frustrating days of Myspace profile v1 with the horrible tug-of-war trying to get things to fit into that horrible code. I haven’t used !important in at least 4 years. I didn’t know anybody was still using it.

  20. 20

    Ahhh, the thing that made custom Myspace code possible…by putting it on EVERY line of CSS. *lol*

  21. 21

    actually, to expand on your point about Overriding Inline Content – if you think about an Inline CMS, the !important keyword can be very important. ;) Assuming you have the default styles for the CMS elements declared above the user styles, the user can unwittingly modify those default styles. Granted, the user could always write a rule with higher priority, but in most cases the !important tag can be a crucial fix for maintaining the look of your CMS elements.

  22. 22

    Thank you for that article! My gut feeling always told me to avoid this ;-)

  23. 23

    Awesome. How this great idea you get ? I think – !important – ‘s behavior very from browser to browser.

  24. 24

    It’s good for IFRAMES ! Where the CSS is included on other server… I try not to use the !important unless it’s veeeeeeryy important! :)

  25. 25

    I disagree…..I know that I’ve been forced to use !important on stylesheets because I was skinning Microsoft SharePoint which has some very “closed” code. It will often render inline styles in its HTML, and the only way to override the style is to use this method. If there was no point in having it, it probably wouldn’t be supported by CSS3 and the browsers.

  26. 26

    In my experience, you should only ever use it to override horrible, inline CSS that has been placed there normally by a rubbish ASP.NET back-end.

  27. 27

    I admit I’m abit of a pain for using the !important alot.. really does vary from job to job and depending what it’s actually for.

    I suppose if you’re doing a high paid design job where you have the time to perfectly layout your CSS then it will perhaps won’t need to be used, however it’s the real world and sometimes it’s just easier.

    Especially when doing custom headers etc and when you want to force (regardless of whatever anything else says) colours, styling and font-size etc.

    To sit and say !important should never be used really is quite ignorant.

    The Irony in this post is that @LouisLazaris needs to practise what he preaches, his own website is full of !important decorations, ignorance is bliss.

  28. 28

    Nice read…. #article { agreed !important; } <== haha! Cheers!

  29. 29

    I think you missed a colon, it won’t validate ;)

  30. 30

    “In my experience, you should only ever use it to override horrible, inline CSS that has been placed there normally by a rubbish ASP.NET back-end.”

    That’s exactly why I use it as well. -_-;;


↑ Back to top