How to Use the !important Property in CSS (2024)

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.

How to Use the !important Property in CSS (1)

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.

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:

  • what important means in CSS
  • how to use the !important rule
    • why the !important rule isn't working
  • how to override the !important rule

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.

What does important mean in CSS?

In CSS, important means that only the !important property value is to be applied to an element and all other declarations on the element are to be ignored. In other words, an important rule can be used to override other styling rules in CSS.

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.

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.

How to Override Important in CSS

There are two ways you can override an !important tag in CSS. You can add another CSS rule with an !important tag and use a selector with a higher specificity. Or you can add another CSS rule with an !important tag using the same selector and simply add it later in the stylesheet.

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.

How to Use the !important Property in CSS (2024)

FAQs

How to apply imp in CSS? ›

CSS The !important Rule

The !important rule in CSS is used to add more importance to a property/value than normal. In fact, if you use the !important rule, it will override ALL previous styling rules for that specific property on that element!

How to provide important in CSS? ›

The ! important keyword must be placed at the end of the line, immediately before the semicolon, otherwise it will have no effect (although a space before the semicolon won't break it) If for some particular reason you have to write the same property twice in the same declaration block, then add !

Is it good practice to use important in CSS? ›

Best practices

Avoid using ! important to override specificity.

What is the importance priority in CSS? ›

The ! important property in CSS indicates that whatever rule to which it is attached takes precedent over other rules. It is the top priority for the element and selector its used with, and therefore lets developers and designers have specific control over styling for individual parts of the site.

How do I apply CSS to a specific element? ›

A CSS ID selector uses the ID attribute of an HTML element to select one unique element on a page. To use an ID selector in CSS, you simply write a hashtag (#) followed by the ID of the element. Then put the style properties you want to apply to the element in brackets.

How to use important in Bootstrap? ›

Importance. All utilities generated by the API include ! important to ensure they override components and modifier classes as intended. You can toggle this setting globally with the $enable-important-utilities variable (defaults to true ).

What can I use in CSS instead of important? ›

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. Use inline styles: Another way to ensure that a style is applied is to use inline styles.

What is CSS importance? ›

Cascading Style Sheets, commonly known as CSS, is a powerful tool in web development that helps to separate the structure of a website from its presentation. The primary function of CSS is to define the styling and layout of web pages, making them more visually appealing, user-friendly, and accessible.

How to make text important in HTML? ›

The HTML <strong> element defines text with strong importance. The content inside is typically displayed in bold.

When to use important? ›

important as moving a particular rule into a special layer that sits above the others—still is subject to the cascade, but it will only be overridden by subsequent rules that are also marked ! important . This enables you to place rules that you want to take precedence earlier in the cascade where this is logical.

What is most important for CSS? ›

To succeed in the CSS exam, you will need to study a wide range of subjects, including Pakistan's history, current affairs, and administrative law. The course structure tested in these examinations includes a list of subjects that you will need to pass.

How to get rid of important CSS? ›

The only thing that can override an ! important tag is another ! important tag. By using it one once, you potentially end up with a CSS file that is full of !

How do you define important in CSS? ›

What does important mean in CSS? In CSS, important means that only the ! important property value is to be applied to an element and all other declarations on the element are to be ignored. In other words, an important rule can be used to override other styling rules in CSS.

What is the order of important CSS? ›

1) Inline style: Inline style has highest priority among all. 2) Id Selector: It has second highest priority. 3) Classes, pseudo-classes and attributes: These selectors has lowest priority.

How do I make my CSS priority? ›

important: The second way is to add the ”! important" attribute at the end of the style, which will tell the web browsers that this CSS code takes precedence over any others that may exist.

How to use @import in CSS? ›

The @import CSS at-rule is used to import style rules from other valid stylesheets. An @import rule must be defined at the top of the stylesheet, before any other at-rule (except @charset and @layer) and style declarations, or it will be ignored.

How do you get immediate child in CSS? ›

The CSS child combinator ( > ) can be used to select all elements that are the immediate children of a specified element. A combinator combines and explains the relationship between two or more selectors.

How to use initial CSS? ›

The usage of the initial value depends on whether a property is inherited or not:
  1. For inherited properties, the initial value is used on the root element only, as long as no specified value is supplied.
  2. For non-inherited properties, the initial value is used on all elements, as long as no specified value is supplied.
Aug 1, 2024

How to use CSS inspector? ›

To open up the inspector, you can right-click on any element of a webpage and click “Inspect” or press F12. Go ahead and do that right now to see the HTML and CSS used on this page.

Top Articles
U.S. Dollar / Indian Rupee Historical Reference Rates from Bank of England for 2006
A new extremophile ostracod crustacean from the Movile Cave sulfidic chemoautotrophic ecosystem in Romania
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
Non Sequitur
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 5535

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.