Love the web

Love the web is a design, programming, and usability blog | switch to light on dark

Backgrounds Disappear on Horizontal Scroll 9 comments

Reg Sime 06 Nov 2008

Hors d'œuvre

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.

The Main

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!

Before

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

During

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

Dessert

The finished footer

After

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.

Subscribe to these comments

9 comments submit a comment

28 Jan 2009

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!?

11 Feb 2009

John

Tnx, saved my day ... :)

05 Mar 2009

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.

27 Mar 2009

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);}

15 Oct 2009

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 ?

15 Oct 2009

Johan

PS : Problem described above occurs in Firefox 3.5 (in IE the solution of Reg works great)

20 Oct 2009

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.

22 Jun 2010

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

16 Aug 2010

Dan

Sweet, thanks! you just saved me a tonne of time tracing down the root cause of the disappearing background issue.

Cheers,

Dan

Submit a comment

Submit comment