New Gist: How to use Dojo to keep your footer at the bottom.
It’s not easy, at least as far as I can tell, to have a footer on a page which does all of the following:
- Sticks to the bottom of the window when the window is larger than the body contents,
- attaches to the bottom of the body contents when the window is shorter, and
- will move to the bottom of floated body contents (acknowledge their height when figuring out where it should go).
- Create a temporary
<div>with a style attribute ofclear: both;and append it to the<body>. - Get the body’s dimensions.
- Delete the
<div>we just created. - If the window’s height is greater than the body’s calculated height with the temporary
<div>, then set the footer’spositionstyle toabsoluteand move it to the bottom. (bottom: 0) - If that’s not the case (the window is shorter than the body): make
position: relativeandclear: both.
<div> is because without it - the body’s “height” would only be that of the non-floated content’s height. For example: imagine you had a menu that contained 500 pixels worth of vertical content which was floated to the left of the main page content, but your main page content only took up 200 pixels of vertical space; the <body>’s height would be 200. The temporary element with clear: both forces the body’s height to become the full height of the content.
I’m not a fan of having to do this with JavaScript, so please tell me a better way if you know one - especially if it’s pure css, in the comments!
FYI, you can have a safe fallback if your users don’t have JavaScript enabled: just manually set the position and bottom styles to absolute and 0 in a CSS file.
