文档中inline-styles
具有最高优先级,因此,例如说,如果你想要一个div
元素的颜色改变为blue
,但你有一个inline style
与color
属性设置为red
<div style="font-size: 18px; color: red;">
Hello World, How Can I Change The Color To Blue?
</div>
div {
color: blue;
/* This Won't Work, As Inline Styles Have Color Red And As
Inline Styles Have Highest Priority, We Cannot Over Ride
The Color Using An Element Selector */
}
那么,我应该使用jQuery/Javascript? - 答案是NO
我们可以使用element-attr
CSS选择器,!important
,笔记,!important
在这里很重要,要不然也不会在乘坐内嵌样式..
<div style="font-size: 30px; color: red;">
This is a test to see whether the inline styles can be over ridden with CSS?
</div>
div[style] {
font-size: 12px !important;
color: blue !important;
}
Demo
注意:使用!important
只能在这里使用,但我用 div[style]
选择具体选择具有div
style
属性
为什么要使用内嵌样式,如果你想覆盖它们? – BFWebAdmin
@BFDatabaseAdmin他可能无法访问声明内联样式的标记。 – TylerH
是的,好点。 – BFWebAdmin