1.
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#box1{
width:400px;
height:300px;
background:grey ;
}
#box2{
width:200px;
height:100px;
background:yellow;
margin-top:30px;
}
</style>
</head>
<body>
<div id="box1">
<div id="box2"></div>
</div>
</body>
</html>
如代码及图,父子级包含的时候,子级的margin-top会传递给父级;(内边距替代外边距)
要想让黄色框离灰色框上面有30像素的距离,我们可以把#box1中添加属性:padding-top:30px;
2.如果同时给上个块设置下外边距和下个块设置上外边距,上下外边距会叠压。
3.margin-left:auto;
margin-right:auto;
同时设置这两个属性可以是div始终居中,不随浏览器窗口的大小变化。