Home  >  Article  >  Web Front-end  >  How to Override Inline `!important` Styles in CSS?

How to Override Inline `!important` Styles in CSS?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-29 05:08:30542browse

How to Override Inline `!important` Styles in CSS?

Overriding Inline !important Styles

In CSS, the !important flag is used to indicate that a style property should take precedence over all other declarations. However, you may encounter situations where you need to override an inline style with !important from a stylesheet.

Consider the following HTML element:

<code class="html"><div style="display: none !important;"></div></code>

If you wish to make this element visible, you cannot simply use another !important rule in your stylesheet, as it will not override the inline style.

Solution

To override an inline style with !important, you must use the !important flag on a more specific selector. In this case, you can use a class selector like this:

<code class="css">div.visible {
  display: block !important;
}</code>

This rule will target any div element with the visible class and override the inline display: none style, making the element visible.

<code class="html"><div style="display: none !important;" class="visible"></div></code>

Note that this technique only works if the selector you use in your stylesheet is more specific than the inline selector. Additionally, it is generally good practice to avoid using !important excessively, as it can make your CSS code harder to maintain.

The above is the detailed content of How to Override Inline `!important` Styles in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn