浮动元素容易造成父元素高度塌陷,使用overflow可使父元素自适应浮动元素最高高度。
光说不练假把式
代码:
<!DOCTYPE html>
<html lang="en">
<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 type="text/css">
.a{
/* overflow: hidden; */
background-color: brown;
}
.b{
float: left;
width: 200px;
height: 200px;
background-color: aqua;
}
/* .c{
float: left;
width: 200px;
height: 300px;
background-color: aqua;
} */
</style>
</head>
<body>
<div class="a">
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
</div>
</body>
</html>
效果:
看,没有父元素的颜色。
代码:
<!DOCTYPE html>
<html lang="en">
<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 type="text/css">
.a{
overflow: hidden;
background-color: brown;
}
.b{
float: left;
width: 200px;
height: 200px;
background-color: aqua;
}
/* .c{
float: left;
width: 200px;
height: 300px;
background-color: aqua;
} */
</style>
</head>
<body>
<div class="a">
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
</div>
</body>
</html>
效果:
加上overflow之后出现了颜色
再来看看自适应最高高度
<!DOCTYPE html>
<html lang="en">
<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 type="text/css">
.a{
overflow: hidden;
background-color: brown;
}
.b{
float: left;
width: 200px;
height: 200px;
background-color: aqua;
}
.c{
float: left;
width: 200px;
height: 300px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="a">
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
</div>
</body>
</html>
效果: