css---選取器的特殊性,css---選取器

來源:互聯網
上載者:User

css---選取器的特殊性,css---選取器

特殊性這點吃了虧,是該梳理一下了~

先說一下大概的選取器,沾代碼

最權威的還是《css權威指南》

我們把特殊性分為4個等級,每個等級代表一類別選取器,每個等級的值為其所代表的選取器的個數乘以這一等級的權值,最後把所有等級的值相加得出選取器的特殊值。

4個等級的定義如下:

l  第一等:代表內聯樣式,如: style=””,權值為1000。

l  第二等:代表ID選取器,如:#content,權值為100。

l  第三等:代表類,偽類和屬性選取器,如.content,權值為10。

l  第四等:代表類型選取器和虛擬元素選取器,如div  p,權值為1。

注意:通用選取器(*),子選取器(>)和相鄰同胞選取器(+)並不在這四個等級中,所以他們的權值都為0。

栗子來了

<html>#content div#main-content h2{color:red; }/*=2*100+2*1=202*/#content #main-content>h2{color:blue }/*=2*100+1=201*/body #content div[id="main-content"] h2{color:green; }/*=1*100+1*10+3*1=113*/#main-content div.paragraph h2{color:orange; }/*=1*100+1*10+2*1=112*/#main-content [class="paragraph"] h2{color:yellow; }/*=1*100+1*10+1*1=111*/div#main-content div.paragraph h2.first{color:pink;}/*=1*100+2*10+3*1=123*/</html>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.