I came here to find a solution to a similar problem, which should have a footer that spans the width of the window and is below (variable height and width) content. In other words, make sure that the footer is βfixedβ relative to its horizontal position, but retains its normal position in the document flow relative to its vertical position. In my case, I had the text of the footer aligned right, so for me, dynamically adjusting the width of the footer. Here is what I came up with:
HTML
<div id="footer-outer"> <div id="footer"> Footer text. </div> </div>
CSS
#footer-outer { width: 100%; } #footer { text-align: right; background-color: blue; }
CoffeeScript / JavaScript
(Using prototype.js).
class Footer document.observe 'dom:loaded', -> Footer.width = $('footer-outer').getDimensions().width Event.observe window, 'scroll', -> x = document.viewport.getScrollOffsets().left $('footer-outer').setStyle( {'width': (Footer.width + x) + "px"} )
which compiles to:
Footer = (function() { function Footer() {} return Footer; })(); document.observe('dom:loaded', function() { return Footer.width = $('footer-outer').getDimensions().width; }); Event.observe(window, 'scroll', function() { var x; x = document.viewport.getScrollOffsets().left; return $('footer-outer').setStyle({ 'width': (Footer.width + x) + "px" }); });
This works great in FireFox, and pretty good in Chrome (this is a little annoying) I have not tried other browsers.
I also wanted any free space below the footer to be a different color, so I added this footer-stretch div:
HTML
... </div> <div id="footer-stretch"></div> </div>
CSS
#footer-outer { height: 100%; } #footer-stretch { height: 100%; background-color: #2A2A2A; }
Please note that for the # footer-stretch div to work, all parent elements prior to the body element (and possibly the html element are not sure) must have a fixed height (in this case, height = 100%).
Waz Jun 27 '12 at 4:16 2012-06-27 04:16
source share