How to Override Inline CSS Styles

Share this article

This article was written in 2009 and remains one of our most popular posts. If you’re keen to learn more about CSS, you may find this recent article on CSS techniques for retina displays of great interest.
Override inline CSSOne of the most powerful features of CSS is the cascading. Knowing how a browser chooses and applies your styles is invaluable knowledge. Novices can find it especially confusing given that style use is influenced by the method used to include the CSS, the order of the rules, how the selectors are specified, and special declarations such as !important. Inline styles are those defined in the HTML itself, e.g.

<p>This is an 
<strong style="color: red;">inline style that should be blue</strong>
.</p>
Inline styles have the highest priority of all CSS. In general, I would recommend you avoid using them and place your CSS declarations in external files. However, you may not have that luxury if you are working on a legacy system or do not have direct access to the HTML code. Fortunately, there is a way to override inline styles from an external stylesheet:

strong[style] { color: blue !important; }
This will force the text in the strong tag to become blue in the example above. The method appears to work in all major browser, including:
  • Internet Explorer 8.0
  • Mozilla Firefox 2 and 3
  • Opera 9
  • Apple Safari, and
  • Google Chrome
Internet Explorer 6 and 7 are the notable exceptions. However, this technique is not something you should use on a day-to-day basis. Keep your CSS clean and only override inline styles when there is absolutely no alternative. And if you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like Practical CSS. Comments on this article are closed. Have a question about CSS? Why not ask it on our forums?

Frequently Asked Questions (FAQs) about Overriding Inline CSS

What is the importance of overriding inline CSS?

Overriding inline CSS is crucial in web development for several reasons. Firstly, it allows developers to apply global styles to HTML elements, which can significantly reduce the time and effort required to style individual elements. Secondly, it promotes code reusability, as the same style sheet can be applied to multiple HTML documents. Lastly, it enhances website performance as browsers can cache external style sheets, reducing the load time of web pages.

How can I use the !important rule to override inline CSS?

The !important rule in CSS is a powerful tool that can override other subsequent style declarations in the CSS. To use it, simply append “!important” to your style declaration. For example, if you want to override an inline style of “color: red”, you can do so with an external or internal CSS rule like “color: blue !important”. However, use this rule sparingly as it can make debugging difficult due to its high specificity.

Can I override inline CSS with external CSS?

Yes, you can override inline CSS with external CSS. However, inline styles have a higher specificity than external styles. To override them, you need to use the same specificity in your external CSS and declare it after the inline style. Alternatively, you can use the !important rule to increase the specificity of your external CSS.

Why is my CSS not overriding inline styles?

If your CSS is not overriding inline styles, it could be due to several reasons. One common reason is that the inline style has a higher specificity than your CSS rule. Another reason could be the order of your CSS declarations – CSS rules declared later will override earlier ones. Lastly, it could be due to the use of the !important rule in the inline style, which increases its specificity.

How can I increase the specificity of my CSS rules?

You can increase the specificity of your CSS rules by using more specific selectors. For example, an ID selector (#id) has a higher specificity than a class selector (.class), and a class selector has a higher specificity than a type selector (e.g., div). You can also increase the specificity by chaining selectors, for example, div.class#id. Lastly, you can use the !important rule to increase the specificity of a CSS rule.

What is the order of precedence in CSS?

The order of precedence in CSS is determined by the specificity of selectors and the order of CSS rules. Inline styles have the highest specificity, followed by ID selectors, class selectors, and type selectors. If two selectors have the same specificity, the one declared later will take precedence. The !important rule can also affect the order of precedence by increasing the specificity of a CSS rule.

Can I override inline CSS with JavaScript?

Yes, you can override inline CSS with JavaScript. You can do this by accessing the style property of an HTML element and changing its value. For example, document.getElementById(“myElement”).style.color = “blue” will change the color of the element with the ID “myElement” to blue. However, keep in mind that this will add an inline style to the element, which has a high specificity.

How can I override CSS in a WordPress theme?

To override CSS in a WordPress theme, you can use a child theme or a custom CSS plugin. A child theme allows you to make changes to your theme without affecting the original theme. A custom CSS plugin allows you to add your own CSS rules that will override the theme’s CSS. In both cases, you need to use the correct selectors and specificity to override the theme’s CSS.

How can I override CSS in Bootstrap?

To override CSS in Bootstrap, you can create a custom CSS file and link it after the Bootstrap CSS file in your HTML document. This will ensure that your CSS rules take precedence over Bootstrap’s rules. You can also use the !important rule to increase the specificity of your CSS rules. However, use this rule sparingly as it can make debugging difficult.

How can I override CSS in a React component?

To override CSS in a React component, you can use inline styles or CSS modules. Inline styles in React are written as JavaScript objects and have a high specificity, allowing them to override other styles. CSS modules allow you to write CSS that is scoped to a specific component, reducing the risk of style conflicts. In both cases, you need to use the correct selectors and specificity to override the existing CSS.

Craig BucklerCraig Buckler
View Author

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week