Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I override inline !important?

If you have

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

Is there a way to override that in the style sheet to make it displayed?

Preferably using something similar to this:

div { display: block !important; } 
like image 599
Jeffrey Basurto Avatar asked Jun 22 '12 05:06

Jeffrey Basurto


People also ask

Can you override an important?

You can override the ! important rule, naturally by another one.

Can you override inline styles?

The only way to override inline style is by using ! important keyword beside the CSS rule.

How do you override inline style without important?

The only way to override a CSS rule without using ! important is to use a more specific selector. No selector is more specific than the style attribute.

Does inline CSS override internal?

It works kind of counter-intuitively, so just to explain further: inline styles override internal CSS, and internal CSS overrides external CSS files, and external CSS files override browser defaults. One way to think about it is like layers. The “closer” the style is to the element, the higher precedence it has.


2 Answers

Let me begin by saying that generally inline styles can be overridden:

.override {color:red !important;}
<p style="color:blue;">I will be blue</p> <p style="color:blue;" class="override">But I will be red</p>

This behavior is described in W3 specs, where it is stated that !important declarations do not alter the specificity, but rather take precedence over "normal" declarations.

That being said, when conflicting rules both have the !important flag, specificity dictates that an inline rule is applied - meaning that for OP's scenario, there's no way to override an inline !important.

like image 134
Oleg Avatar answered Sep 21 '22 17:09

Oleg


You cannot override inline CSS if it has !important. It has higher precedence than the style in your external CSS file.

However, if you want it to change some actions later on, you can use a bit of JavaScript.

like image 32
Shakti Singh Avatar answered Sep 21 '22 17:09

Shakti Singh