【问题标题】:Extra space below <div> element<div> 元素下方的额外空间
【发布时间】:2017-07-13 16:49:43
【问题描述】:

在 id 为“header”元素(第二个 div)的 div 之后有一些额外的空间。如果我删除 p,div 元素之间没有空格。如何在不删除 p 元素的情况下消除两个 div 元素之间的空间以及为什么它会这样?

body {
  margin: 0px;
  padding: 0px;
}

div#page {
  width: 960px;
  margin: 10px auto;
}

div#header {
  width: 960px;
  height: 80px;
  background-color: lightgray;
}

div#main {
  height: 400px;
  width: 960px;
  background-color: antiquewhite;
}
<div id="page">
  <div id="header">header</div>
  <div id="main">
    <p>we make your business</p>
    <p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
    <form action="" method="post">
      <button>about us</button>
    </form>
  </div>
</div>

【问题讨论】:

  • 你需要明确哪个div元素
  • 给出写着“我们做你的生意”的div,margin-top: 0
  • 尝试使用 css 重置。
  • div#header { width: 960px; height: 80px; margin-bottom: -16px; background-color: lightgray; }

标签: html css margin


【解决方案1】:

因为margin collapsing:

父母和第一个/最后一个孩子
如果没有边框,填充,内联内容,block_formatting_context创建或clearance将块的margin-top与其第一个子块的margin-top分开,或者没有边框,填充,内联内容,@987654326 @、min-heightmax-height 将块的 margin-bottom 与其最后一个子块的 margin-bottom 分开,然后这些边距折叠。折叠的边距最终在父级之外。

您可以从第一个 &lt;p&gt; 元素中删除 margin-top,然后将 padding-top 添加到 div#main

body {
  margin: 0px;
  padding: 0px;
}

div#page {
  width: 960px;
  margin: 10px auto;
}

div#header {
  width: 960px;
  height: 80px;
  background-color: lightgray;
}

div#main {
  height: 400px;
  width: 960px;
  background-color: antiquewhite;
  padding-top: 15px;
}

div#main p:first-child {
  margin-top: 0;
}
<div id="page">
  <div id="header">header</div>
  <div id="main">
    <p>we make your business</p>
    <p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
    <form action="" method="post">
      <button>about us</button>
    </form>
  </div>
</div>

【讨论】:

    【解决方案2】:

    如果你删除这两个 div 之间的空格,然后添加这个 CSS:-

    #main p:first-child {
        margin: 0px;
    }
    

    【讨论】:

      【解决方案3】:

      只使用 * 代替 body 元素

      *{
          margin:0px;
          padding:0px;
      }
      div#page{
          width:960px;
          margin:10px auto;
      }
      div#header{
          width:960px;
          height:80px;
          background-color:lightgray;
      }
      div#main{
          height:400px;
          width:960px;
          background-color:antiquewhite;
      }
      <div id="page">
              <div id="header">
                 header
              </div>
              <div id="main">
                  <p>we make your business</p>
                  <p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
                  <form action="" method="post">
                      <button>
                          about us
                      </button>
                  </form>
              </div>
      </div>

      【讨论】:

        【解决方案4】:

        请试试这个

        <!-- language: lang-css -->
        body{
            margin:0px;
            padding:0px;
        }
        div#page{
            width:960px;
            margin:10px auto;
        }
        div#header{
            width:960px;
            height:80px;
            background-color:lightgray;
        }
        div#main{
            height:400px;
            width:960px;
            background-color:antiquewhite;
        }
        p.demo{
         margin:0px;
        }
        
        
        
        <!-- language: lang-html -->
        
            <div id="page">
              <div id="header">
                header
              </div>
              <div id="main">
               <p class="demo">we make your business</p>
               <p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
              <form action="" method="post">
                  <button>about us
                            </button>
                </form>
              </div>
            </div>
        

        【讨论】:

          【解决方案5】:

          快速解决

          padding-top:1px; 添加到div#main

          智能解决方案

          使用 flex,好处:响应迅速、代码更少、更易读、更现代

          /* the main content will take all remaining space, makin it responsive */
          
          html,
          body {
            height: 100%;
          }
          
          body {
            margin: 0px;
            padding: 0px;
          }
          
          div#page {
            /* make page fill all available space*/
            height: 100%;
            /* change predefined value to 100%, and adjust spaces */
            width: 100%;
            padding: 0 10px;
            margin: 10px auto;
            /* flex usage itself */
            display: flex;
            /* place divs horizontally */
            flex-direction: column;
          }
          
          div#header {
            height: 80px;
            /* header will not resize when window resized*/
            flex-shrink: 0;
            background-color: lightgray;
          }
          
          div#main {
            /* responsive container, remove width/height, any predefined values */
            background-color: antiquewhite;
            flex: 1;
          }
          <div id="page">
            <div id="header">
              header
            </div>
            <div id="main">
              <p>we make your business</p>
              <p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
              <form action="" method="post">
                <button>about us</button>
              </form>
            </div>
          </div>

          【讨论】:

          • 这种情况下是否灵活?我不这么认为。只有在需要它的功能时才应该使用 flex,而不是针对所有情况而不是块级元素。
          • @SergeyDenisov 不同意,我自己一直在使用 flex,它有超级有用的功能,为什么要打扰块嗯? Flex 将用 p 解决小的 OP 问题,但也将有助于防止未来的重构,就像用 bazuka 杀死 2 只兔子,有点 给一个人一条鱼,你喂他一天;教一个人钓鱼,你就可以养活他一辈子,因为我很酷,哈哈:p无论如何,今天我的生日很有趣
          【解决方案6】:

          它发生的原因是p 标签有一个自动边距。更多信息here

          【讨论】:

          • @rajini-raja 这并不完全正确,大多数浏览器默认添加边距,而不仅仅是 Webkit 浏览器。在你的情况下这不是问题,问题是margin collapsing。查看my answer
          猜你喜欢
          • 2014-08-28
          • 2013-12-18
          • 1970-01-01
          • 2013-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-13
          相关资源
          最近更新 更多