CSS基礎(chǔ)教程之優(yōu)先級
CSS優(yōu)先級
單個選擇器的優(yōu)先級
行內(nèi)樣式 > id選擇器 > ?class選擇器 > 標(biāo)簽選擇器
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <style type="text/css"> h1{ color:green; } .hclass{ color:bule; } #hid{ color:black; } </style> </head> <body> <div> <h1 class="hclass" id="hid" style="color:red">習(xí)近平心中的互聯(lián)網(wǎng)</h1> </div> </body> </html>
利用firebug觀察:
多個選擇器的優(yōu)先級
多個選擇器的優(yōu)先級,一般情況下,指向越準(zhǔn)確,優(yōu)先級越高。
特殊情況下,我們需要假設(shè)一些值:
標(biāo)簽選擇器 ?????優(yōu)先級為1
類選擇器 ???????優(yōu)先級為10
Id選擇器 ???????優(yōu)先級為100
行內(nèi)樣式 ???????優(yōu)先級為1000
計算以下優(yōu)先級
.news h1{color:red;}?????優(yōu)先級:10 + 1 = 11
.title{color:blue;}????????優(yōu)先級:10
div.news h1{color:red;}???優(yōu)先級:1 + 10 + 1 = 12
h1.title{color:blue;}??????優(yōu)先級:1 + 10 = 11