1. Home >
  2. Computers & Internet >
  3. Programming & Design >
  4. Resolved Question
CJ=^.^= CJ=^.^=
Member since:
October 07, 2006
Total points:
104 (Level 1)

Resolved Question

Show me another »

On a web page, how do you make the background stretch to fit the page rather than creating a tiled image?

For example, I used this to put the backround on...
<body background="http://img185.imageshack.us/…
  • 2 years ago
Doug V by Doug V
Member since:
April 17, 2006
Total points:
107272 (Level 7)

Best Answer - Chosen by Voters

You could do that with JavaScript, because you need to know the size of the client window in order to know how large the background image should be.

Something like this:

<script type="text/javascript">
function stretchBGToFit() {
//get background image
var bgImg = new Image;
//change line below to be relative path to your background image
bgImg.src = "path/to/bgimage.jpg";

//set image to have same dimensions as current window
var imgX = self.outerwidth;
var imgY = self.outerheight;
bgImg.width = imgX;
bgImg.height = imgY;

//assign image to background of page
document.body.style.backgroundImage = "url(" + bgImg + ")";
document.body.style.backgroundRepeat = "no-repeat";
}
</script>

Add this onload event to your BODY tag:

<body onload="stretchBGToFit()">
  • 2 years ago
75% 3 Votes

There are currently no comments for this question.

Other Answers (1)

  • Tracy L by Tracy L
    A Top Contributor is someone who is knowledgeable in a particular category.
    Member since:
    October 22, 2006
    Total points:
    67400 (Level 7)
    Badge Image:
    A Top Contributor is someone who is knowledgeable in a particular category.
    Contributing In:
    Computer Networking
    Aircraft
    Well you can use script or CSS to autosize based upon the users window size... OR you can set your page size to "center" your page and use a fixed width for the main content. Then you aren't tiling the image.

    Just set up a table to the width you want and center that table. It will appear much like this page does which doesn't fill the screen either but still looks very good. Use a textured or solid background for the boarders.

    Just another thought.
    • 2 years ago
    25% 1 Vote

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback