清除浮動的幾種方式,清除浮動幾種方式

來源:互聯網
上載者:User

清除浮動的幾種方式,清除浮動幾種方式

不清除浮動會怎樣?

(1):背景不能顯示

(2):邊框不能撐開

(3):margin 設定值不能正確顯示

(4):下方內容往上移蓋住浮動地區的內容

 

html代碼:

<div class="container">    <div class="fl"></div>    <div class="fr"></div></div><style>    .fl{float:left;}    .fr{float:right;}</style>

 

 

方式一:

.container下,最後再加一個div如下:

<div style="clear: both;height: 0; line-height: 0; font-size: 0"></div>

 

方式二:

給父元素.container添加樣式:

.container{    overflow: auto;    zoom: 1;}

 

方式三:

偽類清除浮動:after:

.container:after{    content:'';    display: block;    clear: both;    height:0;    width:100%;    visibility: hidden;}.container{  zoom:1;//為了相容IE}

 

 

方法四:

雙偽類清除浮動:

.container:before,.container:after {    content: "";    display: block;    clear: both;}.container{    zoom: 1;}

 

方式五:

展現為表格,需設定寬度:

.container{    width:100%;    display:table;}

 

方式六:

展現為內聯塊狀,需設定寬度:

.container{    width:100%;    display:inline-block;} 

 

方式七:

超出隱藏,不推薦使用

.container{    overflow:hidden;}

 

方式八:

給父元素定義高度,不推薦使用

.container{    height:50px;}

 

聯繫我們

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