CSS基礎之清除浮動

來源:互聯網
上載者:User

標籤:css 清除浮動 浮動

CSS基礎之清除浮動


  本人前端菜鳥一枚,在學習 CSS 過程中發現網上流傳的教程參差不齊,要麼內容不夠詳細,要麼方法就是錯的。本文是在我參考了許多前輩經驗的基礎上編寫的,如有錯誤的地方,請各位大牛不吝賜教。以下是我總結的三種行之有效而且比較簡單實用的方法。


一、父級div定義偽類 :after


代碼如下:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>CSS基礎之清除浮動</title>    <style>        *{            margin:0;            padding: 0;        }        #header,#footer{            width: 100%;            height: 50px;            background: #3ac;        }        .father{            margin: 10px auto;        }        .float_left{            float: left;            background: #a3f;            width: 70%;            height: 450px;        }        .float_right{            float: right;            background: #f3f;            width: 30%;            height: 300px;        }        .father:after{            display: block;            content: "";            clear: both;        }    </style></head><body><div id="header">頭部</div><div class="father">    <div class="float_left">left</div>    <div class="float_right">right</div></div><div id="footer">底部</div></body></html>

其中關鍵的部分為:

 .father:after{            display: block;            content: "";            clear: both;        }

  這裡 content 的值盡量寫為空白或者不寫,如果寫其他值,則需添加多餘的代碼,舉例如下:

 .father:after{          display: block;          height: 0;          visibility: hidden;          content: "清除浮動";          clear: both;        }

雖然也能清除浮動,但多了一些不必要的代碼。


二、在結尾處添加空的div標籤


原理跟使用 :after 偽類一樣,都是通過 clear: both; 來實現的。範例程式碼如下

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>CSS基礎之清除浮動</title>    <style>        *{            margin:0;            padding: 0;        }        #header,#footer{            width: 100%;            height: 50px;            background: #3ac;        }        .father{            margin: 10px auto;        }        .float_left{            float: left;            background: #a3f;            width: 70%;            height: 450px;        }        .float_right{            float: right;            background: #f3f;            width: 30%;            height: 300px;        }        .clr{            display: block;            content: "";            clear: both;        }    </style></head><body><div id="header">頭部</div><div class="father">    <div class="float_left">left</div>    <div class="float_right">right</div>    <div class="clr"></div></div><div id="footer">底部</div></body></html>


三、父元素定義 overflow:auto;


HTML結構跟上面一樣,CSS樣式部分如下:

*{    margin:0;    padding: 0;}#header,#footer{    width: 100%;    height: 50px;    background: #3ac;}.father{    margin: 10px auto;    overflow: auto;}.float_left{    float: left;    background: #a3f;    width: 70%;    height: 450px;}.float_right{    float: right;    background: #f3f;    width: 30%;    height: 300px;}

這種方法使用起來很簡單,但具有一定的局限性。內部寬高超過父級div時,會出現捲軸。 


以上就是清除浮動的三種方法。另外如果父元素本身也是浮動的話,則父元素內就無需清除浮動。要根據實際情況選擇可行的方法。

本文出自 “Fcheng” 部落格,請務必保留此出處http://fcheng.blog.51cto.com/10644114/1835366

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.