【发布时间】:2016-02-23 04:57:31
【问题描述】:
在我做的代码示例中:
- 将所有边距/填充重置为 0
- 将身高设置为 100%
- 将弹性容器高度设置为 96%
- 将 flex 容器上边距设置为 2%
现在,即使 flex 容器高度 + 边距顶部的总和仅达到 98%,这也给了我在正文上的滚动,所以我的问题是,我不能这样使用边距顶部吗?额外的在哪里空间来自于强制身体滚动?
将 body 设置为 overflow:hidden 会移除滚动条,但这感觉更像是一种创可贴,而不是一种解决方案(除非这是一种“行为设计”,在这种情况下需要这样做)。
编辑
删除 flex 容器上的 margin-top 然后在主体上设置 padding-top: 2%; 或在容器上使用 position: relative; top: 2%; 或使用 absolute: position; 我可以让它按预期工作,但这里的情况这就是margin-top: 2% 不这样做的原因。
* {
box-sizing: border-box;
padding: 0;
margin: 0
}
html, body {
background-color: gray;
height: 100%;
}
.outer {
display: flex;
flex-flow: column;
margin: 0 auto;
height: 96%;
width: 50%;
margin-top: 2%;
}
.top {
background-color: lightgray;
}
.middle {
background-color: darkgray;
}
.the-rest {
flex: 1 0 auto;
background-color: lightgray;
}
<div class="outer">
<div class="top">
top
</div>
<div class="middle">
middle
</div>
<div class="the-rest">
content
</div>
</div>
【问题讨论】:
-
仅供参考,
flex标签指的是 Apache Flex。对于 CSS flex 属性,请改用标签flexbox。 -
投反对票,如果有什么我可以澄清而不是投反对票,请发表评论。
-
可能是
collapsing-margins见stackoverflow.com/a/33597266/2813224 和css-tricks.com/what-you-should-know-about-collapsing-margins