html - How do you stretch a div element to the footer?
问题描述
My goal was to get the footer to stay at the bottom of the page and to go further down when more content is added. In doing so, a div element on my page which follows the footer has stopped half way when there isn't enough content.
My question is, how do you get the middle-strip
div to stretch to the footer and have the goal above still achievable.
Here is a simplified JSFiddle to show the issue.
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
#container {
min-height: 100%;
position: relative;
}
#header {
background: #283343;
height: 50px;
}
#middle-strip {
padding-bottom: 100px;
background: #32cd32;
width: 500px;
margin: auto;
}
#content-area {
width: 400px;
height: 100%;
margin: auto;
}
#footer {
background: #283343;
position: absolute;
bottom: 0;
width: 100%;
height: 100px;
}
<div id="container">
<div id="header">
THIS IS THE HEADER
</div>
<div id="middle-strip">
<div id="content-area">
THIS IS WHERE THE CONTENT WILL GO
</div>
</div>
<div id="footer">
THIS IS THE FOOTER
</div>
</div>
解决方案
您可以使用 flexbox 来实现:
#container {
display: flex;
min-height: 100vh;
flex-direction: column;
}
#middle-strip {
flex: 1;
}
https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/
推荐阅读
- html - 如何在 VSTS 中发布 html 报告
- python - 使用 http.server 从 python 脚本显示动态网页
- javascript - 调整窗口大小后如何使图像重新定位?
- swift - 在 Swift 中将可选绑定转换为错误处理的过程是什么?
- android - 将图像从 imageview 上传到 Firebase Android
- c# - 将 webApi 控制器的令牌身份验证添加到现有的 asp.net MVC 5 应用程序
- r - 如何在 R 中使用带有条件的 lapply 以每次仅适合一个元素
- c - 我应该使用块设备而不是 char 设备来读取和写入内存吗?
- html - 为什么不明确:两者都消除了这种重叠?
- html - 居中的 div 和从右到左的图片