How to Override CSS Styles (2024)

Table of Contents

Sometimes developers have to work with old codes, and it is when they run into some big problems, in particular, the inline style that cannot be overridden.

To prevent that problem, you should understand two concepts - the concept of Order and Inheritance.

How to Override CSS Styles (1)Watch a video course

The term “cascading” means hierarchical order in which different style sheet types interact when two styles come into conflict. The conflict occurs when two different styles are applied to the same element.

For these cases, there exists an order for style sheets according to their priority (4 has the highest priority):

  • Browser Defaults.
  • External Style Sheets (Linked or Imported).
  • Internal Style Sheets (Embedded).
  • Inline Styles.

So, it means that when a conflict arises between two styles, the last one used takes precedence. To make it clearer, you should remember these two rules:

  • You must place inline styles in the <body> of the HTML document, while embedded style sheets must be placed in the <head> of the HTML document so that the inline styles will always be the last used ones and therefore they will take precedence.
  • Internal style sheets have a higher priority than external ones, as according to the browser the external style sheets (linked style sheets) always come before the internal style sheets (embedded sheet), even if you place them after.

You can find examples of different types of style sheets here.

Inheritance

HTML uses parent-child relationships. A child element will usually inherit the characteristics of the parent element unless otherwise defined. For example, look at the following code.

Example of using an element inheriting the style of the parent element:

<!DOCTYPE html><html> <head> <style> body { color: blue; font-family: arial; } </style> </head> <body> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry . </p> </body></html>

Since the <p> tag, which is our child element, is inside of the <body> tag, which is the parent element, it will take all the styles given to the <body> tag even if it wasn’t given any styles of its own. But if you want the paragraph to take on some rules of the body but not others, you can override the rules you don’t want. Here’s an example for you.

Example of overriding the style of the <p> tag:

<!DOCTYPE html><html> <head> <style> body { color: blue; font-family: arial; } p { color: red; font-weight: bold; } </style> </head> <body> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p> </body></html>

Try it Yourself »

Now let’s see the list of the internal priorities (1 has the highest priority):

  1. ID
  2. Class
  3. Element

You can find detailed information about CSS id and class here.

To get a better understanding, keep in your mind the following structure:

How to Override CSS Styles (2)

It means that if you have an element with a class and ID selector with different styles, it is the ID style that takes precedence. As an example, let’s look at this code.

Example of overriding CSS style with the ID selector:

<!DOCTYPE html><html> <head> <style> #testid { color: blue; font-weight: bold; } .example { color: red; font-weight: normal; } </style> </head> <body> <p id="testid" class="example"> Lorem Ipsum is simply dummying text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. </p> </body></html>

Try it Yourself »

As we can see, the Class was placed after the ID, but the ID still takes precedence. It's only applicable if both the ID and the Class are used in the same element.

Now, let’s see an example, where an ID and a Class are used in two different elements.

Example of overriding CSS style with the Class selector:

<!DOCTYPE html><html> <head> <style> #testid { color: #777777; font-style: normal; background-color: lightgreen; } .example { display: block; color: whitesmoke; font-style: italic; background-color: lightblue; padding: 20px; } </style> </head> <body> <div id="testid"> <span class="example"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </span> </div> </body></html>

Try it Yourself »

Here, the Class selector overrode the ID selector because it was the last used one. An ID selector only takes precedence over a Class selector if they are both used in the same element.

Let’s now see how we can make one class override another. If that class has a background-color of blue, and you want your <div> to have a red background instead, try to change the color from blue to red in the class itself. You could also create a new CSS class that defined a background-color property with a value of red and let your <div> reference that class.

Example of making one style override another:

<!DOCTYPE html><html> <head> <style> .bg-blue { background-color: blue; } .bg-red { background-color: red; } </style> </head> <body> <div class="bg-blue bg-red"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div> </body></html>

Try it Yourself »

! Important

An !Important declaration is a great way to override the styles you want. When an important rule is used on a style declaration, this declaration will override any other declarations. When two conflicting declarations with the !important rules are applied to the same element, the declaration with a greater specificity will be applied.

