【问题标题】:Css margin-top vs margin-bottomCSS margin-top vs margin-bottom
【发布时间】:2010-09-22 00:57:57
【问题描述】:

如果您有一系列块元素,并且您想在它们之间放置边距。

您更喜欢哪一个,margin-top 或 margin-bottom 或两者兼而有之?为什么?

【问题讨论】:

  • 不要忘记margin-collapse

标签: css layout


【解决方案1】:

我总是使用margin-bottom,这意味着第一个元素之前没有多余的空格。

【讨论】:

  • 这些天我也在使用这种方法。看起来很自然/合乎逻辑,并且通常在某事之后的边距比上面的边距在逻辑上与该事物更相关,如果这有任何意义的话。但是,与不同类型的项目之间的空间相比,我无法在相同类型的项目(如 P 标签)之间放置更少的空间。从这个页面上的一些例子来看,走上边距路线似乎提供了一个更优雅的解决方案。但我不知道。我想我会针对这个特定问题开始另一个 SO 线程。
  • 在被margin-top 咬了几次之后,我切换到了这个。虽然@sblundy 有一点我从来没有发现自己需要那个解决方案。但话又说回来,我将 React 与 SASS 和 Radium 一起使用,这使得多年来发明的许多 CSS hack 变得过时了。
  • @BAR 我很好奇,到底是什么咬到了你,或者在使用 margin-top 时发生了什么?
【解决方案2】:

取决于上下文。但是,通常margin-top 更好,因为您可以使用:first-child 将其删除。像这样:

div.block {
    margin-top: 10px;
}

div.block:first-child {
    margin-top: 0;
}

这样,边距只在块之间。

显然,仅适用于更现代的浏览器。

【讨论】:

  • 请记住,像这样的伪元素往往会在旧版浏览器中崩溃。
  • 我以前遇到过这样的伪类问题,并且倾向于回避它们,直到更现代的浏览器成为“旧”。
  • satzansatz.de/cssd/pseudocss.html - 更多研究可能会对您有所帮助,但通过快速谷歌搜索,我没有看到任何不涉及黑客攻击的有希望的东西。
  • 您的目标是 IE 6 及更早版本?无论你做什么,都会让事情变得丑陋。
  • 按照这个逻辑,你不能使用 margin-bottom 然后使用 first-child 来 add 上边距吗?那样的话,有什么区别呢?
【解决方案3】:

@This Mat - 我不同意你的做法。我会以语义方式为元素分配间距,并使用上下文选择器来定义该元素集合的行为。

.content p { /* obviously choose a more elegant name */
   margin-bottom: 10px;
}

根据类的行为而不是内容来命名类有点滑坡,并且会混淆 HTML 的语义性质。例如,如果在页面的某个区域,所有具有类 .top10 的元素突然需要 20 个像素,该怎么办?您不必更改单个规则,而是必须创建一个新的类名,并在您想要影响的所有元素上更改它。

要回答最初的问题,这完全取决于您希望元素如何堆叠。您想要顶部或底部的额外空间吗?

