第一種:
<!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;}