CSS清除浮动的六种方法

19 篇文章 0 订阅

六种方法中,推荐使用第三种。

1、父级div定义 height

  • 原理:手动设置父级div的height,解决了父级div无法自动获取高度的问题。
  • 缺点:只适合高度固定的布局,如果高度和父级div不一样时,会产生问题
<style type="text/css"> 
.box{background: red;
  /*解决代码*/
  height: 100px;
}
.left{
    background: blue;float: left;
    height: 100px;width:40%;
}
.right{
    background: pink;float: right;
    height: 100px;width:50%;
}
.footer{background: orange;height: 50px;}
</style> 

<div class="box"> 
  <div class="left">Left</div> 
  <div class="right">Right</div> 
</div> 
<div class="footer"> </div> 

没添加 解决代码 前:
这里写图片描述
添加 解决代码 后:
这里写图片描述


2、结尾处加空div标签 clear:both

  • 原理:先添加一个空div,再利用clear:both清除浮动,让父级div能自动获取到高度(添加span等inline标签无效)
  • 缺点:如果页面浮动布局多,就要增加很多空div,让人感觉很不好
<style type="text/css"> 
.box{background: red;}
.left{
    background: blue;float: left;
    height: 100px;width:40%;
}
.right{
    background: pink;float: right;
    height: 100px;width:50%;
}
.clear{/*解决代码*/clear: both;}
.footer{background: orange;height: 50px;}
</style> 

<div class="box"> 
  <div class="left">Left</div> 
  <div class="right">Right</div> 
  <div class="clear"></div>
  <!-- 无效:<span class="clear"></span> -->
</div> 
<!-- 不能加到这里<div class="clear"></div> -->
<div class="footer"> </div> 

执行结果:
这里写图片描述
如果你不懂原理,改成下面这样你就看懂了

<style type="text/css"> 
.left{
  background: blue;float: left;
  height: 100px;width:40%;
}
.right{
  background: pink;float: right;
  height: 100px;width:50%;
}
.clear{/*解决代码*/clear: both;background: red;}
.footer{background: orange;height: 50px;}
</style> 
<div class="left">Left</div> 
<div class="right">Right</div> 
<div class="clear">I am clear</div>
<div class="footer"></div> 

执行结果:
这里写图片描述
所以若没有上面的div.box元素且去掉div.clear,直接可以这样实现清除浮动

<style type="text/css"> 
.left{
  background: blue;float: left;
  height: 100px;width:40%;
}
.right{
  background: pink;float: right;
  height: 100px;width:50%;
}
.footer{/*解决代码*/clear: both;background: orange;height: 50px;}
</style> 

<div class="left">Left</div> 
<div class="right">Right</div> 
<div class="footer"></div> 

执行结果:
这里写图片描述


3、父级div定义 伪类:after 和 zoom

  • 原理:IE8以上和非IE浏览器才支持:after,原理和方法2有点类似,zoom(IE转有属性)可解决ie6,ie7浮动问题
  • 优点:浏览器支持好、不容易出现怪问题(目前:大型网站都有使用,如:腾迅,网易,新浪等等)

推荐使用。

<style type="text/css"> 
.box{background: red;}
.left{
    background: blue;float: left;
    height: 100px;width:40%;
}
.right{
    background: pink;float: right;
    height: 100px;width:50%;
}
.footer{background: orange;height: 50px;}
/*解决代码*/
.clear{zoom:1;}/*为解决ie6,ie7浮动问题*/
.clear:after{  /*三者缺一不可*/
  display:block;
  clear:both;
  content:"";
}
</style> 

<div class="box clear"> 
  <div class="left">Left</div> 
  <div class="right">Right</div> 
</div> 
<div class="footer"></div> 

执行结果:
这里写图片描述


4、父级div定义 overflow:hidden/auto

  • 原理:必须定义width或zoom:1,同时不能定义height,使用overflow:hidden/auto时,浏览器会自动检查浮动区域的高度.
  • 缺点:
    (1)overflow:hidden:不能和position一起用,因为超出的尺寸的会被隐藏。
    (2)overflow:auto:内部宽高超过父级div时,会出现滚动条。
<style type="text/css"> 
.box{background: red;
  /*解决代码*/
  width:100%;
  /*或 overflow:auto*/
  overflow:hidden;
}
.left{
    background: blue;float: left;
    height: 100px;width:40%;
}
.right{
    background: pink;float: right;
    height: 100px;width:50%;
}
.footer{background: orange;height: 50px;}
</style> 

<div class="box"> 
  <div class="left">Left</div> 
  <div class="right">Right</div> 
</div> 
<div class="footer"></div> 

执行结果:同上


5、大家一起浮动

  • 原理:大家都设为float,所有代码一起浮动,就变成了一个整体
  • 缺点:会产生新的浮动问题。
<style type="text/css"> 
.box{background: red;
  /*解决代码*/
  float: left;
  width: 100%;
}
.left{
    background: blue;float: left;
    height: 100px;width:40%;
}
.right{
    background: pink;float: right;
    height: 100px;width:50%;
}
.footer{
   background: orange;height: 50px;
  /*解决代码*/
  float: left;
  width: 100%;
}
</style> 
<div class="box">
  <div class="left">Left</div> 
  <div class="right">Right</div> 
</div>
<div class="footer">footer</div> 

执行结果:
这里写图片描述


6、父级div定义 display:table

  • 原理:将div属性变成表格
  • 缺点:会产生新的未知问题。
<style type="text/css"> 
.box{background: red;
  /*解决代码*/
  display:table;
  width:100%;
}
.left{
    background: blue;float: left;
    height: 100px;width:40%;
}
.right{
    background: pink;float: right;
    height: 100px;width:50%;
}
.footer{background: orange;height: 50px;}
</style> 

<div class="box"> 
  <div class="left">Left</div> 
  <div class="right">Right</div> 
</div> 
<div class="footer"> </div> 
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值