html - 如何将 div 元素拉伸(stretch)到页脚?

标签 html css

我的目标是让页脚停留在页面底部,并在添加更多内容时进一步向下。这样做时,当没有足够的内容时,我的页面上页脚后面的 div 元素会停止到一半。

我的问题是,如何让 middle-stripdiv 延伸到页脚并仍然可以实现上述目标。

这是一个简化的 JSFiddle显示问题。

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 - 如何将 div 元素拉伸(stretch)到页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51213393/

相关文章:

html - 如何自动将多张图片隔开以均匀地适应屏幕的宽度 ' html

javascript - 从文本区域删除选定的文本

CSS 星级 - 选择悬停的星星之前的所有星星

html - CSS 文件仅部分适用于 Google App Engine

html - HTML 表格内的输入文本溢出

javascript - Href 导航到 Div 标签

javascript - JavaScript 中的 HTML "onclick"事件(在表中)

html - bootstrap 3 中的表单对齐

CSS - 使元素从上一行的中间开始

jquery - 事件类菜单 HTML-CSS-JS 不工作