当前位置:趣知科普网>生活家居>生活常识>

css清除浮动常用的两种方法

生活常识 阅读(7.04K)

css清除浮动在网页布局中经常试用到的,下面两个方法可以实现浮动

第一种方法是 clear:both;

(01)css代码{ width:300px; background:#CCC; padding-bottom:10px; margin-bottom:10px;} { width:145px; height:100px; float:left; background:#F0F;} t{ width:145px; height:100px; float:right; background:#F00;}rboth{ clear:both;}html代码<div class="Box"><div class="left"></div><div class="right"></div><div class="clearboth"></div></div>

css清除浮动常用的两种方法

第二种方法是用伪类:after写入空白元素来清

(01)IE8以上和非IE浏览器才完全支持:after,IE6、IE7需要用ie的私有属性zoom来触发hasLayout才能完美兼容,没有增加多余的标签,推荐使用。css代码{ width:300px; background:#CCC; padding-bottom:10px; margin-bottom:10px;} { width:145px; height:100px; float:left; background:#F0F;} t{ width:145px; height:100px; float:right; background:#F00;}rfloat:after{ display:block; clear:both; content:""; overflow:hidden; height:0}rfloat{ zoom:1;}html代码<div class="Box"><div class="left"></div><div class="right"></div><div class="clearboth"></div></div>