清除浮動

清除浮動指清除掉元素float屬性。

假設了有三個盒子對象,一個父級里包含了兩個子級,子級一個使用了float:left屬性,另外一個子級使用float:right屬性。同時設定div css border,父級css框線顏色為紅色,兩個子級框線顏色為藍色;父級CSS背景樣式為黃色,兩個子級背景為白色;父級css width寬度為400px,兩個子級css寬度均為180px,兩個子級再設定相同高度100px,父級css height高度暫不設定(通常為實際css布局時候這樣父級都不設定高度,而高度是隨內容增加自適應高度)。

清除浮動介紹,清除浮動方法,

清除浮動介紹

假設了有三個盒子對象,一個父級里包含了兩個子級,子級一個使用了float:left屬性,另外一個子級使用float:right屬性。同時設定div css border,父級css框線顏色為紅色,兩個子級框線顏色為藍色;父級CSS背景樣式為黃色,兩個子級背景為白色;父級css width寬度為400px,兩個子級css寬度均為180px,兩個子級再設定相同高度100px,父級css height高度暫不設定(通常為實際css布局時候這樣父級都不設定高度,而高度是隨內容增加自適應高度)。

清除浮動方法

給父級也加浮動
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style>
.box{ width:300px;margin:0 auto;border:10px solid #000; float:left;}
.div{ width:200px;height:200px;background:red;float:left;}
/*
清浮動
1.給父級也加浮動(不居中了)
*/
</style>
</head>
<body>
<div class="box">
<div class="div"></div>
</div>
</body>
</html>
給父級加display:inline-block;
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style>
.box{ width:300px;margin:0 auto;border:10px solid #000; display:inline-block;}
.div{ width:200px;height:200px;background:red;float:left;}
/*
清浮動
1.給父級也加浮動
2.給父級加display:inline-block
*/
</style>
</head>
<body>
<div class="box">
<div class="div"></div>
</div>
</body>
</html>
加<div class="clear"></div>
.clear{ height:0px;font-size:0;clear:both;}但是在ie6下,塊元素有最小高度,即當height<19px時,默認為19px,解決方法:font-size:0;或overflow:hidden;
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style>
.box{ width:300px;margin:0 auto;border:10px solid #000;}
.div{ width:200px;height:200px;background:red;float:left;}
.clear{ height:0px;font-size:0;clear:both;}
/*
清浮動
1.給父級也加浮動
2.給父級加display:inline-block
3.在浮動元素下加<div class="clear"></div>
.clear{ height:0px;font-size:0;clear:both;}
*/
</style>
</head>
<body>
<div class="box">
<div class="div"></div>
<div class="clear"></div>
</div>
</body>
</html>
在浮動元素下加<br clear="all">
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style>
.box{ width:300px;margin:0 auto;border:10px solid #000;}
.div{ width:200px;height:200px;background:red;float:left;}
/*
清浮動
1.給父級也加浮動
2.給父級加display:inline-block
3.在浮動元素下加<div class="clear"></div>
.clear{ height:0px;font-size:0;clear:both;}
4.在浮動元素下加<br clear="all"/>
*/
</style>
</head>
<body>
<div class="box">
<div class="div"></div>
<br clear="all"/>
</div>
</body>
</html>
給浮動元素父級加{zoom:1;}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style>
.box{margin:0 auto;border:10px solid #000;}
.div{ width:200px;height:200px;background:red;float:left;}
.clear{zoom:1;}
.clear:after{content:""; display:block;clear:both;}
/*
清浮動
1.給父級也加浮動
2.給父級加display:inline-block
3.在浮動元素下加<div class="clear"></div>
.clear{ height:0px;font-size:0;clear:both;}
4.在浮動元素下加<br clear="all"/>
5. 給浮動元素的父級加{zoom:1;}
:after{content:""; display:block;clear:both;}
**在IE6,7下浮動元素的父級有寬度就不用清浮動
haslayout 根據元素內容的大小 或者父級的父級的大小來重新的計算元素的寬高
display: inline-block
height: (任何值除了auto)
float: (left 或 right)
width: (任何值除了auto)
zoom: (除 normal 外任意值)
*/
</style>
</head>
<body>
<div class="box clear">
<div class="div"></div>
</div>
</body>
</html>
給浮動元素父級加overflow:hidden;
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style>
.box{ width:300px;border:1px solid #000;overflow:hidden;}
.div1{ width:260px;height:400px;background:Red;float:left;}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
</div>
</body>
</html>

相關詞條

熱門詞條

聯絡我們