0

我在我的应用程序中遇到 CSS 布局问题,并使用jsFiddle重现它。

基本上,它非常简单,如下所示:

html,body {
    height: 100%;
}
.header {
   background: yellow;
   height: 50px;
}
.main {
   background: green;
   height: calc(100% - 50px);
}
<div class="header">Create a div that stretches across the window, with a 50px gap between both sides of the div and the edges of the window:</div>
<div class="main"></div>

标题 div 设置为固定高度50px,我希望主 div 可以占据剩余高度,所以我使用calc(100% - 50px).

但结果对我来说有点奇怪。生成的高度不准确,生成了垂直滚动条。我检查了边距或填充,没问题。

我想要的结果是整个页面分为两部分。并且没有产生滚动条。

4

2 回答 2

6

浏览器默认设置了一些margin(关于),将其重置为.8px0

html,body {
  height: 100%;
}
body {
  margin: 0;
}
.header {
  background: yellow;
  height: 50px;
}
.main {
  background: green;
  height: calc(100% - 50px);
}
<div class="header">Create a div that stretches across the window, with a 50px gap between both sides of the div and the edges of the window:</div>
<div class="main"></div>

于 2017-01-16T02:38:26.733 回答
0

正如@Pangloss 上面所说,浏览器会自动为导致该问题的页面添加边距。防止这种情况和其他不同浏览器怪癖的一个好方法是在您的 CSS 构建过程中使用Normalize.css或链接到<head>.

于 2017-01-16T03:34:30.013 回答