html - 内容框应扩展到页脚,即使是空的

标签 html css

即使内容框为空,我也需要内容框到达页脚。我只想使用 CSS 来实现这一点。

  1. padding-bottom 不是一个选项。
  2. 我不想使用背景图片,例如 background-image: url center repeat-y;

我怎样才能做到这一点?

.wrap {
  height: 100%;
}
.l-col {
  padding-top: 0;
  height: 100%;
}
.footer {
  position: relative;
  height: 60px;
  clear: both;
  float: left;
  width: 100%;
  z-index: 3;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="wrap">
  <div class="container">
    <div class="col-xs-12 l-col">
      <div class="col-xs-12" style="padding:0px">
        <table>Content</table>
      </div>
    </div>
  </div>
</div>


当前布局:

enter image description here


所需布局:

enter image description here

最佳答案

使用 CSS calc()函数,您可以使用计算内容 div 的最小高度。

body {
  margin: 0
}

.header,
.footer {
  width: 100%;
  height: 60px;
  background-color: grey;
}

.content {
  width: 100%;
  min-height: calc(100vh - 120px);
  background-color: #FFF8DC;
}
<div class="wrap">
  <div class="container">
    <div class="header">
      Header
    </div>

    <div class="content">
      Content
    </div>

    <div class="footer">
      Footer
    </div>
  </div>
</div>

如果您首选的方法是 jQuery,则即使在调整页面大小时,以下代码也能正常工作。

function setContentHeight() {
  var headerHeight = $(".header").height();
  var footerHeight = $(".footer").height();
  var winHeight = $(window).height();
  $(".content").css("min-height", winHeight-(headerHeight+footerHeight));
}

setContentHeight();
$(window).resize(setContentHeight);

关于html - 内容框应扩展到页脚,即使是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39416442/

相关文章:

html - Bootstrap 4 : customize checkbox border

css - 具有动态元素的 div 将其他 div 向下推

javascript - 通知 HTML5 是否可以在 Chrome 中本地运行?

javascript - React 中进出 CSS 转换的不同延迟

Javascript - 选择网格元素时显示 div

html - 边距顶部 : 100% gets parent width value. .. 奇怪

javascript - 在 ionic2+bootstrap4 中滚动时,粘性页脚随屏幕一起移动

css - 避免边框重叠 CSS

html - 设置两个类之间的距离

javascript - 当鼠标不在 HTML 元素上时,将 Rythm.js 播放的音乐静音