以下是实现页面顶部和底部固定、中间内容自适应的几种常见方法,附代码示例和适用场景分析:
<body style="margin:0; min-height:100vh; display:flex; flex-direction:column;">
<header style="height:60px; background:#ccc;">顶部固定</header>
<main style="flex:1; overflow-y:auto;">中间自适应内容</main>
<footer style="height:40px; background:#eee;">底部固定</footer>
</body>
特点:
<body style="margin:0; height:100vh; display:grid; grid-template-rows:60px 1fr 40px;">
<header style="background:#ccc;">顶部固定</header>
<main style="overflow-y:auto;">中间自适应内容</main>
<footer style="background:#eee;">底部固定</footer>
</body>
特点:
<body style="margin:0;">
<header style="position:fixed; top:0; width:100%; height:60px; background:#ccc;">顶部固定</header>
<main style="margin:60px 0 40px; height:calc(100vh - 100px); overflow-y:auto;">
中间自适应内容
</main>
<footer style="position:fixed; bottom:0; width:100%; height:40px; background:#eee;">底部固定</footer>
</body>
特点:
<style>
header { height:10vh; background:#ccc; }
footer { height:8vh; background:#eee; }
main {
height: calc(100vh - 18vh);
overflow-y: auto;
}
</style>
<header>顶部固定</header>
<main>中间自适应内容</main>
<footer>底部固定</footer>
特点:
overflow-y: auto
实现独立滚动100vh
时需考虑移动端浏览器地址栏的显示/隐藏z-index
防止内容覆盖根据项目需求选择方案:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。