为什么包裹元素div会向下移动?

3
面对一种非常独特的情况。在下面的代码中,所有的div都嵌套在一个大的父.wrapper div中。在.wrapper属性中,我已经明确定义了定位(顶部/左侧)为0px0px的边距。但它仍然会向下移动一点,导致整个页面内容向下移动,因此留下了一个空白的空间在顶部。
网站的背景颜色是黑色的。您可以看到顶部图像容器div(灰色)以及其他div稍微向下移动。

*{
  box-sizing:border-box;
}

html,body {
  margin: 0px;
  height: 100%;
  width: 100%;
  left: 0px;
  top: 0px;
  background-color: rgba(0,0,0,1);
  padding: 0px;
}

a {
  text-decoration: none;
  color: #FFF;
}

.wrapper {
  height: 725px;
  max-width: 960px;
  margin-left: auto;
  left: 0px;
  top: 0px;
  background-color: rgba(255,0,0,1);
  margin-right: auto;
  position: relative;
  padding: 0px;
}

.topimage {
  width: 100%;
  max-width: 960px;
  height: 100%;
  max-height: 175px;
  background-color: #666;
  position: absolute;
  top: 0px;
  font-size: 36px;
  color: #FFF;
  text-align: center;
  border: thin solid #FFF;
}

.topimage img {
  max-width: 100%;
  max-height: 100%;
  display: block;
  margin-right: auto;
  margin-left: auto;
  border-radius: 15px 15px 0px 0px;
}

.menu {
  background: linear-gradient(#0F1A9B, black 100%);
  height: 100%;
  max-height: 50px;
  max-width: 960px;
  position: relative;
  top: 175px;
}

.list {
  color: rgba(255,255,255,1);
  text-decoration: none;
  margin-right: auto;
  margin-left: auto;
  /* [disabled]background-color: rgba(255,0,0,1); */
  padding: 0px;
  width: auto;
  /* [disabled]position: relative; */
  height: 100%;
  font-family: "MS Serif", "New York", serif;
  font-weight: normal;
  font-size: 18px;
}

.list li {
  display: inline-block;
  margin-right: 0%;
  margin-left: 2.5%;
  width: 10%;
  text-align: center;
  /* [disabled]background-color: rgba(0,51,255,1); */
  position: relative;
  padding: 0px;
  /* [disabled]float: left; */
  line-height: 50px;
  vertical-align: top;
}

.content {
  width: 100%;
  height: 500px;
  background-color: #0F1A9B;
  position: relative;
  top: 175px;
  padding-right: 0.5%;
  padding-left: 0.5%;
}

.leftcontent {
  background-color: rgba(210,238,252,1);
  float: left;
  height: 100%;
  max-height: 500px;
  width: 100%;
  max-width: 85%;
  top: 0px;
  position: relative;
  border-left-color: rgba(205,205,205,1);
  padding-right: 0.5%;
  padding-left: 0.5%;
}

.rightcontent {
  background-color: rgba(51,177,236,1);
  float: right;
  height: 100%;
  max-height: 500px;
  width: 100%;
  max-width: 15%;
  position: relative;
  top: 0px;
  /* [disabled]margin-right: 0.3%; */
}

.footer {
 
}
<div class="wrapper">
  <div class="topimage">
    Top Image
  </div>
  <div class="menu">
    <ul class="list">
      <li><a href="home.html">Home</a></li>
      <li><a href="products.html">Products</a></li>
      <li><a href="services.html">Services</a></li>
      <li><a href="about.html">About Us</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
  </div>
  <div class="content">
    <div class="leftcontent">
      <h1>Home page</h1>
    </div>
    <div class="rightcontent">
    </div>
  </div>
</div>


1
我不清楚正在移动哪个部分。我也看不到任何红色。 - j08691
1
同样在这里 - 没有红色,没有移动。 - Johannes
我已经编辑了代码。网站的背景颜色是黑色。您可以看到顶部图像容器div(灰色)以及其他div向下轻微移动。然而,下面的解决方案解决了这个问题。但我不确定列表的边距如何导致整个包装器div向下移动。 - pure_coder
1个回答

2
这是关于外边距合并的内容:
如果一个块元素的`margin-top`与它的第一个子元素的`margin-top`之间没有边框、内边距、行内内容或间隙来分隔,或者一个块元素的`margin-bottom`与它的最后一个子元素的`margin-bottom`之间没有边框、内边距、行内内容、高度、最小高度或最大高度来分隔,则这些外边距会发生合并。合并后的外边距出现在父元素外面。
在你的示例中,你将`.topimage`设置为绝对定位,使其不再正常的内容流中。同级的`ul`标签的默认上外边距会合并到`wrapper`之外。
要解决这个问题,你可以重置`ul`标签的默认上外边距。
.list {
  margin-top: 0;
}

或者,在父容器
中添加overflow:auto
.wrapper {
  overflow: auto;
}

1
是的,那就是它。你能解释一下为什么会发生这种情况吗?以及列表的顶部边距以何种方式导致整个包装器 div 向下移动? - pure_coder
@pure_coder,更新后的答案是否解释清楚了?我还编辑了你的问题,以便未来的访问者更清楚明白。 - Stickers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接