外边距合并:垂直方向的两个盒子,如果都设置了垂直方向的外边距,则取较大值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style >
.one {
width: 300px;
height: 300px;
background-color: orange;
/* margin-bottom: 20px; */
}
.two {
width: 100px;
height: 100px;
background-color: blue;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="one"></div>
<div class="two"></div>
</body>
</html>
外边距塌陷:嵌套的两个盒子,直接给子盒子设置外边距margin-top, 会发生外边距塌陷
解决办法有两种 1 给父盒子设置边框 2 给父盒子设置overflow: hidden;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style >
.father {
width: 498px;
height: 400px;
background-color: orange;
/* border: 1px solid orange; */
overflow: hidden;
}
.son {
width: 100px;
height: 100px;
background-color: blue;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>