The footer is at the bottom of the page or the bottom of the content (whichever is lower)
P粉716228245
P粉716228245 2024-01-21 15:32:38
0
2
489

I have the following structure:

<body>
    <div id="main-wrapper">
        <header>
        </header>
        <nav>
        </nav>
        <article>
        </article>
        <footer>
        </footer>
    </div>
</body>

I use javascript to dynamically load the content in

. Therefore, the height of the
block may change.


I would like the

block to be at the bottom of the page when there is a lot of content, or at the bottom of the browser window when there are only a few lines of content.


Currently I can choose one or the other...but not both.

Does anyone know how I can do this - have

paste to the bottom of the page/content or the bottom of the screen, whichever is lower.


P粉716228245
P粉716228245

reply all(2)
P粉344355715

Ryan Fait’s Sticky Footer is a simple solution that I’ve used many times in the past.

Basic HTML:

<div class="wrapper">
    <div class="header">
        <h1>CSS Sticky Footer</h1>
    </div>
    <div class="content"></div>
    <div class="push"></div>
</div>
<div class="footer"></div>

CSS

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
    height: 142px; /* .push must be the same height as .footer */
}

/*

Sticky Footer by Ryan Fait
http://ryanfait.com/

*/

Translate this to something similar to what you already got, roughly like this:

HTML

<body>
    <div class="wrapper">
        <header>
        </header>
        <nav>
        </nav>
        <article>
        </article>
        <div class="push"></div>
    </div>
    <footer>
    </footer>
</body>

CSS:

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */
}
footer, .push {
    height: 142px; /* .push must be the same height as .footer */
}

Just don't forget to update the negative numbers on the wrapper margins to match the height of the footer and push div. Good luck!

P粉970736384

Ryan Fait's Sticky Footer is very good, but I found it lacked basic structure*.


Flexbox version

If you're lucky enough to use Flexbox without having to support older browsers, sticky footers are really easy, and support for dynamically resizing footers.

The trick to making the footer stick to the bottom using Flexbox is to make other elements in the same container bend vertically. All it requires is a full-height wrapper element with display: flex and at least one sibling with a flex value greater than 0: p> CSS:子>

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#main-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

article {
  flex: 1;
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#main-wrapper {
  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: 100%;
}
article {
  -webkit-box-flex: 1;
      -ms-flex: 1;
          flex: 1;
}
header {
  background-color: #F00;
}
nav {
  background-color: #FF0;
}
article {
  background-color: #0F0;
}
footer {
  background-color: #00F;
}
<div id="main-wrapper">
   <header>
     here be header
   </header>
   <nav>
   </nav>
   <article>
     here be content
   </article>
   <footer>
     here be footer
   </footer>
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template