css對於float返迴文本流的做法

來源:互聯網
上載者:User

標籤:style   color   strong   for   問題   html   代碼   ar   

在設計中常常會遇到這樣的問題:
當容器內部有float樣式時,容器的高度不會被撐開。

下面介紹兩種方法解決上面的問題

第一種:在浮動元素後面加一個clear: both;比較常見的方法。

<style type="text/css">.left{float:left;}.right{float:right;}.clear{clear:both;}</style><div><div class="left"></div><div class="right"></div><div class="clear"></div></div>

缺點:相容性不強,IE6下div.clear會有高度,需要height:0,overflow:hidden來解決;頁面有冗餘代碼。
第二種方法:clearfix;最完美的解決方案。

.clearfix:after {content: ".";display: block;clear: both;visibility: hidden;line-height: 0;height: 0;} .clearfix {display: inline-block;} html[xmlns] .clearfix {display: block;} * html .clearfix {height: 1%;}

或者

.clearfix{    zoom:1}.clearfix:before,.clearfix:after{    content:‘\0020‘;    display:block;    overflow:hidden;    visibility:hidden;    width:0;    height:0}.clearfix:after{    clear:both}

用法:在浮動外的容器上加一個clearfix類。

<div class="clearfix">    <div class="left">    </div>    <div class="right">    </div></div>

或者

<div class="panel clearfix">    內容···</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.