目录
什么叫做外边距塌陷
当两个嵌套关系块级元素,若父元素设置上外边距或者没有设置上外边距(margin-top),子元素设置上外边距时,两个上外边距会发生合并,且值为相对较大的上外边距值。
示例:
<style>
.box1{
width: 200px;
height: 200px;
background-color: aqua;
margin-top: 50px;
}
.box2{
width: 100px;
height: 100px;
background-color: aquamarine;
margin-top: 100px;
}
</style>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
页面渲染效果:
我们所希望的是父盒子与页面顶部距离为50px,子盒子与父盒子顶部距离为100px。然而实际设置效果是:给父盒子设置的上外边距没有效果,而子盒子也没有和父盒子顶部产生距离,子盒子随着父盒子距页面顶部下降100px,造成父子元素之间的高度塌陷。