【发布时间】:2019-05-16 21:50:02
【问题描述】:
我想创建一个容器元素,它至少是页面的高度。我是这样设置的:min-height: 100vh
正文没有边距。
由于某种原因,元素下方出现了一个空白区域。怎么可能消除呢?
我可以在 Chrome 和 Edge 中观察到此错误,但在 Firefox 中却没有。
我的代码:
body {
margin: 0;
background: red;
}
.container {
margin: auto;
max-width: 200px;
background: gray;
min-height: 100vh;
box-sizing: border-box;
}
.item {
background: white;
margin-bottom: 1em;
height: 100px;
}
<div class="container">
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
</div>
看起来是这样的:
【问题讨论】:
-
在容器上添加一些内边距 (1px) ,由于某些原因,项目上的边距底部跳到容器外jsfiddle.net/z8tg1746 或删除最后一个项目上的边距jsfiddle.net/ahm9yqoL
-
你看到的原因是因为高度 100%,100% 并不总是按预期工作,你必须使用 JavaScript 作为你的高度,或者组合显示类型为高度弯曲
-
关于跳出的边距:developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/… 父母和第一个/最后一个孩子
If there is no border, padding, inline part, 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. -
@G-Cyr 又一个奇怪的边距崩溃案例:stackoverflow.com/q/48777787/8620333
-
重复的问题比这个更通用。答案解释了为什么 FF 和 Chrome 的行为不同,您还将找到规范的相关部分来描述所有这些