我一直在尝试从表格布局切换到更现代的布局。我的布局需要“固定”页眉和粘滞页脚。为了实现它,我决定通过稍微改变页面的工作方式,而不是让整个页面滚动,而是创建一个滚动div作为正文/页脚区域。这样一来,在模仿典型页面滚动方式的同时,标题就可以保持动态顶部。
但是,我在flexbox和grid中都遇到了相同的问题。只要没有基于百分比的子元素,我就可以使用该布局。如果有,似乎flexbox会根据孩子的大小进行调整。例如,如果将一个子级中的div高度设置为100%,它将显示div,但溢出后其他任何内容都会显示页脚。这对我来说似乎有点违反直觉,我希望有人可以解释其背后的设计选择?是否有任何变通办法允许这种功能?
我基本上认为flexbox系统可以填充剩余的空间(用于身体),但也可以根据需要扩展以包围所有孩子。
我写了一个快速样本来说明我在说什么。见下文,谢谢!
编辑:这似乎仅在Firefox和Edge中发生,而不在Chrome中发生。
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.container {
display: inline-block;
height: 100%;
width: 50%;
max-height: 100%;
overflow: auto;
}
.bg {
background-color: gray;
}
.span {
width: 100%;
height: 100%;
}
.flex {
display: flex;
flex-direction: column;
min-height: 100%;
}
.flex > .main {
flex: 1;
}
<div class="container">
<div class="flex">
<div class="main">
<ul>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
</ul>
</div>
<footer>
This should be a sticky footer
</footer>
</div>
</div><div class="container">
<div class="flex">
<div class="main">
<div class="bg span">
This breaks the page
</div>
<ul>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
<li>This is a spacer</li>
</ul>
</div>
<footer>
This should be a sticky footer
</footer>
</div>
</div>