【问题标题】:How to do a sticky footer and still be able to do scrollable flexbox content?如何做一个粘性页脚并且仍然能够做可滚动的 flexbox 内容?
【发布时间】:2018-07-10 03:47:48
【问题描述】:

我正在尝试实现sticky footer(flexbox 版本)。但是,如果我还希望能够在 flex: 1 div 中包含可滚动内容(这要求父母拥有 height: 100%),我无法找到可行的解决方案。

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

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

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

更新

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

【问题讨论】:

    标签: html css flexbox sticky-footer


    【解决方案1】:

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

    是的,你需要的是一直使用 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

    堆栈sn-p

    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

    堆栈sn-p

    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>

    【讨论】:

    • 这真的很接近!唯一的问题是,如果您在 article-1 上有 min-height: 300px 并向下调整视口大小,则页脚不会像粘性页脚那样向下推(即使它是相对的!,太奇怪了!)。它没有向下推,而是保持在与其他 div 进一步向下推相同的位置(页脚实际上变得比其他 div 更高)。 Article-1 需要一个最小高度(大于 0),否则该 div 太小而无法在较小的屏幕上使用。
    • 在我的问题的“更新”部分查看第二张图片。
    • @prograhammer 这里的主要问题是,内部滚动需要一个高度,所以不能在外部元素上设置min-height,比如html/body .最好的选择是在content 上使用min-height 的建议,否则滚动元素需要绝对定位。
    • 是的,设置min-height: 450px(足以满足该可滚动 div 的最小高度需求)有效!你的第二把小提琴很棒。当我缩小视口以强制滚动时,页脚会变粘并向下移动。谢谢。
    • 即使此解决方案有效,但必须根据某些页面将内容的最小高度设置为不同的值是很麻烦的。我最终只在每个页面模板/组件的底部包含页脚组件,以便我可以将它与页面上它上方的其他弹性框一起包含在弹性框中。但是您的其他调整对我和我的理解仍然有很大帮助!
    猜你喜欢
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多