Css

How to override important

25 September 2026 · 6 min read

How to override important

Cascading Style Sheets (CSS) is the backbone of web design, dictating how elements appear on a page. The !important declaration adds a powerful, yet often misused, tool to the CSS arsenal. It enforces a style rule, overriding any other declarations for that property. While useful in certain situations, overuse can lead to a tangled mess of styles and make debugging a nightmare. So, how do you override !important when you absolutely must? This article delves into the strategies and best practices for managing and overriding this potent CSS feature, ensuring your styles remain manageable and predictable.

Understanding the Power (and Peril) of !important

The !important declaration adds weight to a specific style rule, making it the dominant declaration for that property. This means it will override any other styles applied to the same element, even those with higher specificity. This can be helpful for quick fixes or overriding styles from third-party libraries. However, relying heavily on !important creates brittle and difficult-to-maintain code. It disrupts the natural cascading flow of CSS, making it harder to predict how styles will interact.

Imagine a large project where !important is scattered throughout the stylesheets. Tracking down and modifying styles becomes a tedious and error-prone process. This is why understanding how to manage and override !important is crucial for any web developer.

Specificity: The Key to Overriding !important

One of the most effective ways to override !important is by using higher specificity. Specificity is determined by the selectors used to target an element. More specific selectors will win out over less specific ones, even if the less specific selector has the !important declaration. For example, an ID selector (myElement) is more specific than a class selector (.myClass), which is more specific than a type selector (div).

Consider this example: .myClass { color: blue !important; } and myElement { color: red; }. If an element has both the “myClass” class and the “myElement” ID, the text will be red because the ID selector is more specific.

Using more specific selectors allows you to override !important without resorting to using it yourself, keeping your styles cleaner and more manageable.

Using a More Specific !important Declaration

While not ideal, sometimes you need to fight fire with fire. If you absolutely must use !important, make sure the overriding declaration has a higher specificity than the one you’re trying to override. This might involve adding another class or ID to the element or using a more complex selector.

For instance, if you need to override .button { background-color: blue !important; }, you could use body .page-container main-section .button { background-color: red !important; }. While using multiple !important declarations is generally discouraged, this approach can be a last resort.

Just remember that this approach should be used sparingly and with caution, as it can contribute to the very problems that !important overuse creates.

Later Stylesheets Win: Cascading Order

CSS applies styles based on the order in which stylesheets are linked in the HTML document. Stylesheets loaded later will override styles from earlier stylesheets. This also applies to !important declarations. By placing your custom styles in a stylesheet loaded after the one containing the !important rule you want to override, you can effectively neutralize it.

Think of it as layers. Later stylesheets are layered on top of earlier ones, effectively covering up any conflicting styles. This provides a clean way to manage overrides without cluttering your HTML with inline styles or overly specific selectors.

  1. Identify the stylesheet containing the !important rule you need to override.
  2. Create a new stylesheet with your overriding styles.
  3. Link this new stylesheet after the original stylesheet in your HTML.

Resetting Styles: A Clean Slate

Sometimes, the best approach is to start fresh. Using a CSS reset can help neutralize problematic styles, including those using !important, by setting common element styles to a baseline. This allows you to build your styles from a consistent foundation, minimizing unexpected behavior from inherited or default styles.

Popular CSS resets, such as the Meyer Reset or Normalize.css, provide a solid starting point for styling your website and can help mitigate issues caused by overly aggressive use of !important in third-party libraries or legacy code.

Practical Examples and Best Practices

Let’s say a third-party library styles all buttons blue with !important. To make your primary button red, instead of using another !important, add a specific class to your button element, like “primary-button”, and style that class with the desired red color. This avoids the !important clash.

  • Avoid !important Unless Absolutely Necessary: Overuse leads to style conflicts and maintenance headaches.
  • Use Specificity: Leverage the cascading nature of CSS to override styles strategically.

FAQ: Common Questions about Overriding !important

Q: Is using !important always bad practice?

A: Not necessarily. It can be useful for quick fixes or overriding third-party styles, but excessive use should be avoided.

Managing !important effectively is crucial for maintaining clean, predictable, and maintainable CSS. By understanding specificity, cascading order, and using CSS resets, you can confidently tackle style overrides without resorting to !important overkill. Prioritize clean coding practices, and your future self (and your team) will thank you. Explore resources like MDN Web Docs and CSS-Tricks for more in-depth information on CSS specificity and best practices. Also, consider using a tool like Specificity Calculator to visualize and understand how specificity is calculated.

Ready to streamline your CSS and conquer those styling challenges? Start implementing these strategies today for a cleaner, more efficient development workflow.

Question & Answer :
I have created a custom style sheet that overrides the original CSS for my Wordpress template. However, on my calendar page, the original CSS has the height of each table cell set with the !important declaration:

td {height: 100px !important} 

Is there some way I can override this?

Overriding the !important modifier

  1. Simply add another CSS rule with !important, and give the selector a higher specificity (adding an additional tag, id or class to the selector)
  2. add a CSS rule with the same selector at a later point than the existing one (in a tie, the last one defined wins).

Some examples with a higher specificity (first is highest/overrides, third is lowest):

table td {height: 50px !important;} .myTable td {height: 50px !important;} #myTable td {height: 50px !important;} 

Or add the same selector after the existing one:

td {height: 50px !important;} 

Disclaimer:

It’s almost never a good idea to use !important. This is bad engineering by the creators of the WordPress template. In viral fashion, it forces users of the template to add their own !important modifiers to override it, and it limits the options for overriding it via JavaScript.

But, it’s useful to know how to override it, if you sometimes have to.