tags:

views:

121

answers:

9

Hello,

How can I load images to cover the whole background like some websites, using CSS. Not the usual background-image property but I want to load the images quickly.

example sites - http://www.marinayachting.it/ http://alexandraowen.co.nz/

Thanks Jean

+6  A: 

background-image is the only way to place images in CSS. If you want it to be vary large put it on the body element or a container div that fills the entire viewport.

body {
    margin: 0;
    padding: 0;
    width: 100%;
    background-image: url('my_big_image.jpg') norepeat;
}

If you use a container div you can set position:fixed; top:0; left:0 and the image will remain stationary when the page scrolls.

There's no magic to it. As far as getting it to load quickly I don't think there's much you can do if it doesn't repeat. If it does repeat then make sure your image is the size of one module. This can be as little as one pixel tall or wide depending on the content.

jasongetsdown
how about fast loading
Jean
@jean your comment doesn't make sense. use a CDN, a faster server, or shrink your filesize. those are pretty much your only options.
Jason
example sites - http://www.marinayachting.it/ http://alexandraowen.co.nz/
Jean
did you even attempt to look at the source for that site? it's not even using css for that image. it's using an `<img />` tag. it's not doing anything special. the image size is 119.08KB
Jason
A: 

Bing is loading a normal background image with a fixed size. It´s not particularly fast (for me...), but perhaps it seems fast because the image is cached after the first time you load it.

jeroen
What´s with the downvotes for almost all answers? An explanation would be nice...
jeroen
A: 

You can set the style inline so that the image can start downloading without waiting for any css file to be ready.

galambalazs
can the guru who downvoted give me a reason why? Or you don't know what you're doing, just having too much free time...
galambalazs
+1  A: 

There is no magic to making a background image load quickly, you just:

  • Have a fast server.
  • Compress the image as much as possible.
  • Make your page HTML small so that the rest can start loading as soon as possible.
  • Don't have many other images that also has to load.
  • Don't have a lot of scripts and other external files that has to load.
Guffa
Why the downvote? If you don't say what it is that you think is wrong, it can't improve the answer.
Guffa
A: 

If you set an image let's say a picture as a background you need to make it large enough to accommodate large screen sizes. You don't want the experience on your site to be, that your picture repeats multiple times on the screen. Probably at the least width should be 1260px. If background is just a simple gradient, you can cut a small part of it in photoshop and apply it on the body like this:

body {
margin:0;
padding:0;
background:#fff url(your/image/location.jpg) repeat-x scroll 0 0;
}

This method could be applied to divs too, Good luck.

Gil
A: 

In your second example site, alexandraowen.co.nz, if you took a second to look at the JS they use, you would have seen the following:

// backgrounds --------------------------------------------------------------//

var Backgrounds = {};

Backgrounds.init = function()
{

    $('body').each
    (  
        function()
        {
            var imgsrc = $(this).css('background-image');
            if(imgsrc != 'none')
            {
                imgsrc = imgsrc.slice( imgsrc.indexOf('(') + 1 , -1);
                $(this).css('background-image', 'none');
                $(this).prepend('');
                if($.browser.msie)
                {
                    // ie 7 is the slow kid and we have to strip out quote marks ffs!
                    $(this).find('div.bg img').attr('src', imgsrc.split('"').join(''));
                }
                else
                {
                    $(this).find('div.bg img').attr('src', imgsrc);
                }
            }
        }
    );
    Backgrounds.resizeHandler();
    $(window).resize(Backgrounds.resizeHandler);
    $('div.bg img').load(Backgrounds.resizeHandler);
}

Backgrounds.resizeHandler = function()
{   
    var w = $(window).width();
    var h = $(window).height();

    $('div.bg img').each
    (  
        function()
        {   
            var wr = w / $(this).width();
            var hr = h / $(this).height();
            var r = Math.max(wr, hr);
            var imgw = Math.round($(this).width() * r);
            var imgh = Math.round($(this).height() * r);

            $(this).width( imgw );
            $(this).height(  imgh );

            var l = Math.round((w/2) - (imgw/2));
            $(this).css('margin-left', l+'px');
        }
    );
}

As well as the HTML on the page:

<body style="background-image: none; ">

If you dig into their scripts a bit more, you can see what they did. But I guarantee you it's nothing faster than just setting the background-image property.

Jason
i'm sure everyone here appreciates your thoughtless downvoting.
Jason
A: 

<img id="foo" src="bar" alt=""> with #foo { width: 100%; height: 100%; }
(use position: absolute; / position: relative; & z-index for layering as desired)

Here's an old example.

reisio
+1  A: 

Hello, I'm the guy who made marinayachting.it.

Basically, there are three way for big (and scaled) backgrounds: use css background-image, stretch an <img /> tag positioned: fixed or use both ways.

The best way is to use css background-image with the new cool background-size property. Firefox (since 3.6, i think), Safari, Chrome and Opera support it (with vendor prefixes, for now). The bad guy is (guess!) Internet Explorer. So, the trick is to target IE and Firefox < 3.6 via javascript and inject a streched <img /> tag in the document onready event.

For making things fast, export the image no larger than 1024px (on some kind of images, even smaller resolution works well) and not more than 100kb in size. A fast server or even better a CDN will help, of course.

Note that IE prior to version 8 renders streched images in a very ugly way (linear scaling) and is slooow! Using the proprietary vendor css -ms-interpolation-mode:bicubic; will help a bit, but will also make that crappy browser even slower. For best quality and performance you can use the AlphaImageLoader filter. Yes, the same used for solving png transparency issues in the old IE6. For Example. the backgrounds and the animated carousel in the gallery sections of marinayachting.it are all loaded with the AlphaImageLoader filter. The carousel in particular, wich uses slightly streched pngs animated via javascript, was running at 0.5fps without the filter!

Hope that helps. If you have more questions feel free to ask!

achairapart
+1  A: 

I found this tutorial helpful. ->

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

JGrubb