Introduction: What's Wrong with Using !important in Your CSS?
Nothing makes my eye twitch more than when I see !important
scattered throughout the CSS codebase. Recklessly adding !important
to your CSS can make your code hard to manage.
What is !important
in CSS?
The !important
CSS rule is typically used to override previous styling rules. Once this is added, you’ll need to use another !important
if you need to override it.
Using !important
too often in your CSS codebase can result in code that’s too difficult to maintain.
Why would you want to avoid using !important
?
Imagine you’re now coming in to maintain this code. You write some CSS only to realize it’s not working due to an !important rule that’s been applied somewhere. Your next step would be to either:
- Remove the !important and update the code so that everything looks as expected without it
- Add another !important for what you’re trying to do
Frequent use of !important
can result in a chaotic and convoluted CSS codebase.
Here’s what you should do instead to make maintaining the code easier for the next developer.
Steps to Take to Avoid Adding !important
to Your CSS
Option 1: Increase Specificity
Are you familiar with CSS specificity? No shame if you're not! I certainly wasn’t when I first started my web developer career. I was self-taught and ended up coming across it later.
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Specificity is based on the matching rules which are composed of different sorts of CSS selectors.
Learn more: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
Here is a list of selector types from the highest specificity to the lowest:
- ID selectors — (e.g.
#idname
). - Class selectors, attribute selectors, and pseudo-class selectors:
a) Class selectors — (e.g.,.example
).
b) Attribute selectors — (e.g.,[type="radio"]
).
c) Pseudo-class selectors — (e.g.,:hover)
. - Type selectors — (e.g.,
h1
).
Option 2: Consider Atomic CSS
Now that I’ve mentioned specificity, it’s worth noting that we want to avoid constantly creating too high specificity that we always need to override. To achieve this, let's consider reviewing an alternative approach to CSS: Atomic CSS.
Thierry Koblentz first coined the term Atomic CSS in his article titled “Challenging CSS Best Practices.” This CSS methodology works by mapping classes to a single style so that each class serves as a building block instead of contextual CSS.
It’s the difference between...
Too high specificity...
1// specificity too high and hard to maintain
2
3.button.red {
4 display: inline-block;
5 height: 1.75rem;
6 background-color: red;
7}
8
9.modal .button {
10 position: absolute;
11 bottom: 2rem;
12}
(usage in HTML)
1<div class="modal">
2 <button type="button" class="button red">Button</button>
3</div>
...and modularized CSS
1// Atomic CSS Classes - great for large scale sites
2
3.display-ib { display: inline-block; }
4
5.height-2 { height: 2rem; }
6
7.bg-red { background-color: red; }
8
9.absolute { position: absolute; }
10
11.left-50 { left: 50%; }
12
13.translateX-50 { transform: translateX(-50%); }
14
15.bottom-2 { bottom: 2rem; }
(usage in HTML)
1<div class="modal">
2 <button type="button" class="display-ib height-2 bg-red absolute left-50 translateX-50 bottom-2">Button</button>
3</div>
Why would I want to use Atomic CSS?
You may be thinking, “Why in the world would I want to create that many classes??”
I’ve found that these many, single functional CSS classes are beneficial in large projects. Consider these pros for Atomic CSS:
- It helps to minimize bloat — there is less need to add more styles to your stylesheet. With the atomic CSS classes, you’d be able to build out whole components without the need to add new CSS styles for the new component.
- It allows for code reuse — you can use the same CSS classes across many components.
- It reduces specificity conflicts — the need for !important is reduced due to fewer specificity conflicts since all atomic classes would use a low specificity.
Start Here: Atomic CSS Libraries
You can start using Atomic CSS with one of the following free libraries listed below.
- Tachyons CSS — https://tachyons.io/
- Tailwind CSS — https://tailwindcss.com/
- Bulma — https://bulma.io/
Option 3: Consider BEM
BEM stands for Block, Element, Modifier and is a naming convention for CSS classes. These classes are usually formed to look like: .block__element--modifier
Example:
/* the block component */
.modal {}/* the block component with a modifier that changes the style */
.modal--yellow {}/* the element that relies on the block */
.modal__button {}/* the element with a modifier */
.modal__button--red
(usage in HTML)
1<div class="modal modal--yellow">
2 <button type="button" class="modal__button--red">Button</button>
3</div>
Pros for BEM
- Standard naming convention — will help other developers have a common understanding of how the CSS classes should be named, thus adding to maintainability. At a glance, you’re able to determine the meaning of the class name and which components rely on another.
- Reduced specificity conflicts — this is another methodology that aims to reduce specificity conflicts by utilizing low specificity CSS classes.
Conclusion
Consider the options laid out in this article before invoking the !important
rule. It does have its place in CSS writing, such as in HTML newsletters and overriding inline styles. However, I wrote this article because I’ve seen it become a burden more often than not.
Using !important
too often in your CSS codebase can result in code that’s too difficult to maintain.
Consider the following when you need to override styles:
- Increase specificity — determine what is the suitable specificity and use it to override the necessary styles
- Beware of creating CSS selectors with too high specificity.
- Consider the following methodologies in your CSS codebase:
- Atomic CSS
- BEM
You’ll thank yourself later :).
Further Reading
- https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
- https://specifishity.com/
- https://blog.hubspot.com/website/css-important
- https://www.smashingmagazine.com/2013/10/challenging-css-best-practices-atomic-approach/
- https://www.sitepoint.com/css-architecture-block-element-modifier-bem-atomic-css/
- https://www.toptal.com/css/introduction-to-bem-methodology