css權重問題例子詳解

來源:互聯網
上載者:User

標籤:ace   標籤   style   層級   html   屬性   margin   div   字型   

首先,id選取器權重層級最高,class選取器其次,html選取器最後。 例子:

<div class="t1" id="one">    <div class="t2" id="two">        <div class="t3" id="three">            <span>嘿嘿嘿</span>        </div>    </div></div>
選中span標籤

當選中span標籤,也可能權重相同,也可能不同,得分下面2種情況。

1.權重相同,誰在後面誰決定。(下面例子嘿嘿嘿的字型顏色為黃色)

<style>    div.t2 span {         color:red;    }    div.t1 span {        color:yellow;    }</style>

2.權重不同,權重大的決定。(下面例子嘿嘿嘿的字型顏色為黃色)

<style>    /**class選取器最進階只有一個**/    div div.t1 span {        color:red;    }    /**id選取器最進階也只有一個,但層級比class高**/    #one span {        color:yellow;    }</style>
沒選中span標籤

如果沒選中span標籤,誰描述得近就誰決定,如果一樣近就再比權重,可以分為下面3種情況。

1.描述得近的決定。(下面例子嘿嘿嘿的字型顏色為黃色)

<style>    /**t3描述得最近**/    .t3 {        color:yellow;    }    #two {        color:red;    }    div.t1 #one {        color:green;    }</style>

2.描述得一樣近,權重不同,權重大的決定。(下面例子嘿嘿嘿的字型顏色為黃色)

<style>    /**多一個html選取器div**/    div.t1 div.t2 {        color:yellow;    }    div.t1 .t2 {        color:red;    }</style>

3.描述得一樣近,權重相同,誰在後面誰決定。(下面例子嘿嘿嘿的字型顏色為紅色)

<style>    div.t1 div.t2 {        color:yellow;    }    div.t1 div.t2 {        color:red;    }</style>
特別的!important

如果在某個屬性後面加個!important,那麼它權重無限大

下面例子如果不加!important嘿嘿嘿就是紅色,加了後就變成黃色了。

<style>     #two span {        color:red;    }    .t2 span {        color:yellow !important;    }</style>

css權重問題例子詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.