幾種對于html清除浮動的方法
發表時間:2023-09-08 來源:明輝站整理相關軟件相關文章人氣:
[摘要]本文講了6種清除HTML元素浮動的方法供大家參考使用,具體下看下文使用display:inline-block會出現的情況:1.使塊元素在一行顯示2.使內嵌支持寬高3.換行被解析了4.不設置的時候寬度由內容撐開5.在IE6,7下步支持塊標簽由于inline-block屬性換行的時候被解析(有間隙)...
本文講了6種清除HTML元素浮動的方法供大家參考使用,具體下看下文
使用display:inline-block會出現的情況:
1.使塊元素在一行顯示
2.使內嵌支持寬高
3.換行被解析了
4.不設置的時候寬度由內容撐開
5.在IE6,7下步支持塊標簽
由于inline-block屬性換行的時候被解析(有間隙)故解決方法使用浮動float:left/right
使用浮動時出現的情況:
1.使塊元素在一行顯示
2.使內嵌元素支持寬高
3.不設置不寬高的時候寬度由內容撐開
4.換行不被解析(故使用行內元素的時候清除間隙的方法可以使用浮動)
5.元素添加浮動,會脫離文檔流,按照指定的一個方向移動,直到碰到父級的邊界或者另一個浮動元素停止(文檔流是文檔中可顯示對象在排列時所占用的位置)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style>
p,span{height:100px;background:red;border:1px solid #000; float:left;}
/*
inline-block
1.使塊元素在一行顯示
2.使內嵌支持寬高
3.換行被解析了
4.不設置寬度的時候寬度由內容撐開
5.在IE6,7下不支持塊標簽
浮動:
1.使塊元素在一行顯示
2.使內嵌支持寬高
3.不設置寬度的時候寬度由內容撐開
*/
</style>
</head>
<body>
<p class="p1">p1</p>
<p class="p2">p2</p>
<span class="span1">span1</span>
<span class="span2">span2</span>
</body>
</html>
下面的代碼只有box1浮動,則box1,box2重疊一起。兩者都浮動就不會重疊
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style>
.box1{ width:100px;height:100px;background:red; float:left;}
.box2{ width:200px;height:200px;background:blue; /* float:left;*/}
</style>
</head>
<body>
<p class="box1"></p>
<p class="box2"></p>
</body>
</html>
清浮動的方法:
1.給父級也加浮動(這種情況當父級margin:0 auto;時不居中)
<!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;}
.p{ width:200px;height:200px;background:red;float:left;}
/*
清浮動
1.給父級也加浮動(不居中了)
*/
</style>
</head>
<body>
<p class="box">
<p class="p"></p>
</p>
</body>
</html>
2.給父級加display:inline-block;(同方法1,不居中。只有IE6,7居中)
<!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;}
.p{ width:200px;height:200px;background:red;float:left;}
/*
清浮動
1.給父級也加浮動
2.給父級加display:inline-block
*/
</style>
</head>
<body>
<p class="box">
<p class="p"></p>
</p>
</body>
</html>
3.在浮動元素下加<p class="clear"></p>
.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;}
.p{ width:200px;height:200px;background:red;float:left;}
.clear{ height:0px;font-size:0;clear:both;}
/*
清浮動
1.給父級也加浮動
2.給父級加display:inline-block
3.在浮動元素下加<p class="clear"></p>
.clear{ height:0px;font-size:0;clear:both;}
*/
</style>
</head>
<body>
<p class="box">
<p class="p"></p>
<p class="clear"></p>
</p>
</body>
</html>
4.在浮動元素下加<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;}
.p{ width:200px;height:200px;background:red;float:left;}
/*
清浮動
1.給父級也加浮動
2.給父級加display:inline-block
3.在浮動元素下加<p class="clear"></p>
.clear{ height:0px;font-size:0;clear:both;}
4.在浮動元素下加<br clear="all"/>
*/
</style>
</head>
<body>
<p class="box">
<p class="p"></p>
<br clear="all"/>
</p>
</body>
</html>
5.給浮動元素父級加{zoom:1;}
:after{content:""; display:block;clear:both;}
<!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;}
.p{ width:200px;height:200px;background:red;float:left;}
.clear{zoom:1;}
.clear:after{content:""; display:block;clear:both;}
/*
清浮動
1.給父級也加浮動
2.給父級加display:inline-block
3.在浮動元素下加<p class="clear"></p>
.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>
<p class="box clear">
<p class="p"></p>
</p>
</body>
</html>
6.給浮動元素父級加overflow:auto;
<!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:auto;}
.p1{ width:260px;height:400px;background:Red;float:left;}
</style>
</head>
<body>
<p class="box">
<p class="p1"></p>
</p>
</body>
</html>
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
如何使用Html屏蔽右鍵菜單和左鍵劃字功能
關于HTML 文本格式化的代碼
以上就是幾種關于html清除浮動的方法的詳細內容,更多請關注php中文網其它相關文章!
網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。