How to make my footer always stay at the bottom of the page, even when the page content is smaller than the screen?

前端 未结 4 1087
再見小時候
再見小時候 2021-01-23 05:31

I haven\'t added content to my page yet, since I\'m still working on the header and footer. I noticed that my footer, instead of being at the bottom of the page, like it would b

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-23 05:42

    You can use flexbox auto margins

    Minimal Example:

    body {
      display: flex;
      flex-flow: column;
      min-height: 100vh; /* have the flex container at least take up the viewport */
      
      margin: 0;
      font-size: 20px;
    }
    
    footer {
      margin-top: auto;
      background: #999999;
    }
    
      
    Header
    Some random content
    Footer


    Using the same minimal example above but with long content:

    body {
      display: flex;
      flex-flow: column;
      min-height: 100vh; /* have the flex container at least take up the viewport */
      
      margin: 0;
      font-size: 20px;
    }
    
    div {
      height: 3000px;
    }
    
    footer {
      margin-top: auto;
      background: #999999;
    }
    
      
    Header
    Some random content
    Footer

    Using the OP's code:

    body {
      font-family: Lato;
      margin: 0px;
      padding: 0px;
      width: 100%;
      display: flex;
      flex-flow: column;
      min-height: 100vh;
    }
    
    header {
      height: 80px;
      padding-left: 20px;
      padding-right: 5px;
      padding-top: 15px;
    }
    
    header h1 {
      display: inline;
    }
    
    header nav {
      float: right;
      font-size: 18pt;
    }
    
    header nav a {
      color: #999;
      line-height: 50px;
      margin-right: 20px;
      text-decoration: none;
    }
    
    header nav a:hover {
      color: #666;
    }
    
    header nav a.currentPage {
      color: #7a7acc;
    }
    
    header nav a.currentPage:hover {
      color: #7a7acc;
    }
    
    footer {
      margin-top: auto;
      background-color: #f2f2f2;
      color: #666;
      font-size: 12pt;
      padding: 20px;
      text-align: center;
    }
    
    footer div {
      max-width: 750px;
      margin: 0px auto;
    }
    
    footer a {
      color: #666;
    }
    
    
    
    
      

    Footer text. Footer text. Footer text.

提交回复
热议问题