外边距合并
一个元素与另一个元素之间在垂直方向上合并起来。
解决方案:
1、给父级加内边距来撑开垂直方向的内容。
<html>
<head>
<meta charset='utf-8'/>
<title> Document</title>
<meta name='keywords' content=''/>
<meta name='description' content=''/>
<style type="text/css">
body{
margin:0;
}
.mydiv1{
width:500px;
height:500px;
background-color:#90f;
/* margin-top:50px; */
padding-top:50px;
box-sizing:border-box;
}
.mydiv2{
width:300px;
height:300px;
background-color:#ff0;
/* margin-top:50px; */
}
.mydiv3{
width:100px;
height:100px;
background-color:deeppink;
}
</style>
</head>
<body>
<div class="mydiv1">
<div class="mydiv2">
<div class="mydiv3"></div>
</div>
</div>
</body>
</html>
2、给父级加边框线来阻止触发BFC机制
<html>
<head>
<meta charset='utf-8'/>
<title> Document</title>
<meta name='keywords' content=''/>
<meta name='description' content=''/>
<style type="text/css">
body{
margin:0;
}
.mydiv1{
width:500px;
height:500px;
background-color:#90f;
/* margin-top:50px; */
/* padding-top:50px; */
/* box-sizing:border-box; */
border:1px solid transparent;
}
.mydiv2{
width:300px;
height:300px;
background-color:#ff0;
margin-top:50px;
}
.mydiv3{
width:100px;
height:100px;
background-color:deeppink;
}
</style>
</head>
<body>
<div class="mydiv1">
<div class="mydiv2">
<div class="mydiv3"></div>
</div>
</div>
</body>
</html>
兄弟元素之间外边距合并:
元素1加下外边距,元素2加上外边距;
两者之间会产生外边距合并,外边距取两者中的最大值;
解决方案:
直接给一方加设定的外边距;
<!doctype html>
<html>
<head>
<meta charset='utf-8'/>
<title> Document</title>
<meta name='keywords' content=''/>
<meta name='description' content=''/>
<style type="text/css">
body{
margin:0;
}
.mydiv1{
width:200px;
height:200px;
background-color:#90f;
border:1px solid transparent;
margin-bottom:130px;
}
.mydiv2{
width:300px;
height:300px;
background-color:#ff0;
}
</style>
</head>
<body>
<div class="mydiv1"></div>
<div class="mydiv2"></div>
</body>
</html>
布局技巧
1、从外面到里面
2、从简单到复杂
3、从大到小
4、使用辅助线(边框线,背景颜色)