清除浮动的集中方式

1 )设置 父级div的高度

2)在结尾处加添加 空div 标签  clear :both

.clearfloat {

clear :both

}

将 <div class = clearfloat><div> 包含在父级div 的最后

3)给父级div定义伪类: after 和 zonm

<style type="text/css"> 
   .div1{background:#000080;border:1px solid red;}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
   
   .left{float:left;width:20%;height:200px;background:#DDD}
   .right{float:right;width:30%;height:80px;background:#DDD}
   
   /*清除浮动代码*/
   .clearfloat:after{display:block;
clear:both;
content:"";
visibility:hidden;
height:0} .clearfloat{zoom:1} </style> <div class="div1 clearfloat"> <div class="left">Left</div> <div class="right">Right</div> </div> <div class="div2"> div2 </div>



4)父级div定义 overflow:auto

<style type="text/css"> 
   .div1{background:#000080;border:1px solid red;/*解决代码*/width:98%;overflow:auto}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px;width:98%}
   
   .left{float:left;width:20%;height:200px;background:#DDD}
   .right{float:right;width:30%;height:80px;background:#DDD}
   </style> 
<div class="div1"> 
<div class="left">Left</div> 
<div class="right">Right</div>
</div>
<div class="div2">
   div2
   </div>
posted @ 2017-03-24 17:45  奇&迹  阅读(70)  评论(0编辑  收藏  举报