对于h1和p,浏览器自动会为他们加上margin-top和margin-bottom。
<h1>
Obama says Warren Buffett is right about taxes
</h1>
<p >
CANNON FALLS, Minnesota (Reuters) - Small-town Americans probably don't make as much money as Warren Buffett, but they pay more of their income in taxes, President Barack Obama said on Monday, citing the billionaire investor to argue that the government needs more revenues to balance the budget.
</p>
如果要移除h1与p之间的间隔,我们很容易想到使用如下CSS规则:
h1{
margin-bottom:0;
}
p{
margin-top:0;
}
但是,这样做会使得网页中所有p段落都失去了margin-top,那么我们能不能告诉浏览器:只帮我把紧跟h1之后的p去掉margin-top呢?
答案是肯定的,在CSS2.0中就提供了这样的功能:Adjacent Seletor(临近选择符)
h1{
margin-bottom:0;
}
h1 + p{
margin-top:0;
}
这样,上诉代码就只会去掉紧跟h1之后的p的margin-top。
兼容性:IE7+=