Extend div to bottom of page (only HTML and CSS)

前端 未结 2 1781
情歌与酒
情歌与酒 2020-12-23 17:37

Its a very simple problem but I can\'t seem to find a suitable solution online. I want to have a list of elements in natural flow with the last element extended to the botto

2条回答
  •  囚心锁ツ
    2020-12-23 17:46

    Solution: #1: css tables:

    html, body {
      height: 100%;
      width: 100%;
    }
    body {
      display: table;
      margin: 0;
    }
    #top, #bottom {
      width: 100%;
      background: yellow;
      display: table-row;
    }
    #top {
      height: 50px;
    }
    #bottom {
      background: lightgrey;
      height: 100%;
    }
    

    html, body {
      height: 100%;
      width: 100%;
    }
    body {
      display: table;
      margin: 0;
    }
    #top, #bottom {
      width: 100%;
      background: yellow;
      display: table-row;
    }
    #top {
      height: 50px;
    }
    #bottom {
      background: lightgrey;
      height: 100%;
    }
    A header
    The content area - extends to the bottom of the page

    Codepen #1 (little content)

    Codepen #2 (lots of content)

    Solution #2: Using calc and viewport units

    #top {
      height: 50px;
      background: yellow;
    }
    #bottom {
      background: lightgrey;
      min-height: calc(100vh - 50px);
    }
    

    body {
      margin: 0;
    }
    #top {
      height: 50px;
      background: yellow;
    }
    #bottom {
      background: lightgrey;
      min-height: calc(100vh - 50px);
    }
    
    Where `min-height: calc(100vh - 50px);` means:
    
    'Let the height of the content div be **at least** 100% of the viewport height minus the 50px height of the header.'
    A header
    The content area - extends to the bottom of the page

    Codepen #1 (little content)

    Codepen #2 (lots of content)

    Solution #3 - Flexbox

    body {
      margin: 0;
      min-height: 100vh;
    }
    body {
      display: flex;
      flex-direction: column;
    }
    #top {
      height: 50px;
      background: yellow;
    }
    #bottom {
      background: lightgrey;
      flex: 1;
    }
    

    body {
      margin: 0;
      min-height: 100vh;
    }
    body {
      display: flex;
      flex-direction: column;
    }
    #top {
      height: 50px;
      background: yellow;
    }
    #bottom {
      background: lightgrey;
      flex: 1;
    }
    A header
    The content area - extends to the bottom of the page

    Codepen #1 - little content

    Codepen #2 - lots of content

提交回复
热议问题