文檔中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