我可以重写内联! 重要吗?

如果你有的话

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

有没有办法在样式表中重写它以使其显示?

最好使用类似的东西:

div { display: block !important; }
130338 次浏览

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.

You cannot override inline style having !important. First preference is inline style.

For eg: we have a class

.styleT{float:left;padding-left:4px;width:90px;}

and in jsp

<div class="styleT" id="inputT" style="padding-left:0px;">

here doesn't take the padding-left:4px; .It takes class styleT except the padding-left:4px;. There will be padding-left:0px;.

You can not override inline CSS having !important, because it has higher precedence, but, using JavaScript, you can achieve what you want.

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.

Here is a simple jQuery solution.

$(document).ready(function() {
$('div').css('display','block');
})

Precedence rules when two CSS properties apply to the same node:

  • !important beats not-!important. If equally !important, ...

  • style attribute beats css in a file. If both are in css files...

  • an ID in the CSS selector beats no ID. And more IDs beat less. (and you thought there was no reason for two IDs in a selector.) If same ID count...

  • Classes, or attributes like [name] in the selector, count them; more beats less. If all those are the same...

  • tag names like span or input, more beats less.

So you see the inline !important is the highest precedence.

You can see this example! There are several rules for CSS selector. The strongest selector is inline (if same level with/without !important). Next ones: id, class, etc. So if a tag is already stylized by inline css with !important, you just have a way: use Javascript to change.

var pid = document.getElementById('pid');
var button = document.getElementById('button');
button.addEventListener('click', function(){
pid.style.color = '';
});
p{color:violet !important;}


*{color:blue !important;}


html *{color:brown !important;}


html p{color:lighblue !important;}


.pclass{color:yellow !important;}


#pid{color:green !important;}
<p class="pclass" id="pid" style="color:red !important;">
Hello, stylize for me !
</p>


<button id='button'>Change color by JS</button>

As you see, the style by inline css was removed and the id is the strongest selector now !