1.额外标签法
<!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>
.nav {
width: 600px;
margin: 0 auto;
}
.first {
width: 200px;
height: 200px;
float: left;
background-color: #333;
}
.second {
width: 200px;
height: 200px;
float: left;
background-color: #555;
}
.footer {
height: 200px;
background-color: skyblue;
}
.clear {
clear: both;
}
</style>
</head>
<body>
<div class="nav">
<div class="first"></div>
<div class="second"></div>
<div class="clear"></div>
</div>
<div class="footer"></div>
</body>
</html>
2.父元素添加overflow属性
<!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>
.nav {
width: 600px;
margin: 0 auto;
overflow: hidden;
}
.first {
width: 200px;
height: 200px;
float: left;
background-color: #333;
}
.second {
width: 200px;
height: 200px;
float: left;
background-color: #555;
}
.footer {
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="nav">
<div class="first"></div>
<div class="second"></div>
</div>
<div class="footer"></div>
</body>
</html>
3.after伪元素法
<!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>
.clearfix:after {
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
*zoom: 1;
}
.nav {
width: 600px;
margin: 0 auto;
}
.first {
width: 200px;
height: 200px;
float: left;
background-color: #333;
}
.second {
width: 200px;
height: 200px;
float: left;
background-color: #555;
}
.footer {
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="nav clearfix">
<div class="first"></div>
<div class="second"></div>
</div>
<div class="footer"></div>
</body>
</html>
3.双伪元素法
<!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>
.clearfix::before,
.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
.nav {
width: 600px;
margin: 0 auto;
}
.first {
width: 200px;
height: 200px;
float: left;
background-color: #333;
}
.second {
width: 200px;
height: 200px;
float: left;
background-color: #555;
}
.footer {
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="nav clearfix">
<div class="first"></div>
<div class="second"></div>
</div>
<div class="footer"></div>
</body>
</html>