简体   繁体   English

覆盖div的内联样式

[英]Override the inline style of the div

I have use the following code snippet to override the inline style of the div. 我使用以下代码片段覆盖了div的内联样式。

.gantttoolbar {
    height: 50px;
    display: block !important;
}

Is this correct way to override CSS style? 这是重写CSS样式的正确方法吗? OR Is there any other way to achieve this without using !important in the above code? 或在上述代码中不使用!important的方法还有其他方法吗?

You can use [style] trick: 您可以使用[样式]技巧:

.gantttoolbar[style] {
    height: 50px; // Use !important to override height too
    display: block !important;
}

http://css-tricks.com/override-inline-styles-with-css/ http://css-tricks.com/override-inline-styles-with-css/

There is no need to use the [style] css selector in this case as the element already has a class associated. 在这种情况下,无需使用[style] css选择器,因为该元素已经有一个关联的类。 The [style] only needs to be applied to select elements that do not have a class or id associated with them. 仅需要将[style]用于选择没有与之关联的类或ID的元素。

The !important is the only pure css way to override inline styles unfortunately, or you can use javascript to clear the styles on the elements, so that you don't use the important tag. 不幸的是,!important是覆盖内联样式的唯一纯css方法,或者您可以使用javascript清除元素上的样式,从而不必使用重要的标记。 To do that you would do the following: 为此,您将执行以下操作:

document.querySelector('.gantttoolbar').removeAttribute('style');

Here is a fiddle http://jsfiddle.net/jRxzH/ 这是一个小提琴http://jsfiddle.net/jRxzH/

Yes , this is right way to override the inline css. 是的,这是覆盖内联CSS的正确方法。

but i recommend don`t override CSS, use css classes and use that rather then inline css. 但我建议不要覆盖CSS,使用CSS类并使用它,而不要使用内联CSS。 it will save your loading and rendering speed. 它将节省您的加载和渲染速度。

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

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