Chr*_*oph 25
您可以尝试以下内容:
h1{
margin-bottom:<x>px;
}
div{
margin-bottom:<y>px;
}
div:last-of-type{
margin-bottom:0;
}
Run Code Online (Sandbox Code Playgroud)
或者代替第一条h1规则:
div:first-of-type{
margin-top:<x>px;
}
Run Code Online (Sandbox Code Playgroud)
甚至更好地使用相邻的兄弟选择器.使用以下选择器,您可以在一个规则中覆盖您的案例:
div + div{
margin-bottom:<y>px;
}
Run Code Online (Sandbox Code Playgroud)
分别h1 + div控制头部后面的第一个div,为您提供额外的样式选项.
如果您不需要IE6的支持:
h1 {margin-bottom:20px;}
div + div {margin-top:10px;}
Run Code Online (Sandbox Code Playgroud)
第二行添加div之间的间距,但不会在第一个div之前或最后一个之后添加任何内容.
为什么不使用保证金?您可以将各种页边距应用到元素。不只是周围的全部利润。
您应该使用css类,因为它引用了多个元素,并且可以为那些想要与众不同的对象使用id
即:
<style>
.box { height: 50px; background: #0F0; width: 100%; margin-top: 10px; }
#first { margin-top: 20px; }
#second { background: #00F; }
h1.box { background: #F00; margin-bottom: 50px; }
</style>
<h1 class="box">Hello World</h1>
<div class="box" id="first"></div>
<div class="box" id="second"></div>?
Run Code Online (Sandbox Code Playgroud)
这是一个jsfiddle示例:
参考: