How to Use the !important Property in CSS

Download Now: 25 Free HTML & CSS Hacks
Anna Fitzgerald
Anna Fitzgerald

Updated:

Published:

When styling your website, you may apply multiple CSS declarations to the same element. In that case, the browser determines which declarations are the most relevant to an element and applies those.

Programmer using !important property in CSS

This relevance — or, specificity — is based on the matching rules of CSS selectors. CSS selectors are modifiers that tell a browser which elements to style with CSS. There are different selector types, each with their own syntax and level of specificity.

Download Now: 25 HTML & CSS Hacks [Free Guide]

However, there is an exception to the rules of CSS specificity. It's known as the !important rule. Let’s go over a brief recap of the rules of specificity, then take a closer look at the !important rule below. We'll cover:

Recap of CSS Specificity

The following selector types rank from having the highest specificity to the lowest:

  1. ID selectors: These select an element based on its ID attribute and have the syntax #idname.
  2. Class selectors, attribute selectors, and pseudo-class selectors:
    a) Class selectors select all elements in a CSS class and have the syntax .class name.
    b) Attribute selectors selects all elements that have a given attribute and have the syntax [attr].
    c) Pseudo-class selectors select elements only when in a special state, like visited or hover, and have the syntax selector:pseudo-class.
  3. Type selectors: These select all HTML elements that have a given node name and have the syntax element.

Note: The universal selector (*) has no effect on specificity.

If multiple declarations with equal specificity, like a class selector and attribute selector, are made on the same element, then the last declaration (ie. the declaration listed later in the stylesheet) will be applied to the element.

The other major rule to keep in mind when coding the HTML and CSS of your website is the !important rule. This is the exception to all the specificity rules mentioned above. Let’s take a closer look at what it means below.

Although easier than following the many rules of specificity when combining property values, using the !important property is considered a bad practice. That’s because it breaks the natural cascading of stylesheets, which makes maintaining and debugging your website much more difficult.

However, there are specific use cases in which using the !important property is ideal. One such use case is defining utility classes, like the button class.

Say you want any element targeted by the button class to look like the same button: orange background, white font color, solid black border. Without the !important property, you’d use the following code to make a link look like a button.

Here’s the HTML and CSS:

