简体   繁体   English

覆盖内联 css !重要

[英]override inline css !important

how can I override the style "color: red" either with javascript or CSS?如何使用 javascript 或 CSS 覆盖样式“颜色:红色”? I want to make it "2px" instead of "1px" border.我想使它成为“2px”而不是“1px”边框​​。

<div style="border: 1px solid #ccccc !important"> 
       Lorem... 
</div>

I can NOT add a class, id to the "div".我不能在“div”中添加一个类,id。 that is not a solution here.这不是这里的解决方案。 I have to override the code above somehow.我必须以某种方式覆盖上面的代码。

Solution Used jquery already loaded on the site.解决方案使用的 jquery 已经加载到站点上。 Ran: $( document ).ready(function() { $('table[style*=border]').css('border-width',"4px"); }); Ran: $( document ).ready(function() { $('table[style*=border]').css('border-width',"4px"); }); https://jsfiddle.net/78oxmgLj/10/ https://jsfiddle.net/78oxmgLj/10/

With your actual code you cannot do it with CSS (As @TJ Crowder commented, there is no CSS rule that can beat inline with !important ) but here is a JS solution:有了您的实际代码你不能用CSS做(如@TJ克罗德评论说,没有任何可以与在线击败CSS规则!important ),但这里是一个JS的解决方案:

 document.querySelector('.box').style.borderWidth="2px"; document.querySelector('.box').style.borderColor="red"; //or //document.querySelector('.box').style.border="2px solid red";
 <div class="box" style="border: 1px solid #cccccc!important"> Lorem... </div>

UPDATE更新

If for some reason you cannot add class you can use attribute selector based on style (but I don't advice to use this as a generic solution)如果由于某种原因您无法添加类,您可以使用基于样式的属性选择器(但我不建议将其用作通用解决方案)

 document.querySelector('div[style*=border]').style.borderWidth="2px"; document.querySelector('div[style*=border]').style.borderColor="red";
 <div style="border: 1px solid #cccccc!important"> Lorem... </div>

While the accepted answer is true, there are ways depending on your usecase that you can do here with CSS alone.虽然接受的答案是正确的,但根据您的用例,您可以在此处单独使用 CSS 进行一些方法。 Even if you cannot override the specific value, you can use a different property to change the original state.即使您无法覆盖特定值,您也可以使用不同的属性来更改原始状态。

 b { filter: hue-rotate(250deg); /*blue*/ } i { display: inline-block; transform: scale(0.4); }
 <b style="color: red !important">Stack</b> <i style="font-size: 50px !important">Stack</i>

In your example, if you need to change 1px important inline border to 2px, you can use outline or box-shadow without blurring:在您的示例中,如果您需要将 1px 重要的内联边框更改为 2px,您可以使用轮廓或框阴影而不会模糊:

 div:first-child { box-shadow: 0 0 0 3px #ccc; }
 <div style="border: 1px solid #cccccc !important"> Lorem... </div> <br> <div style="border: 4px solid #cccccc !important"> Lorem... </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM