7

I have a large image to be use as a background-image of a page. What I want is that the height of the image will be stretched to fill the height of the page. It should be centered also.

background: black url("/image/background.jpg") no-repeat fixed center;
JR Galia
  • 17,229
  • 19
  • 92
  • 144

4 Answers4

16

background-size: cover will do the trick in modern browsers - see mozilla's documentation for more details.

For older browsers (particularly older versions of IE), I've had a lot of success with jQuery plugins like this one to emulate the behaviour.

Kelvin
  • 5,227
  • 1
  • 23
  • 36
4

here is a good writeup

http://css-tricks.com/perfect-full-page-background-image/

the gist of it being

body { 
background: url(images/bg.jpg) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
foundry
  • 31,615
  • 9
  • 90
  • 125
0

add this to the css

{
background: black url("/image/background.jpg") no-repeat fixed center;
height: 100%;
width:100%
}
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • 1
    Kelvin's answer is perfect if the image is actually larger then the space you want to put it in and will crop it , setting width and height will stretch the image – Scott Selby Jan 09 '13 at 00:18
0

I think using a div would be the easiest way to get the effect you are looking for.

<div id="background">
    <img src="/image/background.jpg" class="stretch" alt="" />
</div>
    <style>
    #background {
        background-color:#000000;
        width: 100%; 
        height: 100%; 
        position: fixed; 
        left: 0px; 
        top: 0px; 
        z-index: -1;
    }

    .stretch {
        width:100%;
        height:100%;
    }
    </style>
Devon Bernard
  • 2,250
  • 5
  • 19
  • 32