<div> 元素下方的额外 space

Extra space below <div> element

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

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 之间的 space 然后添加这个 CSS:-

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

发生这种情况的原因是 p 标签具有自动边距。更多信息 here

仅使用 * 代替正文元素

*{
    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>

因为margin collapsing:

Parent and first/last child
If there is no border, padding, inline content, block_formatting_context created or clearance to separate the margin-top of a block from the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block from the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.

您可以从第一个 <p> 元素中删除 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>

请试试这个

<!-- 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>

快速解决

add padding-top:1px; to div#main

智能解决方案

use flex, benefits: responsive, less code, more readable, modern

/* 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>