【发布时间】:2020-08-14 05:01:32
【问题描述】:
我正在尝试为网页实现特定的flex-direction: column 布局;我可以在所有桌面浏览器上使用它,但它在 Mobile Safari 中看起来不正确。
这是一个示例布局,其中包含必要的 HTML/CSS 来演示我正在寻找的内容:
<!DOCTYPE html>
<html>
<head>
<style>
html,
body {
height: 100vh;
margin: 0;
padding: 0;
}
p {
margin: 0;
padding: 0;
}
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
.header {
background: green;
height: 150px;
}
.content {
background: gray;
flex: 1 0 auto;
min-height: 1px; /* IE11 fix */
}
.contentContainer {
background: blue;
display: flex;
flex-direction: column;
height: 100%;
margin: auto;
width: 600px;
}
.contentHeader {
background: red;
height: 75px;
}
.contentBody {
background: yellow;
flex: 1 0 auto;
height: 0;
overflow-y: auto;
}
.footer {
background: purple;
height: 150px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
</div>
<div class="content">
<div class="contentContainer">
<div class="contentHeader">
</div>
<div class="contentBody">
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
<p>Lots of content here.</p>
</div>
</div>
</div>
<div class="footer">
</div>
</div>
</body>
</html>
如果您将其加载到任何浏览器(即 Chrome、Firefox、Edge、IE11 等)中,它会产生预期的效果,即在黄色部分有一个垂直滚动条,并带有“这里有很多内容”。 .
如果您将页面加载到 Mobile Safari 中,则黄色区域不可见/不存在。我怀疑这是因为为contentBody div 设置了height: 0 定义。问题是,如果我删除 height: 0 定义,那么内容会将整个页面拉长,这也是我不想要的。
我希望始终显示页眉/页脚和内容页眉,并让黄色内容主体区域填充页面上的任何剩余高度,并在内容主体内容长到需要一个时添加垂直滚动条.
如何使用 Flexbox 实现这种效果并使其在所有桌面和移动浏览器中都能正常工作?谢谢。
编辑#1:请注意,即使我在示例中为页眉、页脚等设置了固定高度,但在我的真实页面上,并没有设置高度。一切都是动态的,所以使用calc(100% - 300px) 之类的东西是行不通的。谢谢。
【问题讨论】:
标签: html css flexbox mobile-safari