外边距合并(塌陷)问题
2019-03-18 本文已影响0人
xing222333
第一种:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
width: 200px;
height: 200px;
}
#one {
background-color: #00B4FF;
margin-bottom: 100px;
}
#two {
background-color: #2AB561;
margin-top: 20px;
}
</style>
</head>
<body>
<div id="one"></div>
<div id="two"></div>
</body>
</html>

看代码2个div应该相距120px,
但是实际上只相距100px,
大距离吞掉小距离就是外边距合并现象
解决方案:避免上下都加外边框的写法
第二种:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#one {
width: 50px;
height: 50px;
background-color: #00B4FF;
margin-top: 30px;
}
#two {
width: 30px;
height: 30px;
background-color: #2AB561;
margin-top: 20px;
}
</style>
</head>
<body>
<div id="one">
<div id="two"></div>
</div>
</body>
</html>

绿色div 并没有相对蓝色div产生上边距
解决方案:增加样式#one{overflow: hidden;}