<a href="#" class="button">Click Me</a> .button { background: #FF7A59; color: #FFFFFF; padding: 5px; font-size: 50px; border-radius: 5px; border: 2px solid black; }

Here’s the result:

See the Pen Anchor element with button class by Christina Perricone (@hubspot) on CodePen.

Now let’s say you keep building out your web page. At some point, you add a new section with the ID “content” to your HTML. This section contains another link with the button class. You also add another rule to your CSS, defining all <a> elements with the ID name “content” to have a solid blue border. You’d have the following code.

Here’s the HTML and CSS:

<a href="#" class="button">Click Me</a> <section id="content"> <p> text text blah <a href="#" class="button">Click Me</a> </p> </section> .button { background: #FF7A59; color: #FFFFFF; padding: 5px; font-size: 50px; border-radius: 5px; border: 2px solid black; } #content a { border: 4px solid #00A4BD; }

Here’s the result:

See the Pen xxgQYKq by Christina Perricone (@hubspot) on CodePen.

Because an ID selector has a higher specificity than a class selector, the CSS style of the selector #content a takes precedence over the CSS style of the selector .button. That’s why the second <a> element has a solid blue border, not a solid black one.

A scenario like this is inevitable, whether you’re building a site from scratch or using a framework like Bootstrap CSS. The larger the volume of code you’re working with, the harder it is to keep track of the specificity of your CSS selectors.

You can avoid the style inconsistencies of the above-mentioned scenario on your site by using the !important property. Let’s look at how exactly to use this property below.

Customer Testimonial CTA

How to Use Important in CSS

Using the !important rule in CSS is easy. You just have to add !important at the end of the line, immediately before the semicolon. So the syntax would be:

element { style property !important; }

Let’s take a look at how the CSS for the example above changes when adding the !important rule.

CSS Important Example

For this example, you want any element targeted by the button class to look like the same button: orange background, white font color, solid black border.

The problem is that you have some anchor elements (or links) defined by the button class and an ID name. In your CSS, elements with this ID name will have a solid blue border. Since ID selectors are more specific than class selectors, these anchor elements will be styled like a button with a orange background and white font color — but they'll have a solid blue border instead of solid black.

You don't want that type of inconsistent styling on your site. So you'll use the !important rule when specifying the CSS for the button class.

The HTML stays the same while the CSS changes:

<a href="#" class="button">Click Me</a> <section id="content"> <p> text text blah <a href="#" class="button">Click Me</a> </p> </section> .button { background: #FF7A59 !important; color: #FFFFFF !important; padding: 5px !important; font-size: 50px !important; border-radius: 5px !important; border: 2px solid black !important; } #content a { border: 4px dotted #00A4BD; }

The result is:

See the Pen CSS Important Example by Christina Perricone (@hubspot) on CodePen.

You’ll notice how both <a> elements look identical thanks to the !important rule.

CSS Important Not Working

Let's say you use the !important rule in your CSS, but the elements aren't styled as you want them to. There are only a couple reasons the !important rule would not work.

First, the !important rule will not work in the author's stylesheet if it is being used in the user's stylesheet. The author stylesheet is supplied by the author of the web page. The user stylesheet is supplied by the user of the browser.

By default, CSS rules in an author's stylesheet override those in a user's stylesheet. However, to create a balance of power between authors and users, !important rules in a user stylesheet will override !important rules in the author’s stylesheet. This CSS feature is designed for accessibility. Users that need larger fonts, specific color combinations, or other requirements due to disabilities related to vision can maintain control over the presentation of a web page.

It also possible that you have added !important to a CSS rule set that is overridden by another CSS rule set with the !important rule later in the stylesheet. You may have done this on accident, which would explain why important is not working — or you may want to do this on purpose.

Let's say you change your website's color scheme or you don’t want all elements with the button class to look the same. Whatever the reason, you may want to override the !important rule in your CSS. Let’s look at how below.

Why do these two methods work? Because of the rules of specificity. When two declarations using the !important rule are applied to the same element, the declaration with a greater specificity or the declaration defined last will be applied. You might remember these rules from the introduction, but it’s important to understand they apply to declarations using the !important rule as well as declarations not using the rule.

Rather than override the !important property in CSS with another !important property, it is ideal to rewrite the rule and avoid using the property altogether. But sometimes this isn’t possible.

Take user stylesheets, for example. Using a custom user stylesheet, a reader can override styles of the website according to their wishes. A reader might, for example, increase the font size or change the color of typography on the page to enable them to see the content better.

Since a user-defined stylesheet will override an author stylesheet regardless of specificity, a reader might be able to add CSS rules without the !important tag. However, if the author stylesheet is using the !important rule to define its font size or color, then the !important rule will be necessary in the user stylesheet.

Let’s continue using the same button example from above. Say, my website already had the !important rules applied to the button class. But now I do want all <a> elements with the ID name “content” to have a solid blue border.

I should simply rewrite the code, removing all !important tags from my CSS. But let’s say I’m on a time crunch and looking for a quick fix. Then I could simply add the !important tag to the CSS ID selector. Then both declarations would be using the !important property and, because ID selectors have a higher specificity then class selectors, the <a> element in the content section would have a solid blue border.

The HTML stays the same while the CSS changes:

<a href="#" class="button">Click Me</a> <section id="content"> <p> text text blah <a href="#" class="button">Click Me</a> </p> </section> .button { background: #FF7A59 !important; color: #FFFFFF !important; padding: 5px !important; font-size: 50px !important; border-radius: 5px !important; border: 2px solid black !important; } #content a { border: 4px dotted #00A4BD !important; }

The result is:

See the Pen Example 1 of Overriding !Important Rule by Christina Perricone (@hubspot) on CodePen.

The !important rules essentially cancel each other out. So since the ID selector has a higher specificity than a class selector, the second <a> element has a solid blue border, not a solid black one.

You can also override the !important rule by using a CSS selector of the same specificity, but placing it later in the stylesheet.

Let’s say I want all buttons on my website to have a solid blue border. Again, I should simply rewrite the code but I can use the !important tag for a quick fix. Using the same class selector .btn with a new rule for the solid blue border and an !important tag, I can simply add it after the existing rule in the stylesheet. Because it comes later, it has higher specificity and will be applied to elements with the .btn class.

Once again, the HTML stays the same while the CSS changes:

<a href="#" class="button">Click Me</a> <section id="content"> <p> text text blah <a href="#" class="button">Click Me</a> </p> </section> .button { background: #FF7A59 !important; color: #FFFFFF !important; padding: 5px !important; font-size: 50px !important; border-radius: 5px !important; border: 2px solid black !important; } .button { border: 4px dotted #00A4BD !important; }

Here’s the result:

See the Pen Example 2 of Overriding !Important Rule by Christina Perricone (@hubspot) on CodePen.

Using the !important Property in your CSS

While !important declarations should rarely be used in code, it’s still necessary to understand what this property is and how to use it. Maybe you’ll need to use the !important rule in your user stylesheets. Maybe you’ll take over a website that contains instances of the rule. Whatever the reason, becoming familiar with the !important property is useful step in learning HTML and CSS.

Editor's note: This post was originally published in May 2020 and has been updated for comprehensiveness.

coding-hacks

 

Related Articles

Tangible tips and coding templates from experts to help you code better and faster.

    CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

    START FREE OR GET A DEMO