清除浮动的三种方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
border: 1px solid red;
}
.box div {
width: 40%;
height: 100px;
}
.left {
background: pink;
}
.right {
background: blue;
}
</style>
</head>
<body>
<div class="box">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
</body>
</html>
如图所示:
给left、right添加浮动:
.left {
float: left;
}
.right {
float: right;
}
如图所示:
父元素的高度塌陷
一、最简单的处理方式 给父元素添加 overflow:hidden
.box {
overflow: hidden;
}
如图所示:
二、在浮动元素添加 clear:both
<div class="box">
<div class="left">
left
</div>
<div class="right">
right
</div>
<p style="clear: both"></p>
</div>
三、给父元素添加 :after { }
.box:after{
/* content内容可以省略,但是content属性不能省略 */
content: "";
display: block;
clear: both;
}