Archive

Posts Tagged ‘Perfect Full Page Background Image’

Perfect Full Page Background Image

December 3, 2009 2 comments

We visited this concept of re-sizeable background images before… but reader Doug Shults sent me in a link that uses a really awesome technique that I think is better than any of the previous techniques.

This technique and the above background image is credited to this site. Here is what this technique is going to accomplish:

  • Fills entire page with image, no white space
  • Scales image as needed
  • Retains image proportions
  • Image is centered on page
  • Does not cause scrollbars
  • Cross-browser compatible
  • Isn’t some fancy shenanigans like Flash

This is a very tall order, and we are going to be pulling out all kinds of different stuff to make it happen. First of all, because the image needs to scale, a traditional CSS background-image is already disqualified. That leaves us with an inline image.

Technique #1

This inline image is going to be placed on the page inside of a bunch of wrappers, each necessary for accomplishing all our goals.

<div id="bg">
    <div>
        <table cellspacing="0" cellpadding="0">
            <tr>
                <td>
                    <img src="images/bg.jpg" alt=""/>
                </td>
            </tr>
        </table>
    </div>
</div>

And the CSS:

* {
    margin: 0;
    padding: 0;
}

html, body, #bg, #bg table, #bg td {
    height:100%;
    width:100%;
    overflow:hidden;
}

#bg {
    position: fixed;
}

#bg div {
    height:200%;
    left:-50%;
    position:absolute;
    top:-50%;
    width:200%;
}

#bg td {
    text-align:center;
    vertical-align:middle;
}

#bg img {
    margin:0 auto;
    min-height:50%;
    min-width:50%;
}

That’s quite a little load of markup and CSS, but it does the trick very well! Just doing this alone gets the job done, but what about if we want actual content on the page. Setting that overflow to hidden on the html and body elements should scare you a little bit, as that will totally cut off any content that is below the fold on a site with no scrollbars. In order to bring back scrollable content, we’ll introduce another wrapper. This one will sit on top of the background, be the full width and height of the browser window, and set the overflow back to auto (scrollbars). Then inside of this wrapper we can put content safely.

<div id="cont">
    <div>
          <!-- content in here -->
    </div>
</div>
#cont {
    position:absolute;
    top:0;left:0;
    z-index:70;
    overflow:auto;
}

.box {
    margin: 0 auto;
    width: 400px;
    padding: 50px;
    background: white;
    padding-bottom:100px;
    font: 14px/2.2 Georgia, Serif;
}

JavaScript Fixes

For IE 6, there is some MooTools stuff going on to fix it up

In Firefox, default “focus” is placed on the body element when the page loads. That means that pressing the space bar will scroll down the body, which would reveal some ugly white space. We can use some jQuery for a quick fix, to remove that focus and place it on a hidden element instead, removing that problem:

$(function(){
       $(".box").prepend('<input type="text" id="focus-stealer" type="hidden" />');
       $("#focus-stealer").focus();
});

The addition of fixed positioning on the #bg wrapper eliminated the space bar/scrolldown problem. CSS FTW!

View Demo Download Files

Current Bugs

  • In Safari 4 & Chrome, the min-height isn’t catching and doesn’t resize vertically to fit.

Technique #2

Big thanks, as usual, to Doug Neiner for this alternate version.

Here, we can use CSS with no JavaScript fixes, and the image is just an inline image with a class name of “bg” and no extra markup. That’s a big win for all the folks unhappy about the extra markup.

The only caveat is that it doesn’t quite meet all the requirements layed out about, in that it doesn’t center in IE 7, doesn’t work at all in IE 6, and may not always be proportionate depending on the original image size. BUT, since it’s simpler and bug-free, it’s defenitly a solid option. Here is the CSS:

img.bg {
	/* Set rules to fill background */
	min-height: 100%;
	min-width: 1024px;

	/* Set up proportionate scaling */
	width: 100%;
	height: auto;

	/* Set up positioning */
	position: fixed;
	top: 0;
	left: 0;
}

@media screen and (max-width: 1024px){
	img.bg {
		left: 50%;
		margin-left: -512px; }
}

div#content {
	/* This is the only important rule */
	/* We need our content to show up on top of the background */
	position: relative; 

	/* These have no effect on the functionality */
	width: 500px;
	margin: 0 auto;
	background: #fff;
	padding: 20px;
	font-family: helvetica, arial, sans-serif;
	font-size: 10pt;
	line-height: 16pt;
	-moz-box-shadow: #000 4px 4px 10px;
	-webkit-box-shadow: #000 4px 4px 10px;
}

body {
	/* These rules have no effect on the functionality */
	/* They are for styling only */
	margin: 0;
	padding: 20px 0 0 0;
}

View Demo

Follow

Get every new post delivered to your Inbox.

Join 89 other followers