• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
If She Said "Yes".
What'll you do ?
博客园    首页    新随笔    联系   管理    订阅  订阅
如何移除h1与p之间的间距:临近选择符

     对于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+=

posted on 2011-08-16 17:19  百花盛开  阅读(1756)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3