清除浮动的四种常用方法及其优缺点
浮动带来的问题
众所周知,清除浮动主要是为了解决如下两个问题
1,子元素浮动导致父元素高度为0
浮动前
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.container{
width:300px;
background: #f3e5f5;
border: 5px solid #6a1b9a;
}
.box1{
width:100px;
height: 100px;
background: #ba68c8;
}
.box2{
width:100px;
height: 100px;
background: #ab47bc;
}
</style>
</head>
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>
浮动后
.box1{
width:100px;
height: 100px;
background: #ba68c8;
float:left;
}
.box2{
width:100px;
height: 100px;
background: #ab47bc;
float:left;
}
2,元素浮动后释放文档流导致布局混乱
浮动前
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box0{
width:100px;
height: 100px;
background: #ffcc80;
}
.container{
width:300px;
background: #f3e5f5;
border: 5px solid #6a1b9a;
}
.box1{
width:100px;
height: 100px;
background: #ba68c8;
}
.box2{
width:100px;
height: 100px;
background: #ab47bc;
}
</style>
</head>
<body>
<div class="box0"></div>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>
浮动后
.box0{
width:100px;
height: 100px;
background: #ffcc80;
float:left;
}
清除浮动的四种常用方法
1,额外标签法
在container div的最后位置添加一个新的元素,并为其设置clear:both;
接着之前父元素因为子元素浮动高度变为0的例子,我们给container div添加一个p元素并为其设置clear:both;
,如下
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.container{
width:300px;
background: #f3e5f5;
border: 5px solid #6a1b9a;
}
.box1{
width:100px;
height: 100px;
background: #ba68c8;
float:left;
}
.box2{
width:100px;
height: 100px;
background: #ab47bc;
float:left;
}
.p1{
clear:both;
}
</style>
</head>
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
<p class="p1">我是额外标签</p>
</div>
</body>
</html>
可以看到浮动被清除了,父元素的高度也恢复了。
方法点评:
优点: 容易理解。
缺点: 语义化差,添加了无意义的元素
推荐指数: ☆☆☆☆☆
2,设置overflow触发BFC
通过设置overflow:hidden;
或者overflow:auto;
来触发BFC特性,使得其可以清除浮动。理论上讲,只要触发了BFC特性,该元素就可以清除浮动(即可包含浮动元素)。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.container{
width:300px;
background: #f3e5f5;
border: 5px solid #6a1b9a;
overflow:hidden;
}
.box1{
width:100px;
height: 100px;
background: #ba68c8;
float:left;
}
.box2{
width:100px;
height: 100px;
background: #ab47bc;
float:left;
}
</style>
</head>
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>
方法点评:
优点: 使用简单,一小段代码即可生效。
缺点: 无法显示溢出的内容或者产生不美观的滚动条。
推荐指数: ☆☆☆☆☆
3,设置after伪元素
通过给container div设置after伪元素来清除浮动,其原理和额外标签法类似,但是after伪元素不占用DOM。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.container{
width:300px;
background: #f3e5f5;
border: 5px solid #6a1b9a;
}
.container::after{
content: "";
display: block;
clear:both;
}
.clearfix{
*zoom: 1; /*IE6清除浮动的方式 *号只有IE6-IE7执行,其他浏览器不执行*/
}
.box1{
width:100px;
height: 100px;
background: #ba68c8;
float:left;
}
.box2{
width:100px;
height: 100px;
background: #ab47bc;
float:left;
}
</style>
</head>
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>
方法点评:
优点: 不占用DOM,渲染效率提高,结构语义化更好。
缺点: IE6-7不支持伪元素::after,考虑兼容得添加zoom:1触发hasLayout来清除浮动。
推荐指数: ★★★☆☆
4,设置before和after双伪元素
与设置after伪元素同理,增加设置before伪元素是为了解决元素上方浮动元素所带来的影响
增设before伪元素前
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box0{
width:100px;
height: 100px;
background: #ffcc80;
float:left;
}
.container{
width:300px;
/* background: #f3e5f5; */
/* border: 5px solid #6a1b9a; */
}
.container::after{
content: "";
display: block;
clear:both;
}
.clearfix{
*zoom: 1; /*IE6清除浮动的方式 *号只有IE6-IE7执行,其他浏览器不执行*/
}
.box1{
width:100px;
height: 100px;
background: #ba68c8;
float:left;
}
.box2{
width:100px;
height: 100px;
background: #ab47bc;
float:left;
}
</style>
</head>
<body>
<div class="box0"></div>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>
此例中,我们希望box0和container div各在自己的一行,这就需要用到before伪元素
增设before伪元素后
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box0{
width:100px;
height: 100px;
background: #ffcc80;
float:left;
}
.container{
width:300px;
/* background: #f3e5f5;
border: 5px solid #6a1b9a; */
}
.container::before,.container::after{
content: "";
display: block;
clear:both;
}
.clearfix{
*zoom: 1; /*IE6清除浮动的方式 *号只有IE6-IE7执行,其他浏览器不执行*/
}
.box1{
width:100px;
height: 100px;
background: #ba68c8;
float:left;
}
.box2{
width:100px;
height: 100px;
background: #ab47bc;
float:left;
}
</style>
</head>
<body>
<div class="box0"></div>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>
方法点评:
优点: 在设置单个after伪元素的优点的基础上,还确保了被设置容器的所在行的独立性。
缺点: IE6-7不支持伪元素::after,考虑兼容得添加zoom:1触发hasLayout来清除浮动。
推荐指数: ★★★★☆