I've been working on a website for a few months, and a lot of times when I've been trying to edit something, I have to use !important
, for example:
div.myDiv {
width: 400px !important;
}
in order to make it display as expected. Is this bad practice? Or is the !important
command okay to use? Can this cause anything undesired further down the line?
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!
If you must use ! important in your CSS, comment your usage so future code maintainers know why the declaration was marked important and know not to override it. But definitely, don't use ! important when writing plugins or frameworks that other developers will need to incorporate without being able to control.
The ! important statement is used for breaking this order and adding weight to a specific declaration to put it in effect, ignoring the others. It is used for overriding the styles that are previously declared in other style sources, in order to achieve a certain design or layout goal.
Yes, I'd say your example of using !important
is bad practice, and it's very likely it would cause undesired effects further down the line. That doesn't mean it's never okay to use though.
!important
:Specificity is one of the main forces at work when the browser decides how CSS affects the page. The more specific a selector is, the more importance is added to it. This usually coincides with how often the selected element occurs. For example:
button {
color: black;
}
button.highlight {
color: blue;
font-size: 1.5em;
}
button#buyNow {
color: green;
font-size: 2em;
}
On this page, all buttons are black. Except the buttons with the class "highlight", which are blue. Except that one unique button with the ID "buyNow", which is green. The importance of the entire rule (both the color and font-size in this case) is managed by the specificity of the selector.
!important
, however, is added at a property level, not a selector level. If, for instance, we used this rule:
button.highlight {
color: blue !important;
font-size: 1.5em;
}
then the color property would have a higher importance than the font-size. In fact, the color is more important than the color in the button#buyNow
selector, as opposed to the font-size (which is still governed by the regular ID vs class specificity).
An element <button class="highlight" id="buyNow">
would have a font-size of 2em
, but a color blue
.
This means two things:
!important
declaration, for example in the button#buyNow
selector.This not only makes your stylesheets a lot harder to maintain and debug, it starts a snowball effect. One !important
leads to another to override it, to yet another to override that, et cetera. It almost never stays with just one. Even though one !important
can be a useful short-term solution, it will come back to bite you in the ass in the long run.
This is what !important
was invented for in the first place: to give the user a means to override website styles. It's used a lot by accessibility tools like screen readers, ad blockers, and more.
Generally I'd say this is a case of code smell, but sometimes you just have no option. As a developer, you should aim to have as much control over your code as possible, but there are cases when your hands are tied and you just have to work with whatever is present. Use !important
sparingly.
Many libraries and frameworks come with utility classes like .hidden
, .error
, or .clearfix
. They serve a single purpose, and often apply very few, but very important, rules. (display: none
for a .hidden
class, for example). These should override whatever other styles are currently on the element, and definitely warrant an !important
if you ask me.
Using the !important
declaration is often considered bad practice because it has side effects that mess with one of CSS's core mechanisms: specificity. In many cases, using it could indicate poor CSS architecture.
There are cases in which it's tolerable or even preferred, but make sure you double check that one of those cases actually applies to your situation before using it.
!important
forces the statement to always apply, doing these things:
Most of the time, !important
can be avoided because specificity of selectors handles which one takes effect, which is the idea of cascading styles. However, there are some situations (can't quite remember the exact one) where the specificity isn't that logical and I have to force !important
on the statement.
Reasons not to use/avoid !important
?
!important
makes it harder to readIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With