为什么包装器div向下移动?

时间:2016-06-20 13:56:12

标签: html css

面对一个非常独特的情况。在以下代码中,所有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 个答案:

答案 0 :(得分:2)

这是margin collapsing

  

如果没有边框,填充,内联内容或许可将块的margin-top与其第一个子块的margin-top分开,或者没有边框,填充,内联内容,{ {1}},heightmin-heightmax-height块与其最后一个孩子的margin-bottom分开,然后这些边距就会崩溃。 折叠的保证金最终在父母之外。

在您的示例中,您将margin-bottom设置为绝对位置,使其超出正常内容流。兄弟.topimage的默认上边距在ul之外折叠。

要解决此问题,您可以重置.wrapper的默认上边距:

ul

或者,将.list { margin-top: 0; } 添加到父容器overflow:auto

div
相关问题