How to do a sticky footer and still be able to do scrollable flexbox content?

后端 未结 1 374
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 04:56

I\'m trying to achieve the sticky footer (flexbox version). However, I\'m unable to find a working solution if I also want the ability to have scrollable content inside a

1条回答
  •  隐瞒了意图╮
    2020-12-22 05:33

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

    Yes, and what you need is to use Flexbox all the way.

    So instead of using min-height/height on article-1/card, change their CSS to this:

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

    Note, I also remove some properties not needed, mainly as they were set to their defaults, and added some. And why the need of min-width, is well explained here:

    • Why don't flex items shrink past content size?

    Updated fiddle

    Stack snippet

    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;
    }
    Header
    scrollable flex: 1 div
    1. scrollable
    2. scrollable
    3. scrollable
    4. etc...
    flex: none div


    Updated based on a comment

    If there is a need for the article-1 to have a minimum height, and to avoid absolute positioning on it, a minimum height could be set on content as well, to push the footer further down on smaller screens.

    Updated fiddle 2

    Stack snippet

    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;
    }
    Header
    scrollable flex: 1 div
    1. scrollable
    2. scrollable
    3. scrollable
    4. etc...
    flex: none div

    0 讨论(0)
提交回复
热议问题