html - 如何做一个粘性页脚并且仍然能够做可滚动的 flexbox 内容?

标签 html css flexbox sticky-footer

我正在努力实现 sticky footer ( flex 盒版本)。但是,如果我还希望能够在 flex: 1 div 中包含可滚动内容(这要求父级具有 height: 100%).

这是一个演示问题的 fiddle : https://jsfiddle.net/gfaqLh42/6/

enter image description here

如您所见,红色区域是可滚动的(min-height: 300px)。请注意,即使视口(viewport)不小于红色区域的最小高度 + 蓝色区域,页脚也在屏幕外。

有没有办法做一个粘性页脚并仍然使用 flexbox flex: 1 和可滚动的内容

更新

这是另一张图片,代表我在尝试完成这项工作时遇到的另一个大问题:

enter image description here

最佳答案

Is there a way to do a sticky footer and still use flexbox flex: 1 with scrollable content?

是的,你需要的是一直使用 Flexbox。

因此,不要在 article-1/card 上使用 min-height/height,而是更改它们的 CSS对此:

.article-1 {
  flex: 1;
  display: flex;
  min-height: 0;                     /*  added, i.a Firefox need this  */
}

.card {
  overflow: auto;
}

请注意,我还删除了一些不需要的属性,主要是因为它们被设置为默认值,并添加了一些。为什么需要 min-width,这里有很好的解释:

Updated fiddle

堆栈片段

html, body{
  height: 100%;
  margin: 0;
  font-weight: bold;
}

.header {
  position: absolute;
  height: 40px;
  background-color: grey;
  z-index: 1;
  width: 100%;
}

.content {
  display: flex;
  flex-direction: column;
  height: 100%;  
  padding-top: 40px;
  box-sizing: border-box;            /*  added  */
}

.wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;                     /*  added, i.a Firefox need this  */
}

.container {
  flex: 1;
  padding: 10px;
  box-sizing: border-box;            /*  added  */
  display: flex;
  flex-direction: column;
  min-height: 0;                     /*  added, i.a Firefox need this  */
}

.article-1 {
  flex: 1;
  display: flex;
  min-height: 0;                     /*  added, i.a Firefox need this  */
}

.card {
  overflow: auto;
}

.card-text {
  height: 2000px;
  width: 2000px;
  background-color: red;
}

.article-2 {
  flex: none;
  height: 40px;
  background-color: blue;
}

.footer {
  position: relative;
  height: 40px;
  background-color: grey;
}
<div class="header">Header</div>
<div class="content">

  <div class="wrap">
    <div class="container">
      <div class="article-1">
        <div class="card">
          <div class="card-text">
            scrollable flex: 1 div<br>
            1. scrollable<br>
            2. scrollable<br>
            3. scrollable<br>
            4. etc...
          </div>
        </div>
      </div>
      <div class="article-2">
        flex: none div
      </div>
    </div>
  </div>

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


根据评论更新

如果需要 article-1 有一个最小高度,并且为了避免绝对定位,可以在 content 上设置一个最小高度,如下所示好吧,在较小的屏幕上将 footer 向下推。

Updated fiddle 2

堆栈片段

html, body{
  height: 100%;
  margin: 0;
  font-weight: bold;
}

.header {
  position: absolute;
  height: 40px;
  background-color: grey;
  z-index: 1;
  width: 100%;
}

.content {
  display: flex;
  flex-direction: column;
  height: 100%;  
  min-height: 450px;                 /*  added  */
  padding-top: 40px;
  box-sizing: border-box;            /*  added  */
}

.wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;                     /*  i.a Firefox need this  */
}

.container {
  flex: 1;
  padding: 10px;
  box-sizing: border-box;            /*  added  */
  display: flex;
  flex-direction: column;
  min-height: 0;                     /*  i.a Firefox need this  */
}

.article-1 {
  flex: 1;
  display: flex;
  min-height: 300px;                 /*  changed  */
}

.card {
  overflow: auto;
}

.card-text {
  height: 2000px;
  width: 2000px;
  background-color: red;
}

.article-2 {
  flex: none;
  height: 40px;
  background-color: blue;
}

.footer {
  position: relative;
  height: 40px;
  background-color: grey;
}
<div class="header">Header</div>
<div class="content">

  <div class="wrap">
    <div class="container">
      <div class="article-1">
        <div class="card">
          <div class="card-text">
            scrollable flex: 1 div<br>
            1. scrollable<br>
            2. scrollable<br>
            3. scrollable<br>
            4. etc...
          </div>
        </div>
      </div>
      <div class="article-2">
        flex: none div
      </div>
    </div>
  </div>

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

关于html - 如何做一个粘性页脚并且仍然能够做可滚动的 flexbox 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48535306/

相关文章:

javascript - HTML Canvas - 将背景图像传递到 Canvas,同时传递 base64 数据

html - 调整网页大小时的空白

css - 如何更改移动页脚中的间距(行/段落之间)- css?下面的代码来自 responsive.css wordpress

jquery - 如何在 CSS3 中保持元素缩放直到鼠标离开?然后在 mouseleave 上将元素缩放回其原始大小

html - 带有图像的 flexbox,固定大小

html - 使用 flexbox 的响应式两列布局

javascript - 单击行时表的高度发生变化

html - 在 css 中为菜单标题添加阴影

html - 背景中的图像始终位于同一位置?

css - 忽略 nowrap 的 Firefox 文本溢出(Chrome 有效)