说明:css
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>清除浮动的六种方法</title>
<style> *{ padding: 0; margin: 0; } .wrap{ height: 300px;/*固定父元素的高度*/ } .box1,.box2{ width: 400px; height: 300px; float: left; } .box1{ background-color: yellow; } .box2{ background-color: deepskyblue; } .box3{ width: 900px; height: 200px; background-color: deeppink; } </style>
</head>
<body>
<div class="wrap">
<div class="box1"></div>
<div class="box2"></div>
</div>
<div class="box3"></div>
</body>
</html>
复制代码
说明:html
clear属性表示清除浮动元素对本身的影响bash
clear属性有三个值,left,right,both,left表示清除本元素左边浮动元素本身的影响,right表示清除本元素右边浮动元素对本身的影响,both表示清除两边浮动元素对本身的影响,通常咱们使用both学习
该属性通常写在公共类中,使用时给须要的元素添加该类名便可ui
该方法有个致命的缺点就是:margin属性部分失效spa
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>清除浮动的六种方法</title>
<style> *{ padding: 0; margin: 0; } .box1,.box2{ width: 400px; height: 300px; float: left; } .box1{ background-color: yellow; } .box2{ background-color: deepskyblue; } .box3{ width: 900px; height: 200px; clear: both;/*清除浮动*/ background-color: deeppink; } </style>
</head>
<body>
<div class="wrap">
<div class="box1"></div>
<div class="box2"></div>
</div>
<div class="box3"></div>
</body>
</html>
复制代码
clear:both直接加在想要清除影响的元素上会形成该元素margin值部分失效,可使用隔墙法firefox
外隔墙code
说明:htm
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>清除浮动的六种方法</title>
<style> *{ padding: 0; margin: 0; } .box1,.box2{ width: 400px; height: 300px; float: left; } .box1{ background-color: yellow; } .box2{ background-color: deepskyblue; } .box3{ width: 900px; height: 200px; background-color: deeppink; } /*公共类*/ .clearfix{ clear: both; } .h20{ height: 20px; } </style>
</head>
<body>
<div class="wrap">
<div class="box1"></div>
<div class="box2"></div>
</div>
<div class="clearfix h20"></div>
<div class="box3"></div>
</body>
</html>
复制代码
内隔墙string
说明:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>清除浮动的六种方法</title>
<style> *{ padding: 0; margin: 0; } .box1,.box2{ width: 400px; height: 300px; float: left; } .box1{ background-color: yellow; } .box2{ background-color: deepskyblue; } .box3{ width: 900px; height: 200px; background-color: deeppink; } /*公共类*/ .clearfix{ clear: both; } .h20{ height: 20px; } </style>
</head>
<body>
<div class="wrap">
<div class="box1"></div>
<div class="box2"></div>
<div class="clearfix h20"></div>
</div>
<div class="box3"></div>
</body>
</html>
复制代码
说明:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>清除浮动的六种方法</title>
<style>
*{
padding: 0;
margin: 0;
}
.wrap{
overflow:hidden;
_zoom:1;/*兼容ie6*/
}
.box1,.box2{
width: 400px;
height: 300px;
float: left;
}
.box1{
background-color: yellow;
}
.box2{
background-color: deepskyblue;
}
.box3{
width: 900px;
height: 200px;
background-color: deeppink;
}
</style>
</head>
<body>
<div class="wrap">
<div class="box1"></div>
<div class="box2"></div>
</div>
<div class="box3"></div>
</body>
</html>
复制代码
说明:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>清除浮动的六种方法</title>
<style> *{ padding: 0; margin: 0; } .clearfix:after{/*建议以公共类的形式出现*/ content: '.'; display: block; height: 0; clear: both; visibility: hidden; } .clearfix {*zoom: 1;} /* IE六、7 专有 */ .box1,.box2{ width: 400px; height: 300px; float: left; } .box1{ background-color: yellow; } .box2{ background-color: deepskyblue; } .box3{ width: 900px; height: 200px; background-color: deeppink; } </style>
</head>
<body>
<div class="wrap clearfix">
<div class="box1"></div>
<div class="box2"></div>
</div>
<div class="box3 "></div>
</body>
</html>
复制代码
说明:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>清除浮动的六种方法</title>
<style> *{ padding: 0; margin: 0; } .clearfix:before,.clearfix:after { content:""; display:table; } .clearfix:after { clear:both; } .clearfix { *zoom:1;/*ie6-7*/ } .box1,.box2{ width: 400px; height: 300px; float: left; } .box1{ background-color: yellow; } .box2{ background-color: deepskyblue; } .box3{ width: 900px; height: 200px; background-color: deeppink; } </style>
</head>
<body>
<div class="wrap clearfix">
<div class="box1"></div>
<div class="box2"></div>
</div>
<div class="box3 "></div>
</body>
</html>
复制代码