Reg Sime 06 Nov 2008
One of our graphic's guys has handed me a bunch of new website designs in Photoshop. Where do I begin the process of building said site? Well, I can tell you I don't begin here. While my intention is to detail a complete website build in a series of articles, I've chosen to begin here (near the end) because this is a often unresolved issue (eg. www.facebook.com and www.blogger.com) that I can't find much documentation on.
Right, you've laid out your site and it's all looking hunky-dory. Thanks to James' tutorial (a.k.a. the Boy in Baby Blue) you've even got you footer sticking to the bottom of the window like a lead thing. Every browser/platform combo is playing along nicely, but then… hold the phone, what's this? You've just minimised your browser window to 800x600, then slid the horizontal scrollbar across to reveal the overflowing content, and you see your background disappear!

This often occurs when you have a background you want to stretch across the entire width of the screen despite your content being of a fixed width design. For this reason it's most often a header or footer element suffering. Even though you may have tried defining your element as 100% width, it won't help, because that's 100% of the window's width and you want to scroll beyond that to what's outside the window.
This is a little unsightly, and even though the audience who may experience this is small, it can conceivably create accessibility issues‚ think white text floating beyond what was previously a dark background.
OK, don't panic. Put the phone down Russel. A fix is relatively simple to implement. In this example I'll step through fixing the footer in a wee site we built called www.kiwibank.co.nz.
Step 1: whack a div just inside the problematic element (in this case it was the footer) and give it a funky name like 'narrow_display_fix'
<div id='footer'>
<div id='narrow_display_fix'>
content goes here…
</div>
</div>
Step 2: a little CSS to boot
div#narrow_display_fix {
width: 926px;
background-color:#eaeaea;
}
This div has a defined width which matches the width of your content. As soon as a window is narrowed to this width the horizontal scrollbar will appear. It also contains the background colour information required to maintain consistency. You can't scroll beyond it because there's no content beyond it. And when the window is re-widened the outside footer div expands to fill any spaces.
Kiwibank's footer had two elements that were suffering the same issue. Meet #narrow_display_fix and #narrow_display_fix2

Tip: To help visualise things as you do this try changing the background colour to pink or red.
The finished footer

I also had to do a little math to maintain the original padding/margin proportions, and there's always a chance (depending on how you've positioned the elements inside your element) that you'll require some IE fixes… but that's another article.
9 comments submit a comment
James Player
Sorry Reg I decided I had to fix this site with your code.
One challenge was getting the 1px thick border to behave on the footer. I had to give .narrow_display_fix the same borders as #footer, but since .narrow_display_fix is inside #footer the borders were doubling up.
The answer was to give .narrow_display_fix a top and bottom margin of -1px.
BTW, 'Boy in baby blue'? What's that all about!?
John
Tnx, saved my day ... :)
Josh
I've had this issue also, and fixed it by putting a min-width on the offending div (in your case, the footer). No extra divs needed.
Reg Sime
True Josh.
If you prefer to use min-width you'll need an IE6 specific stylesheet with an expression along the lines of:
#footer {width: expression(this.width < 925 ? 926 : true);}
Johan
This solution leads to a problem when you want the footer-text always to be centered in the middle of the window and the window is greater then the fixed footer width.
With a footer width of 925px and a window width of 1280px, the footer text will not be in the center of this window.
Why is it to much to ask to have a footer-background that is always 100% of the window width, and footer-text that is always in the middle of that window ?
Does anyone has a solution for this ?
Johan
PS : Problem described above occurs in Firefox 3.5 (in IE the solution of Reg works great)
Johan
Eventually it worked for me both in IE6 and FF3.
Sample code CSS:
div#narrow_display_fix {
width: 926px; }
div#footer {
width: 100%;
min-width: 926px; /*FireFox*/
align: center;
text-align: center; }
html {
background-color: #FFF } /*also necessary in my design*/
Thanks Reg.
Mario
Thanks! I always wondered on how to fix this issue.
You need to apply it to your green line on the top of the page
Dan
Sweet, thanks! you just saved me a tonne of time tracing down the root cause of the disappearing background issue.
Cheers,
Dan