Should I avoid using !important in CSS?

Use !important very, VERY sparingly — it overrides just about everything, even inline styles, and messes in a less-than-obvious way with the “cascade” of style rules that gives CSS its name. It’s easy to use badly, and tends to multiply, particularly when misused. You can easily end up with a element with !important rules that you want to override, at which point you often have to either refactor your styles, or use another !important rule and contribute to the problem.

And once it’s spread and you’re using it everywhere, you’re back up in the same situation you’d be in without it (difficulty in specifying elements specifically enough to override other styles), but you also don’t have !important anymore cause everything else is using it too.

When faced with a situation where !important looks appealing — or worse, one where it’s already in use and spreading — prefer to refactor your CSS if you can. (Frankly, if you need !important outside of a user style sheet, it’s usually because your selectors are already way too specific, and/or you’re not taking advantage of the C in CSS.) You’d do better to define your basic styles as close as possible to the html or body elements, and when you want to override, use as little specificity as you can get away with. That way, you have plenty of room to make changes. Usually there’s a reason you want to override a style, and those cases can quite often be boiled down to a class name, a particular section of the page (read: a particular parent element), etc.

(The only real exception that springs to mind is if the styles you’re overriding are effectively out of your control. (If you use a framework that has very strong opinions on how your page should look, for example, you might find it annoyingly difficult to override anything. I’ve worked with applications that actually inserted their own inline styles, where nothing but an !important rule could override them.) If you don’t have full access to the code, overriding and refactoring can easily be more trouble than they’re worth. You can use !important to claw back some control, as long as you’re aware of the consequences.)

Leave a Comment