1、在浮动元素后面加一个空div标签,添加clear : both;
.div1{background:yellow;}
.div2{background:#800080;width:400px;height:100px;}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:100px;background:#DDD}
.clearfloat{clear:both}/*清除浮动代码*/
<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
<div class="clearfloat"></div>
<div class="div2"> div2 </div>
</div>
2、 给父元素增加伪类:after
其中display将元素设置为块状,content属性设置要添加的伪类内容,clear清除浮动,overflow保证添加的伪类内容不会超出,visibility将填入的内容进行隐藏,ie8以上和非IE浏览器才支持
zoom(IE专有属性)可解决ie6,ie7浮动问题。
.div1{background:yellow;}
.div2{background:#800080;width:400px;height:100px;}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:100px;background:#DDD} /*清除浮动代码*/
.clearfloat:after{display:block;clear:both;content:"";height:0}
.clearfloat{zoom:1}
<div class="div1 clearfloat">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
<div class="div2"> div2 </div>