CSS to make HTML page footer stay at bottom of the page with a minimum height, but not overlap the page

前端 未结 29 2487
悲哀的现实
悲哀的现实 2020-11-21 23:48

I have the following page (deadlink: http://www.workingstorage.com/Sample.htm ) that has a footer which I can\'t make sit at the bottom of the page.

I wa

相关标签:
29条回答
  • 2020-11-22 00:24

    A very simple flex solution that does not assume fixed heights or changing position of elements.

    HTML

    <div class="container">
      <header></header>
      <main></main>
      <footer></footer>
    </div>
    

    CSS

    .container {
      display: flex;
      flex-direction: column;
      min-height: 100vh;
    }
    
    main {
      flex: 1;
    }
    

    Browser Support

    All major browsers, except IE11 and below.

    Make sure to use Autoprefixer for appropriate prefixes.

    .container {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -ms-flex-direction: column;
      flex-direction: column;
      min-height: 100vh;
    }
    
    main {
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
    }
    
    /////////////////////////////////////////////
    
    body,
    html {
        margin: 0;
        padding: 0;
    }
    
    header,
    main,
    footer {
      margin: 0;
      display: block;
    }
    
    header,
    footer {
      min-height: 80px; 
    }
    
    header {
      background-color: #ccc;
    }
    
    main {
      background-color: #f4f4f4;
    }
    
    footer {
      background-color: orange;
    }
    <div class="container">
      <header></header>
      <main></main>
      <footer></footer>
    </div>

    0 讨论(0)
  • 2020-11-22 00:24

    I found a better solution from freecodecamp.

    #footer {
       position: absolute;
       bottom: 0;
       width: 100%;
    }
    
    0 讨论(0)
  • 2020-11-22 00:25

    My jquery method, this one puts the footer at the bottom of the page if the page content is less than the window height, or just puts the footer after the content otherwise:

    Also, keeping the code in it's own enclosure before other code will reduce the time it takes to reposition the footer.

    (function() {
        $('.footer').css('position', $(document).height() > $(window).height() ? "inherit" : "fixed");
    })();
    
    0 讨论(0)
  • 2020-11-22 00:26

    This is how i solved the same issue

    html {
      height: 100%;
      box-sizing: border-box;
    }
    
    *,
    *:before,
    *:after {
      box-sizing: inherit;
    }
    
    body {
      position: relative;
      margin: 0;
      padding-bottom: 6rem;
      min-height: 100%;
      font-family: "Helvetica Neue", Arial, sans-serif;
    }
    
    .demo {
      margin: 0 auto;
      padding-top: 64px;
      max-width: 640px;
      width: 94%;
    }
    
    .footer {
      position: absolute;
      right: 0;
      bottom: 0;
      left: 0;
      padding: 1rem;
      background-color: #efefef;
      text-align: center;
    }
    <div class="demo">
    
      <h1>CSS “Always on the bottom” Footer</h1>
    
      <p>I often find myself designing a website where the footer must rest at the bottom of the page, even if the content above it is too short to push it to the bottom of the viewport naturally.</p>
    
      <p>However, if the content is taller than the user’s viewport, then the footer should disappear from view as it would normally, resting at the bottom of the page (not fixed to the viewport).</p>
    
      <p>If you know the height of the footer, then you should set it explicitly, and set the bottom padding of the footer’s parent element to be the same value (or larger if you want some spacing).</p>
    
      <p>This is to prevent the footer from overlapping the content above it, since it is being removed from the document flow with <code>position: absolute; </code>.</p>
    </div>
    
    <div class="footer">This footer will always be positioned at the bottom of the page, but <strong>not fixed</strong>.</div>

    0 讨论(0)
  • 2020-11-22 00:26

    Dynamic one liner using jQuery

    All CSS methods I have come across are too rigid. Also, setting the footer to fixed is not an option if that's not part of the design.


    Tested on:

    • Chrome: 60
    • FF: 54
    • IE: 11

    Assuming this layout:

    <html>
    
    <body>
      <div id="content"></div>
      <div id="footer"></div>
    </body>
    
    </html>
    

    Use the following jQuery function:

    $('#content').css("min-height", $(window).height() - $("#footer").height() + "px");
    

    What that does is set the min-height for #content to the window height - the height of the footer what ever that might be at the time.

    Since we used min-height, if #content height exceeds the window height, the function degrades gracefully and does not any effect anything since it's not needed.

    See it in action:

    $("#fix").click(function() {
      $('#content').css("min-height", $(window).height() - $("#footer").height() + "px");
    });
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html {
      background: #111;
    }
    
    body {
      text-align: center;
      background: #444
    }
    
    #content {
      background: #999;
    }
    
    #footer {
      background: #777;
      width: 100%;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <html>
    
    <body>
      <div id="content">
        <p>Very short content</p>
        <button id="fix">Fix it!</button>
      </div>
      <div id="footer">Mr. Footer</div>
    </body>
    
    </html>

    Same snippet on JsFiddle


    Bonus:

    We can take this further and make this function adapt to dynamic viewer height resizing like so:

    $(window).resize(function() {
        $('#content').css("min-height", $(window).height() - $("#footer").height() + "px");
      }).resize();
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html {
      background: #111;
    }
    
    body {
      text-align: center;
      background: #444
    }
    
    #content {
      background: #999;
    }
    
    #footer {
      background: #777;
      width: 100%;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <html>
    
    <body>
      <div id="content">
        <p>Very short content</p>
      </div>
      <div id="footer">Mr. Footer</div>
    </body>
    
    </html>

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