项目场景:
<style>
.wrapper{
width: 200px;
height: 200px;
background-color: red;
margin-left: 100px;
margin-top: 100px;
}
.demo{
width: 100px;
height: 100px;
background-color: green;
margin-left: 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="demo"></div>
</div>
</body>
子元素设置了 margin-left值 和 margin-top 值
问题描述:
理想中的效果图
但是 实际的效果是
也就是说这里,水平方向的margin生效了,但是垂直方向的margin没有生效,这种现象好像父级的上边框没了,上边框塌陷了,所以称之为 边框塌陷
解决方案:
- 父元素添加 border: 1px solid transparent
- 触发盒子BFC
- 给子元素添加下列任一属性
1.position: absolute
2.display:inline-block
3.float:left
4.overflow:hidden
总结:边框塌陷不能被彻底解决,所有方案只能是弥补!