【问题标题】:CSS Layout - White space when NOT using any paddingCSS 布局 - 不使用任何填充时的空白
【发布时间】:2016-06-29 16:47:02
【问题描述】:

首先,我问这个问题是为了学习和更好地理解 CSS。其次,我正在使用这个规范化版本:

https://necolas.github.io/normalize.css/

我的问题是这样的。当我在标题元素上将填充设置为 0 时,我的布局顶部会出现一个白色边框:

Not Working JSFiddle

但是,当我在标题上设置任何填充量时,它确实有效:

Working JSFiddle

谁能告诉我这是为什么?

html,
body {
  height: calc(100% - 100px);
}
body {
  font-family: verdana;
}
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.container {
  position: relative;
  height: 100%;
}
header {
  width: 100%;
  height: 125px;
  padding: 0;
  background-color: red;
}
nav {
  width: 100%;
  height: 50px;
  padding-left: 10px;
  vertical-align: middle;
  line-height: 50px;
  background-color: yellow;
}
aside {
  float: left;
  width: 200px;
  height: calc(100% - 25px);
  padding: 10px;
  background-color: blue;
}
section {
  float: right;
  padding: 10px;
  height: calc(100% - 25px);
  width: calc(100% - 200px);
  background-color: green;
}
footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  padding-left: 10px;
  vertical-align: middle;
  line-height: 50px;
  background-color: orange;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css" rel="stylesheet"/>
<div class="container">
  <header>
    <h1>Header</h1>
  </header>
  <nav>Menu</nav>
  <aside>Aside</aside>
  <section>Content</section>
  <footer>Footer</footer>
</div>

【问题讨论】:

  • 空白是因为h1的边距。您确实应该尝试在此处重新创建您的问题,因为如果您的网站已关闭或该问题不再存在,则此问题对将来的人们将毫无用处。
  • 我知道,我会想办法使用 jsfiddle 或其他方法来实现。谢谢顺便说一句! :)
  • 添加了 jsfiddle 链接而不使用规范化,因此可以重现问题。
  • 如果你在全页模式下打开sn-p,你现在就可以看到问题了。

标签: css liquid-layout


【解决方案1】:

这是因为您的标题中有一个 h1,其边距不会折叠并影响其父级的边距。

要解决这个问题,您可以将overflow: auto; 添加到您的父元素中,在这种情况下是在您的&lt;header&gt; 标记中。


更多信息:

CSS margin-top of affects parent's margin

【讨论】:

  • 谢谢你的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多