DEV Community

Cover image for CSS !important: Avoid Using
StakeDesigner
StakeDesigner

Posted on

CSS !important: Avoid Using

CSS (Cascading Style Sheets) is a powerful tool that enables web developers to style and format their websites. However, sometimes it can be challenging to get CSS to apply styles exactly as intended. One such tool in the CSS arsenal is the !important rule, which can be used to override other styles and force a particular style to be applied.

While the !important rule can be a useful tool in certain situations, it should be used sparingly and with caution. Overuse of !important can make it difficult to maintain and update a website's styles in the future, and it can also lead to unexpected behavior and styling issues.

What does the !important tag actually do?

When you’re just starting with CSS, the !important tag seems like a secret weapon that you can pull out when styles aren’t working as expected. This situation occurs when you’re trying to override styles that are declared somewhere else in your CSS.

For example, let’s imagine you want to italicize everything that appears inside of <blockquote> elements.

blockquote * {
    font-style: italic;
}
Enter fullscreen mode Exit fullscreen mode

And for some reason it’s not working as you would expect, so you add !important and everything is fine! It’s like magic!

blockquote * {
    font-style: italic!important;
}
Enter fullscreen mode Exit fullscreen mode

What you’re actually doing in this case is increasing the specificity of everything inside blockquotes to an unreasonably high level.

By using !important, you’re essentially telling the browser that, under no circumstances should elements inside of blockquotes ever be anything other than italic.

This is a mistake. Let me explain.

Why you should avoid using !important in your CSS

Using the same scenario as above, let’s imagine 6 months go by and another developer on your team needs to “unitalicize” (for lack of a better word) blockquote text in some cases….such as citations. This is the first attempt:

HTML

<blockquote>
    <p>Don't write lazy CSS</p>
    <cite>- Nick Rollins</cite>
</blockquote>
Enter fullscreen mode Exit fullscreen mode

CSS

cite {
    font-style: normal;
}
Enter fullscreen mode Exit fullscreen mode

Despite declaring a normal font-style specifically for the element, the text is still italicized.

Don’t write lazy CSS
– Nick Rollins

Under normal circumstances, this would override the font-style that is set for the element, but it’s not working as expected!

The developer may even try adding a class or an ID to the element, only to discover that nothing will override the italicized text.

At this point, the only option is to add an !important tag to force the style to apply.

cite {
    font-style: normal!important;
}
Enter fullscreen mode Exit fullscreen mode

Here are some reasons why you should avoid using !important in your CSS code:

  1. Specificity issues: The specificity of a CSS selector determines which styles take precedence over others. When you use !important, you increase the specificity of a style, which can cause conflicts and make it difficult to override the style in the future.

  2. Maintenance challenges: Using !important can make it difficult to maintain and update a website's styles because it requires a lot of manual work to override styles that are marked as important. This can slow down development time and lead to mistakes and errors.

  3. Unexpected behavior: When you use !important, it can override styles that you didn't intend to modify, which can cause unexpected behavior and visual issues on the website.

So what can you do instead of using !important? Here are some alternative strategies that you can use to achieve the same results:

  1. Increase specificity: Instead of using !important, try increasing the specificity of your CSS selector. This can be achieved by adding more specific classes or IDs to the selector, or by using a more targeted CSS selector.

  2. Use inline styles: Another way to ensure that a style is applied is to use inline styles. Inline styles are applied directly to the HTML element, which makes them very specific and difficult to override.

  3. Avoid conflicts: One of the best ways to avoid conflicts and the need to use !important is to keep your CSS organized and modular. Use separate files for different sections of the website, and avoid using global styles that can affect multiple elements at once.

  4. Use CSS pre-processors: CSS pre-processors, such as Sass and Less, allow you to write CSS code with advanced features like nesting, variables, and mixins. These features can help you avoid the need to use !important by making your code more organized and easier to maintain.

conclusion

CSS !important rule can be a useful tool in certain situations, but it should be used sparingly and with caution. By following the alternative strategies listed above, you can achieve the same results without compromising the maintainability and readability of your CSS code.

Support 🤗

YouTube
Website

Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!

😊😊 See you later! 😊😊

Top comments (0)