This question already has an answer here:
Consider:
#div p {
color: red !important;
}
...
#div p {
color: blue;
}
I understand how !important
works. In this case the div will render red because now it has priority (!important
). But I can't still figure out an appropriate situation to use it in. Is there an example where !important
saves the day?
Using
!important
is generally not a good idea in the code itself, but it can be useful in various overrides.I use Firefox and a dotjs plugin which essentially can run your own custom JS or CSS code on specified websites automatically.
Here's the code for it I use on Twitter that makes the tweet input field always stay on my screen no matter how far I scroll, and for the hyperlinks to always remain the same color.
Since, thankfully, Twitter developers don't use
!important
properties much, I can use it to guarantee that the specified styles will be definitely overridden, because without!important
they were not overridden sometimes. It really came in handy for me there.I'm using
!important
to change the style of an element on a SharePoint web part. The JavaScript code that builds the elements on the web part is buried many levels deep in the SharePoint inner-workings.Attempting to find where the style is applied, and then attempting to modify it seems like a lot of wasted effort to me. Using the
!important
tag in a custom CSS file is much, much easier.This is the real life scenario
Imagine this scenario
usuallyvery bad practice.In this case you could set certain styles in your global CSS file as important, thus overriding inline styles set directly on elements.
Actual real world example?
This kind of scenario usually happens when you don't have total control over your HTML. Think of solutions in SharePoint for instance. You'd like your part to be globally defined (styled), but some inline styles you can't control are present.
!important
makes such situations easier to deal with.Other real life scenarios would also include some badly written jQuery plugins that also use inline styles...
I suppose you got the idea by now and can come up with some others as well.
When do you decide to use
!important
?I suggest you don't use
!important
unless you can't do it any other way. Whenever it's possible to avoid it, avoid it. Using lots of!important
styles will make maintenance a bit harder, because you break the natural cascading in your stylesheets.Overwriting the Style Attribute
Say in the example that you are unable to change the HTML source code but only provide a stylesheet. Some thoughtless person has slapped on a style directly on the element (boo!)
Here, !important can override inline CSS.
You use
!important
to override acss
property.For example, you have a control in ASP.NET and it renders a control with a background blue (in the HTML). You want to change it, and you don't have the source control so you attach a new CSS file and write the same selector and change the color and after it add
!important
.Best practices is when you are branding / redesigning SharePoint sites, you use it a lot to override the default styles.
Strictly speaking you shouldn't need to use !important if you've structured your CSS well and don't have too many degrees of specificity.
The most appropriate time to use !important is when you have one exceptional style that you want to style outside of your site's normal cascade.