views:

61

answers:

2

How can I make a full page background image and not have it stretched (something like this)?

<html>
<style>
   html, body {height:100%;}
     #background {
     min-height:100%;
     min-width:1024px;
     width:100%;
     height:auto;
     position:fixed;
     top:0;
     left:0;
     z-index:-1;
  }
   @media screen and (max-width: 1024px){ #background{left:50%;margin-left:-512px;}}
</style>

</head>
<body>
   <img id="background" src="http://www.iwallpapers.in/bulkupload/C/Mountains/Mountains%2040.jpg" alt="" />
</body>
</html>

Pastebin here

EDIT:

The above code might illustrate my goal better. When you re-size the window to let's say 400px you get empty space under the image. I want the image to fill the entire window and keep its aspect ratio.

<!DOCTYPE html>
<html lang="en">
<head><head>
<title></title>
<style>
*{margin:0; padding:0;}
#wrap{ 
    width:100%; 
    height:auto; 
    margin:0 auto;  
}
#bg{ width:100%}
</style>
</head>

<body>
    <div id="wrap">
        <img src="http://www.bmwgallery.co.uk/bmw-motorcycles/bmw-s1000rr-superbike-motorcycle-large.jpg" id="bg" />
    </div>
</div>

</body>
</html>
A: 

Hey Hey,

If you want to make the background change dynamically like that I think you have to use JavaScript. If you don't want the image to warp you have to use one dimension and preserve the aspect ratio like:

<!doctype HTML>
<html>
<head>
<style type = "text/css">
#backgroundDiv{position:absolute;}
</style>
<script type="text/javascript" language="JavaScript" src="jquery-1.4.2.min.js"></script>
<script type ="text/javascript" language="JavaScript">
function resizeImg(){
        var h = parseInt($(document).height()) - 50;
                var imgh = $("#bg-image").height();
                var imgw = $("#bg-image").width();
        $("#bg-image").height(h);
        $("#bg-image").width(Math.floor(imgw*(h/imgh)));
}
$(document).ready(function(){
        resizeImg();
        $(window).resize(function(){
                resizeImg();
        });
});
</script>
</head>
<body>
<div style = "height:100%;width:100%;" id = "backgroundDiv"><img src = "slider-image.jpg" id = "bg-image"></div>
</body>
</html>

Hope this helps! (try adjusting the z-index of the image if you want to make it in the background)

akellehe
A: 

You can also use flash background (image as swf, maybe add some effect).

http://webarto.com/kontakt

Also, your image must be somehow isolated, in order not to stretch it. On PL site image is painted black on sides, a is fixed by height.

Webarto
It has a fixed height but you can see in firebug that the dimensions of the image change according to window's size.
Eeyore