<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.left {
margin-right: 10px;
}
.right {
margin-left: 10px;
}
/*标签之间的margin会合并但不会相加,它会找最大值来执行
如果一个margin-left:50px一个margin-right:100px
那他们水平间隔就是100px
*/
.top,.bottom {
width: 100px;
height: 100px;
background: greenyellow;
}
.top {
margin-bottom: 10px;
}
.bottom {
margin-top: 20px;
}
.one{
margin-top:10px;
width:400px;
height:400px;
background:pink;
/* 解决外边距塌陷的第一种方法 */
/* padding-top:10px; */
/* 解决外边距塌陷的第二种方法
overflow:hidden;
*/
}
.two{
width:200px;
height:200px;
background: orange;
margin:10px auto;/* 此时父级div也会跟着下来,出现外边距塌陷 */
}
</style>
</head>
<body>
<span class="left">左边</span>
<span class="right">右边</span>
<div class="top"></div>
<div class="bottom"></div>
<div class="one">
<div class="two"></div>
</div>
</body>
</html>
外边距塌陷及解决办法
最新推荐文章于 2024-04-29 15:35:16 发布