【讨论】:

    【解决方案4】:

    是的,我通常也使用 margin-bottom ,然后将最后一个类分配给最后一个类。假设你想要一个不同的样式。

    .discussion .detailed.topics { margin: 20px 0 }    
    .discussion .detailed.topics .topic { margin-bottom: 30px }
    .discussion .detailed.topics .topic.last { margin-bottom: 0 }
    

    当您使用动态驱动的内容时,这是一种强大的方法

    HTML(Razor 视图引擎)

    <div class="detailed topics">   
       @if (Model.ActiveTopics != null && Model.ActiveTopics.Count > 0)
       {
          for (int i = 0; i < Model.ActiveTopics.Count(); i++)
          {
              var topic = Model.ActiveTopics[i];
              <div class="topic@(i == Model.ActiveTopics.Count - 1 ? " last" : "")">
                    ...                                 
              </div>
          }
       }
    </div>
    

    【讨论】:

    • 我实际上使用了与此类似的方法,除了我有一个用于顶部/第一个元素的类。由于边距折叠行为,我通常不需要使用该类。但我喜欢将“奇数”类放在第一个,因为我更有可能附加到块列表而不是前置(因此附加需要较少的编辑以将“最后一个”类移动到最后一个元素)。
    【解决方案5】:

    另一种选择是将+ 选择器与margin-top 结合使用:

    .article + .article {
        margin-top: 10px;
    }
    

    这将选择后一个元素,这意味着该规则根本不适用于第一个元素。元素上方或下方没有额外的类、伪类或空间。

    /* CSS rules for legibility. */
    section, article {
      outline: 1px solid blue;
    }
    
    section {
      background: lightgreen;
      padding: 10px;
    }
    
    section + section {
      margin-top: 30px;
    }
    
    article {
      background: lightblue;
    }
    
    /* Using the + selector. */
    #PlusSelectorExample article + article {
      margin-top: 30px;
    }
    
    /* Using margin-bottom only. */
    #MarginBottomExample article {
      margin-bottom: 30px;
    }
    
    /* Using last-child. */
    #LastChildExample article {
      /* Other CSS rules for this element. */
      margin-bottom: 30px;
    }
    
    #LastChildExample article:last-child {
      margin-bottom: 0;
    }
    <p>The plus selector only selects an element following another. Combining the plus selector and margin-top means there's no extra space at the top since the selector doesn't apply.</p>
    <section id ="PlusSelectorExample">
      <article>In vel sem sed nulla scelerisque semper. Fusce dictum semper lectus, a cursus turpis fermentum vitae.</article>
      <article>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</article>
      <article>Suspendisse potenti. Duis id lacus augue. Duis ultricies est viverra, dapibus est quis, pretium sapien.</article>
    </section>
    <p>Using margin-bottom leaves an extra gap at the bottom.</p>
    <section id="MarginBottomExample">
      <article>In vel sem sed nulla scelerisque semper. Fusce dictum semper lectus, a cursus turpis fermentum vitae.</article>
      <article>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</article>
      <article>Suspendisse potenti. Duis id lacus augue. Duis ultricies est viverra, dapibus est quis, pretium sapien.</article>
    </section>
    <p>Combining margin-bottom with last-child achieves the same as the plus operator.</p>
    <section id="LastChildExample">
      <article>In vel sem sed nulla scelerisque semper. Fusce dictum semper lectus, a cursus turpis fermentum vitae.</article>
      <article>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</article>
      <article>Suspendisse potenti. Duis id lacus augue. Duis ultricies est viverra, dapibus est quis, pretium sapien.</article>
    </section>

    【讨论】:

      【解决方案6】:

      我总是为我认为更多可选的元素设置边距。也就是说,对于更有可能从 DOM 中删除的元素。示例:

      <div class="title">My Awesome Book</div>
      <p>Description of My Awesome Book</p>
      

      在这种情况下,我会将margin-top 改为&lt;p&gt;,因为如果没有标题,描述将没有多大意义,但如果我只想提及标题,描述可能会被删除。

      另一个例子:

      <img src="icon.png">
      <div>My Awesome Book</div>
      

      在这里,我会做相反的事情。我会将margin-bottom 添加到图标中。我认为图标只是一种装饰,而且它更有可能被移除。

      这是我的哲学。样式元素是通过潜在的 DOM 更改来防止 CSS 更改的方法。

      【讨论】:

        【解决方案7】:

        这里投票最多的answer 有点过时了,因为它说使用margin-top 的好处是你可以使用:first-child(但截至2018年,你可以使用margin-bottom:last-child 结合使用同等支持)。

        然而,更喜欢 ma​​rgin-top 和 :first-child 的原因(代替 margin-bottom:last-child)在CSS: margin top vs bottom这篇文章中有详细解释。

        基本上,它表示如果您在其他相似块之间有一个块,并且您希望该块的边距(顶部和底部)与其他块不同,您会遇到更多麻烦,因为没有简单的方法来选择继续元素,但是使用 adjacent sibling selector (+) 可以在不需要帮助类的情况下实现这一点:

        使用 ma​​rgin-top 和 :first-child 结构,为广告提供适当间距所需的 CSS 是:

        article {
            margin-top: 2em;
        }
        
        article:first-child {
            margin-top: 0;
        }
        
        //space at the top of the Ad
        .ad {
            margin-top: 1em;
        }
        
        //and to reduce space below the ad
        .ad + article {
            margin-top: 1em;
        }
        

        【讨论】:

        • 这正是我更喜欢margin-top的原因,我的文章中有图像,并且多个图像行应该更靠近margin-top使这很容易,不知道为什么bootstrap和其他人更喜欢margin -底部..
        • 很有趣,但是如何在响应式中使用这个概念呢?我的意思是:在移动设备上我有堆叠文章(这是你的情况),在桌面上我有相同的文章作为每行 3 篇文章的网格。
        • 很好的答案。您确定在 example giben Ad 之前放置的元素没有选择器吗?
        【解决方案8】:

        我不太明白为什么你们中的一些人说 margin-bottom 比 margin-top 更符合逻辑。

        只是为了确保大家都知道:页面是从上到下呈现的。

        让我们考虑一个简单的问题

        <div class="box">
           <div class="item first"></div>
           <div class="item second"></div>
           <div class="item third"></div>
        </div>
        

        框内的元素之间应该有它们的距离。

        一个非常通用的解决方案

        .item:nth-child(n+2) {
            margin-top: 16px;
        }
        

        或者你可以使用

        .item + .item {
            margin-top: 16px;
        }
        

        如您所见,只需要 1 条规则。 它仅在存在超过 1 个元素时才起作用,如果例如由于某种原因“第一”和“第二”不存在,您不希望从顶部获得额外的空间导致空间从顶部开始由“box”元素填充处理。

        如果由于某种原因一个或多个元素的间距应该与其他元素不同,您只需这样做:

        .item + .item {
            margin-top: 16px;
        }
        
        .item + .item.second {
            margin-top: 32px;
           // you could use margin-bottom here but there will be needed a different rule for handling if there is no 3rd element
        }
        
        .item.second + .item {
            margin-top: 32px;
        }
        

        我在这里发现了 1 个问题,如果您将一个元素注入到不包含“item”类的列表中,但是为什么要这样做呢?

        考虑到 margin-bottom 我试图找到一些解决方案,但我没有看到任何不需要编写多个规则的解决方案。

        .item {
            margin-bottom: 16px;
        }
        
        .item:last-child {
           margin-bottom: 0px;
        }
        

        不要误会,在某些情况下需要底部边距,但我发现很少使用它。

        根据我的经验,margin-bottom 总是像脚上的针。

        我发现一般来说,元素之间和页面之间的关系应该尽可能少,并且它们应该尽可能地可重用。

        Ofc 让您的生活更简单是您决定做或不做的事情。

        维护一条规则而不是多条规则更好,您不同意吗?

        更少的 css 代码 = 更好的性能 -> 更快的下载

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-19
          • 2016-06-23
          • 1970-01-01
          • 1970-01-01
          • 2013-09-22
          • 2010-09-06
          • 2012-07-14
          相关资源
          最近更新 更多