第一種
額外標籤清除法
只要添加這句代碼就可以
<div style="clear: both;"></div>
優點:簡單易懂
缺點:添加了很多額外沒用的標籤破壞結構
事例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>額外標籤 清除浮動</title>
<style>
.father {
}
.big {
width: 200px;
height: 200px;
background-color: blue;
float: left;
}
.small {
width: 100px;
height: 100px;
background-color: yellow;
float: left;
}
.footer {
width: 400px;
height: 100px;
background-color: pink;
}
/* 清除浮動 */
/* .clear {
clear: both;
如果清除了浮動, 父親去自動檢測孩子的高度 以最高的爲準
} */
</style>
</head>
<body>
<div class="father">
<div class="big"></div>
<div class="small"></div>
</div>
<!-- 額外標籤清除浮動 -->
<!-- <div class="clear"></div> -->
<div style="clear: both;"></div> <!-- 優點:簡單易懂 缺點:添加了很多額外沒用的標籤破壞結構 -->
<!-- 額外標籤清除浮動 -->
<div class="footer"></div>
</body>
</html>
第二種
overflow清除法
在父盒子裏面加 overflow: hidden;
優點:代碼簡潔;
缺點:內容增多時候容易造成不會自動換行導致內容被隱藏掉
事例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>overflow 清除浮動</title>
<style>
.father {
/* 在父元素這裏加上 overflow :hidden; 就可以清除浮動
不是所有的浮動都要清除 誰影響就清除誰
優點:代碼簡潔;
缺點:內容增多時候容易造成不會自動換行導致內容被隱藏掉
無法顯示需要溢出的元素; */
overflow: hidden;
}
.big {
width: 200px;
height: 200px;
background-color: blue;
float: left;
}
.small {
width: 100px;
height: 100px;
background-color: yellow;
float: left;
}
.footer {
width: 400px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div class="father">
<div class="big"></div>
<div class="small"></div>
</div>
<div class="footer"></div>
</body>
</html>
第三種
after僞元素清除法
.clearfix:after{ /* 正常瀏覽器 清除浮動 */
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix{
*zoom: 1; /* zoom 1 就是ie6 清除浮動的方法 * 是ie7以下的版本所識別 */
}
事例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>overflow 清除浮動</title>
<style>
.clearfix:after{ /* 正常瀏覽器 清除浮動 */
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix{
*zoom: 1; /* zoom 1 就是ie6 清除浮動的方法 * 是ie7以下的版本所識別 */
}
.father {
}
.big {
width: 200px;
height: 200px;
background-color: blue;
float: left;
}
.small {
width: 100px;
height: 100px;
background-color: yellow;
float: left;
}
.footer {
width: 400px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div class="father clearfix">
<div class="big"></div>
<div class="small"></div>
</div>
<div class="footer"></div>
</body>
</html>
第四種
雙僞元素標記法
添加如下
.clearfix:before, .clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
事例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>overflow 清除浮動</title>
<style>
.clearfix:before, .clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
.father {
}
.big {
width: 200px;
height: 200px;
background-color: blue;
float: left;
}
.small {
width: 100px;
height: 100px;
background-color: yellow;
float: left;
}
.footer {
width: 400px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div class="father clearfix">
<div class="big"></div>
<div class="small"></div>
</div>
<div class="footer"></div>
</body>
</html>