對css中clear元素的理解

來源:互聯網
上載者:User

標籤:span   ati   close   log   cat   pre   sed   indicator   one   

  clear:left;表示左側不能有浮動元素。

  clear:right;表示右側不能有浮動元素。
  clear:both;表示左右兩側都不能有浮動元素。

  但在使用時,還得考慮css優先順序問題。相同類型選取器制定的樣式,在樣式表檔案中,越靠後的優先順序越高 。

  當所有元素的clear屬性都設為right時,由於優先順序的原因,並不是所想的那樣:右側沒有浮動元素,而是右側出現了浮動元素。

  比如下面的代碼:

  
<html><head><style type="text/css">.div1{height:40px;width:40px;background-color:red;position:releative;float:left;clear:right;}.div2{height:40px;width:40px;background-color:green;position:relative;float:left;clear:right;}.div3{height:40px;width:40px;background-color:yellow;position:relative;float:left;clear:right;}.div4{height:40px;width:40px;background-color:black;position:relative;float:left;clear:right;}.div5{height:40px;width:40px;background-color:blue;position:relative;float:left;clear:right;}</style></head><body><div class="div1"></div><div class="div2"></div><div class="div3"></div><div class="div4"></div><div class="div5"></div></body></html>
clear-right

  其中:class優先順序關係: div5>div4>div3>div2>div1 

  所以,呈現出情況:

  

 

  當所有元素的clear屬性都設為left時,由於優先順序的原因,並不是所想的那樣:右側可以有浮動元素,而是右側不能出現浮動元素。

  比如下面的代碼:

<html><head><style type="text/css">.div1{height:40px;width:40px;background-color:red;position:releative;float:left;clear:left;}.div2{height:40px;width:40px;background-color:green;position:relative;float:left;clear:left;}.div3{height:40px;width:40px;background-color:yellow;position:relative;float:left;clear:left;}.div4{height:40px;width:40px;background-color:black;position:relative;float:left;clear:left;}.div5{height:40px;width:40px;background-color:blue;position:relative;float:left;clear:left;}</style></head><body><div class="div1"></div><div class="div2"></div><div class="div3"></div><div class="div4"></div><div class="div5"></div></body></html>
clear-left

  其中:class優先順序關係: div5>div4>div3>div2>div1 。 

  所以,呈現出情況:

  我有時偶爾還是會繞暈。。反正,瞭解了css優先順序問題,就容易理解了。

對css中clear元素的理解

聯繫我們

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