<head>
<meta charset="UTF-8">
<title>嵌套块元素外边距塌陷</title>
<style>
.father{
width: 500px;
height: 600px;
background-color: deeppink;
margin-top: 100px;
/*overflow: hidden;*/
/*border: red 2px solid;*/
/*padding-top: 1px;*/
}
.son{
width: 200px;
height: 200px;
background-color: blue;
margin-top: 200px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
问题说明:
- 当儿子有上外边距时,如果父亲也有上外边距,这时会取较大者的值作为父亲的上外边距,儿子就padding-top值就为0了。(如上图)。
解决方法:
- 注释中的都是解决办法;
- 为父亲加一个边框、上内边距都可以解决;
- 像注释中加
overflow:hidden
属性; - 浮动、固定、绝对定位的盒子不会有塌陷问题。