css 兩種清除浮動經典執行個體代碼

來源:互聯網
上載者:User

一、使用空標籤清除浮動
<style type=”text/css”>
<!–
    *{margin:0;padding:0;}
    body{font:36px bold; color:#f00; text-align:center;}
    #layout{background:#ff9;}
    #left{float:left;width:20%;height:200px;background:#ddd;line-height:200px;}
    #right{float:right;width:30%;height:80px;background:#ddd;line-height:80px;}
    .clr{clear:both;}
–>
</style>
<div id=”layout”>
    <div id=”left”>left</div>
    <div id=”right”>right</div>
    <p class=”clr”>
</div>

空標籤可以是div標籤,也可以是p標籤。我習慣用<p>,夠簡短,也有很多人用<hr>,只是需要另外為其清除邊框,但理論上可以是任何標籤。這種方式是在需要清除浮動的父級元素內部的所有浮動元素後添加這樣一個標籤清楚浮動,並為其定義css代碼:clear:both。此方法的弊端在於增加了無意義的結構元


使用overflow屬性
有效地解決了通過空標籤元素清除浮動而不得不增加無意代碼的弊端。使用該方法是只需在需要清除浮動的元素中定義css屬性:overflow:auto,即可!”zoom:1′用於相容ie6。

<style type=”text/css”>
<!–
    *{margin:0;padding:0;}
    body{font:36px bold; color:#f00; text-align:center;}
    #layout{background:#ff9;overflow:auto;zoom:1;}
    #left{float:left;width:20%;height:200px;background:#ddd;line-height:200px;}
    #right{float:right;width:30%;height:80px;background:#ddd;line-height:80px;}
–>
</style>
<div id=”layout”>
    <div id=”left”>left</div>
    <div id=”right”>right</div>
</div>

 

聯繫我們

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