浅谈清除浮动的方法以及利弊分析

引言:

一  浮动的产生对于我们对于网站的布局特别是实现图片环绕等带来了方便,但是在我们使用浮动的时候也产生了一系列的问题,因此我们再使用浮动以后必须对浮动进行清除。

二  在开始介绍清除浮动之前首先说下浮动的原理和过程,浮动脱离文档的普通流中,向左或者向右移动,直到碰到包含框或者另一个浮动框的边框为止,因此我们在清除浮动的时候应该参照他浮动的原理,所以清除浮动实际上也是闭合浮动的一个过程。

清除浮动的方法以及利弊:

1.方法一:父级定义overflow:hidden;

利:兼容性好,代码简单

弊:有特定的使用范围,父级需要有宽度,并且没有高度,并且不可以和定位一起使用

总结:可用性一般,在特定的情况下可以使用

代码示例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">

                       .box{
width: 960px;
margin:20px 0 20px 0;
background: blue;
overflow: hidden;/*父级设置overflow: hidden;*/
/*overflow: scroll;
overflow: auto;*/
}


.div1{
width:200px ;
height:200px ;
background:red ;
margin:32px 0px 32px 32px;
float: left;
}
.div2{
width:200px ;
height:200px ;
background:yellow ;

}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
</div>
<div class="div2">
</div>
</body>
</html>

2.方法二:父级定义overflow:auto;  如果需要宽高都要滚动条也可以overflow:scroll;

利:此方法和方法一相似

弊:auto当子元素高度超过父元素会出现滚动条,scroll一定会出现滚动条

总结:可用性低,一般在明确需要滚动条的情况下可以使用,其他情况不推荐

代码示例见方法一

3.方法三:父级设置height;

利:方便简单,容易掌握

 弊:特定范围,只适合有固定高度的布局

 总结:可用性一般,只适合有固定高度的布局

 代码示例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">
.box{
width: 960px;
margin:20px 0 20px 0;
background: blue;
height: 264px;/*父级定义高度*/
}
.div1{
width:200px ;
height:200px ;
background:red ;
margin:32px 0px 32px 32px;
float: left;
}
.div2{
width:200px ;
height:200px ;
background:yellow ;

}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
</div>
<div class="div2">
</div>
</body>
</html>

4.方法四:父级元素也跟着浮动,并且下面的元素需要使用clear:both;

利:不明显

弊:左右的margin:auto会失效;另外父级浮动会产生很多新的问题

总结:可用性非常低,不推荐使用

 代码示例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">
.box{
width: 960px;
margin:20px 0 20px 0;
background: blue;
   float: left;/*父级元素也跟着子元素浮动*/
  
}
.div1{
width:200px ;
height:200px ;
background:red ;
margin:32px 0px 32px 32px;
float: left;
}
.div2{
width:200px ;
height:200px ;
background:yellow ;
clear:both/*下面的元素需要clear:both*/

}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
</div>
<div class="div2">
</div>
</body>
</html>

5:方法五:父级定义display:table;

利:将div变成表格的形式,用表格进行操作

弊:强制的转化成表格形式,在整个布局中会产生很多新的问题

总结:可用性非常低,不推荐使用

代码示例

  <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">
.box{
width: 960px;
margin:20px 0 20px 0;
background: blue;
   display:table ;/*父级添加display:table*/
  
}
.div1{
width:200px ;
height:200px ;
background:red ;
margin:32px 0px 32px 32px;
float: left;
}
.div2{
width:200px ;
height:200px ;
background:yellow ;


}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
</div>
<div class="div2">
</div>
</body>
</html>

6:方法六:父级定义diaply:inline-block

利:通过转化父级的属性,从而对子元素进行操作

弊:左右的margin:0 auto; 会失效;(如果需要让元素居中可以给父级加text-align:center),使用时会产生新的问题

总结:可用性较低,不推荐使用

代码示例如下

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">
.box{
width: 960px;
margin:20px 0 20px 0;
background: blue;
   display:inline-block ;/*父级添加display:inline-block*/
  
}
.div1{
width:200px ;
height:200px ;
background:red ;
margin:32px 0px 32px 32px;
float: left;
}
.div2{
width:200px ;
height:200px ;
background:yellow ;


}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
</div>
<div class="div2">
</div>
</body>
</html>

7:方法七:父级元素定义伪类:after和zoom

利:浏览器兼容好,目前主流方法,使用比较方便

弊:代码量较多,原理相对其他几种清除方法稍复杂(伪类:after其实就是在父级的后面添加一个伪元素并且对其设置相应的属性,display:block 设置生成的元素以块元素显示; height:0 避免因浮动而改变其高度;visibility:hidden 隐藏其生成的内容,并允许可能被覆盖的元素可以进行交互处理;content:"."生成的内容作为最后一个元素,无论是点还是其他都可以,zoom主要是做ie6 ie7兼容)

总结:可用性高,推荐使用,目前主流的清楚浮动的方法,我代码示例是在上面直接写的,建议使用的时候放在公共类中,提高其复用率

代码示例如下:

    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">
.box{
width: 960px;
margin:20px 0 20px 0;
background: blue;
   zoom:1/*父级设置zoom:1*/  
}
.box:after{display:block;
          clear:both;
          content:"";
          visibility:hidden;
          height:0
          } /*设置伪元素*/
.div1{
width:200px ;
height:200px ;
background:red ;
margin:32px 0px 32px 32px;
float: left;
}
.div2{
width:200px ;
height:200px ;
background:yellow ;
}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
</div>
<div class="div2">
</div>
</body>
</html>

8:方法八:父级定义:zoom:1     br加clear:both

利:不明显

弊:增加多余的标签

总结:可用性低,不推荐使用

代码示例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">
.box{
width: 960px;
margin:20px 0 20px 0;
background: blue;
zoom:1 ;
}

.div1{
width:200px ;
height:200px ;
background:red ;
margin:32px 0px 32px 32px;
float: left;
}
br{
clear: both;
}
.div2{
width:200px ;
height:200px ;
background:yellow ;
}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<br />
</div>
<div class="div2">
</div>
</body>
</html>

9:方法九:增加空标签设置clear:both(理论上clear有clear:left; clear:right,清楚左边的和右边的,除非       你非常确定你要清除的是左或者右,正常情况下可以直接clear:both 清除左右两边的)

利:使用简单,浏览器支持较好

弊:增加多余的空标签,影响整个代码视觉上的流畅性,并且不符合W3C规范 违反结构 样式 行为 三者分离原则

总结:可用度一般,此方法是之前常规的清除浮动的方法

代码示例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">
.box{
width: 960px;
margin:20px 0 20px 0;
background: blue;
zoom:1 ;
}

.div1{
width:200px ;
height:200px ;
background:red ;
margin:32px 0px 32px 32px;
float: left;
}
.clear{
clear: both;
}
.div2{
width:200px ;
height:200px ;
background:yellow ;
}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="clear">

</div>
</div>
<div class="div2">
</div>
</body>
</html>

最后:平常使用清除浮动一般都是我上述的几种方法,各个方法各有利弊,目前伪元素使用相对主流,空标签和         overflow:hidden以及设置元素高度根据实际情况使用,其他几种暂做了解,如果需要可以有选择性的使用。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值