標籤:注意 技術分享 box char tca 定義 als .com type
!important為開發人員提供了一個增加樣式權重的方法。應當注意的是!important是對整條樣式的聲明,包括這個樣式的屬性和屬性值
<!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>測試!Important</title></head><style type="text/css"> #Box div { color: red; } .important_false { color: blue; } .important_true { color: blue !important; }</style><body> <div id="Box"> <div class="important_false"> 這一行末使用important</div> <div class="important_true"> 這一行使用了important</div> </div></body></html>
效果:
CSS代碼第一行設定了box裡面所有div中字型色為紅色,第二行和第三行都用class重新定義了自身div的字型色為藍色,
不同的是,第二行未使用!important,而第三行使用了!
總結:
第一行字型顏色是紅色,可以證明,css樣式設定中,id的優先順序大於class,這行字還是紅色。
第二行字型顏色是藍色,可以證明,!important的優先順序最高,important_true的css樣式生效,這行字變為了藍色!
特別說明:
!important在IE6中是不被識別的,例:
.testClass{ color:blue !important;
color:red;}
這種寫法在IE6下是識別不到的,.testCalss最後顯示為紅色,但也可以通過更改下寫法讓IE6識別到!important
.testClass{ color:blue !important; } .testClass{ color:red; }
Css中!important的用法