目录
[1. 清除浮动](#1. 清除浮动)
[2. 清除浮动本质](#2. 清除浮动本质)
[3. 清除浮动](#3. 清除浮动)
[4. 清除浮动方法](#4. 清除浮动方法)
[4.1 额外标签法](#4.1 额外标签法)
[4.1.1 总结](#4.1.1 总结)
[4.2 父级添加 overflow](#4.2 父级添加 overflow)
[4.3 after 伪元素法](#4.3 after 伪元素法)
[4.4 双伪元素清除浮动](#4.4 双伪元素清除浮动)
[5 总结](#5 总结)
1. 清除浮动



这是上面的源代码:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 不给高度,只给宽度,其中的内容会撑开外面的大盒子 */
.box {
background-color: bisque;
width: 600px;
border: 5px red solid;
}
/* 添加浮动后,不给高度的大盒子高度为 0,因为浮动的元素不占有位置 */
.one,.two {
width: 200px;
height: 200px;
background-color: aquamarine;
float: left;
}
/* 布局再添加一个 footer,布局就会更加乱 */
.footer {
height: 200px;
background-color: burlywood;
}
</style>
</head>
<body>
<div class="box">
<div class="one">111</div>
<div class="two">222</div>
</div>
<div class="footer"></div>
</body>
</html>
2. 清除浮动本质

3. 清除浮动



4. 清除浮动方法

4.1 额外标签法

html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 不给高度,只给宽度,其中的内容会撑开外面的大盒子 */
.box {
background-color: bisque;
width: 600px;
border: 5px red solid;
}
/* 添加浮动后,不给高度的大盒子高度为 0,因为浮动的元素不占有位置 */
.one,.two {
width: 200px;
height: 200px;
background-color: aquamarine;
float: left;
}
/* 布局再添加一个 footer,布局就会更加乱 */
.footer {
height: 200px;
background-color: burlywood;
}
.cle {
clear: both;
}
</style>
</head>
<body>
<div class="box">
<div class="one">111</div>
<div class="two">222</div>
<div class="cle"></div>
</div>
<div class="footer"></div>
</body>
</html>
4.1.1 总结

4.2 父级添加 overflow

html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 不给高度,只给宽度,其中的内容会撑开外面的大盒子 */
.box {
overflow: hidden;
background-color: bisque;
width: 600px;
border: 5px red solid;
}
/* 添加浮动后,不给高度的大盒子高度为 0,因为浮动的元素不占有位置 */
.one,.two {
width: 200px;
height: 200px;
background-color: aquamarine;
float: left;
}
.footer {
height: 200px;
background-color: burlywood;
}
</style>
</head>
<body>
<div class="box">
<div class="one">111</div>
<div class="two">222</div>
</div>
<div class="footer"></div>
</body>
</html>
4.3 after 伪元素法


使用时直接复制以下代码即可:
html
.clearfix:after {
content:"";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
/* .clearfix {
*zoom: 1;
} */
4.4 双伪元素清除浮动

使用时直接复制以下代码即可:
html
.clearfix:before,.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
/* .clearfix {
*zoom: 1;
} */
5 总结

