I have the following structure:
<body>
<div id="main-wrapper">
<header>
</header>
<nav>
</nav>
<article>
</article>
<footer>
</footer>
</div>
</body>
I dynamically load content in the <article>
using javascript. Because of this, the height of the <article>
block can change.
I want the <footer>
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 only a few lines of content exist.
At the moment I can do one or the other... but not both.
So does anyone know how I can do this - get the <footer>
to stick to the bottom of the page/content or the bottom of the screen, depending on which is lower.
Here (Make the Footer Stick to the Bottom of a Page) is great post/tutorial to solve your problem...try it.
Ryan Fait's sticky footer is a simple solution that I have used several times in the past.
Basic HTML:
CSS:
Translating this to be similar to what you already have results with something along these lines:
HTML:
CSS:
Just dont forget to update the negative on the wrapper margin to match the height of your footer and push div. Good luck!
Ryan Fait's sticky footer is very nice, however I find its basic structure to be lacking*.
Flexbox Version
If you're fortunate enough that you can use flexbox without needing to support older browsers, sticky footers become trivially easy, and support a dynamically sized footer.
The trick to getting footers to stick to the bottom with flexbox is to have other elements in the same container flex vertically. All it takes is a full-height wrapper element with
CSS:display: flex
and at least one sibling with aflex
value greater than0
:If you can't use flexbox, my base structure of choice is:
Which isn't all that far off from:
The trick to getting the footer to stick is to have the footer anchored to the bottom padding of its containing element. This requires that the height of the footer is static, but I've found that footers are typically of static height.
HTML: CSS:With the footer anchored to
CSS:#main-wrapper
, you now need#main-wrapper
to be at least the height of the page, unless its children are longer. This is done by making#main-wrapper
have amin-height
of100%
. You also have to remember that its parents,html
andbody
need to be as tall as the page as well.Of course, you should be questioning my judgement, as this code is forcing the footer fall off the bottom of the page, even when there's no content. The last trick is to change the box model used by the
CSS:#main-wrapper
so that themin-height
of100%
includes the100px
padding.And there you have it, a sticky footer with your original HTML structure. Just make sure that the
footer
'sheight
is equal to#main-wrapper
'spadding-bottom
, and you should be set.* The reason I find fault with Fait's structure is because it sets the
.footer
and.header
elements on different hierarchical levels while adding an unnecessary.push
element.I think below 2 lines in css should be sufficient to get you what you want.
It worked for me.