Let’s see how you can use the !important declaration to override inline styles. You can set individual styles in your global CSS file as !important overrides inline styles set directly on elements.

Example of overriding CSS style with the !important rule:

<!DOCTYPE html><html> <head> <style> .box[style*="color: red"] { color: white !important; } .box { background-color: blue; padding: 15px 25px; margin: 10px; } </style> </head> <body> <div class="box" style="color: red;"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div> </body></html>

Try it Yourself »

However, you should avoid using !important, because it makes debugging more difficult by breaking the natural cascading in your stylesheets.

Instead of using !Important, you can try the following:

  1. Make better use of the CSS cascade.
  2. Use more specific rules. By indicating one or more elements before the element you've selected, the rule becomes more specific and gets higher priority.
  3. As a nonsense special case for (2), duplicate simple selectors for increasing specificity when you have nothing more to specify.

If you want to find more information about the !Important declaration, simply click here.

How to Override CSS Styles (2024)

FAQs

How do I override an existing CSS style? ›

To override the CSS properties of a class using another class, the `! important` directive can be employed. In CSS, `! important` signifies the significance of a style declaration, ensuring it takes precedence over other styles, regardless of their specificity.

How to override style in CSS without important? ›

Even when working to override high-specificity styles not under your control, such as styles in a 3rd party plugin declared with an id selector, you don't need to use ! important . Consider instead importing the 3rd party stylesheet script into a named or anonymous layer as your first cascade layer, instead of using !

How do you override default styles in CSS? ›

To override the default styles of an element in CSS, you can use a variety of selectors and properties. Here are some common ways to do it: Use a more specific selector: If you have a CSS rule that targets a generic element like "p" or "div," you can override it by using a more specific selector like an ID or class.

How to restrict overriding CSS? ›

use a selector with a higher priority, such as an ID instead of a class, e.g. #my-cool-link (you need to add that to the HTML to match it) write it inline in the HTML instead of in a CSS file, e.g. <a href=”#” style=”color: #0a0a0a”> use more or more specific selectors, e.g. body .

How do I overwrite CSS in Chrome? ›

TLDR
  • Open Chrome dev tools.
  • Visit Sources tab -> Overrides sub-tab -> Select folder for overrides -> Click Allow -> Select Enable Local Overrides -> Use Sources/Page tab to select files -> Right click Save for Overrides -> Make your style changes.
Mar 11, 2023

How to override text with CSS? ›

Using Pseudo-elements & CSS Visibility

This approach utilizes pseudo-elements and CSS visibility to achieve text replacement. The original text is hidden using a hidden span, and the replacement text is added using the :after' pseudo-element.

How to override CSS class inline? ›

You can override the inline CSS with the ! important keyword. However, if the inline CSS is also marked as ! important then it's not possible to override it with external CSS!

How to override CSS for div? ›

You can override a DIV's CSS class definition by updating the DIV's HTML code.
  1. Open an HTML document containing a DIV that references CSS classes. A typical DIV might appear as shown below: ...
  2. Type the following style attribute after the DIV's class definition: ...
  3. Save the document and view it in your browser.

How to override embedded CSS? ›

By using the [style] parameter with your selectors in your CSS stylesheets, you can completely override any inline styling that might be in your HTML.

What is override in CSS? ›

CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. Overriding: Overriding in CSS means that you are providing any style property to an element for which you have already provided a style.

Can we override CSS variables? ›

Root CSS variables are used extensively across other parts of Bootstrap to allow you to easily override our default styles at a global level. For example, if you wanted to adjust the default border-radius and link color for our components, you could override a couple variables instead of writing new selectors.

Is it possible to override important in CSS? ›

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

How do I undo a CSS style? ›

Use the revert-layer keyword to reset a property to the value established in a previous cascade layer. Use the unset keyword to set a property to its inherited value if it inherits or to its initial value if not.

How do I remove an existing CSS? ›

Method 1: Using CSS removeProperty

The style of the element is selected by going through the styleSheets array and selecting the cssRule. The removeProperty method can then be specified with the property to be removed.

Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6652

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.