24

I would like to have images for my background that change automatically like a slide show. But I would like the images to fill the background no matter what size the browser window is...and if it is scaled down, the aspect ratio of the image does not change.

Here is an example of what I'm trying to do:

http://www.thesixtyone.com/

How can I accomplish these tasks? Any help will be much appreciated.

Sev
  • 15,401
  • 9
  • 56
  • 75

4 Answers4

85

With CSS3, you would use background-size property.

background-size: contain; Scale the image, while preserving its intrinsic aspect ratio (if any), to the largest size such that both its width and its height can fit inside the background positioning area.

Contain always fits the entire image within your viewport, leaving opaque borders on either the top-bottom or the left-right whenever the ratio of the background image and browser window are not the same.

background-size: cover; Scale the image, while preserving its intrinsic aspect ratio (if any), to the smallest size such that both its width and its height can completely cover the background positioning area.

Cover always fills the browser window, cutting off some hair or ears in the process, which is what I personally prefer for most cases. You can control how your image is aligned within the viewport by using the background-position property.

drudge
  • 35,471
  • 7
  • 34
  • 45
13

You can't, at least not like you are trying to now.

What you can do, however, is create an <img> instead, and with css set position:absolute, scale it to 100% width and crop it from the parent with overflow:hidden

example: http://jsfiddle.net/GHmz7/4/

  • and how to get it to go behind all other elements that are on my page? z-value ? – Sev Apr 05 '11 at 23:40
  • your example is stretching the width of the image (changing the aspect ratio) when you resize the browser. i don't want the aspect ratio changing...just like on the example i linked above. – Sev Apr 05 '11 at 23:43
  • 2
    the aspect ratio isn't changing. It's stretching the width & height proportionally. If you use a higher-res image, it'll look fine, I just used a low-res google logo :) –  Apr 05 '11 at 23:46
  • ok, last question...why is it that it's not filling the entire page, and only that thin rectangular box? how can i fix that so that it fills the entire page? – Sev Apr 05 '11 at 23:53
  • that's just how high the div i made was... I updated the example –  Apr 06 '11 at 00:00
2

You could try jquery/js to change the background image:

<!doctype html>
<html>
    <head>
        <title>Width resolution</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
            body {
                font-size: 20px;
                font-family: arial,helvetica,sans-serif;
                font-weight: 700;
                color:#eee;
                text-shadow: 0px 0px 1px #999;
}
            div {
                width:auto;
                height:340px;
                border:#ccc groove 2px;
                padding:5px;
            }
        </style>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('body').css({'background-color':'#ccc'});

                var sw = screen.width;

            function wres() {
                switch(sw) {
                    case 1920:
                        $('div').css({'background':'#192000 url(bg-1920.jpg)'});
                        $('body').css({'background':'#001920 url(bg-1920.jpg)'});
                        break;
                    case 1600:
                        $('div').css({'background':'#160000 url(bg-1600.jpg)'});
                        $('body').css({'background':'#001600 url(bg-1600.jpg)'});
                        break;
                    case 1280:
                        $('div').css({'background':'#128000 url(bg-1280.jpg)'});
                        $('body').css({'background':'#001280 url(bg-1280.jpg)'});
                        break;
                    case 1152:
                        $('div').css({'background':'#115200 url(bg-1152.jpg)'});
                        $('body').css({'background':'#001152 url(bg-1152.jpg)'});
                        break;
                    default:
                        $('div').css({'background':'#102400 url(bg-1024.jpg)'});
                        $('body').css({'background':'#001024 url(bg-1024.jpg)'});
                }
                    //alert(rsw);
                    //$('div').html(rsw);
                    $('.res').append(' '+sw);
                    $('div.res').css('width', 1920);
                }
                //alert(sw);
                wres();
            });
        </script>
    </head>
    <body>
        <h1 class="res">Resolução atual:</h1>
        <div class="res">The div</div>
    </body>
</html>

You can create CSS classes for the backgrounds instead, and use .toggleClass()

msmafra
  • 1,704
  • 3
  • 21
  • 34
0

You can not reliably resize background images across all browsers/versions etc...

The effect you see on thesixtyone.com site is achieved by stretching a normal inline image to 100% of it's container which is a placeholder division. Other elements are then floated over the top of this 'background' division.

So the answer is not to use a background image, but to make an image fill the screen in a placeholder, then put other elements on top of it.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • 1
    Consider deleting this answer things have moved on since 2011. CSS3 allows this via background-size. – DalSoft Jan 05 '14 at 15:46