views:

26451

answers:

6

Duplicate of: Layering images in CSS - possible to put 2 images in same element?


Is it possible to have two background images? For instance, I'd like to have one image repeat across the top (repeat-x), and another repeat across the entire page (repeat), where the one across the entire page is behind the one which repeats across the top.

I've found that I can achieve the desired effect for two background images by setting the background of html and body:

html {
    background: url(images/bg.png);
}

body {
    background: url(images/bgtop.png) repeat-x;
}

Is this "good" CSS? Is there a better method? And what if I wanted three or more background images?

A: 

Short answer, no you can't.

scunliffe
If you don't want to take the time to answer, then don't. And yes, he can have two (or more) background images. He didn't say he wanted them on one element. Really there is no reason to be snide.
Mahdi.Montgomery
Sorry @Mahdi. M - I wasn't trying to be snide I was merely trying to answer the question. Although @Rudd Zwolinski didn't mention that there was only 1 element I presumed it was implied since you can have 500 background elements if you add more elements. (The duplicate link added 1min after my response points to a similar question about 1 element so I wasn't the only one making this presumption.) Otherwise I stand by my answer - currently there is no way in CSS to specify multiple background images... [for a single element],...
scunliffe
... if you are willing to add more elements for every background image you want... then there are no limits.
scunliffe
+1  A: 

You could have a div for the top with one background and another for the main page, and seperate the page content between them or put the content in a floating div on another z-level. The way you are doing it may work but I doubt it will work across every browser you encounter.

Sean James
+17  A: 

CSS3 is going to allow this sort of thing and it'll look something like this:

body {
    background-image: url(images/bg.png) url(images/bgtop.png);
    background-repeat: repeat repeat-x;
}

but that doesn't work in pretty much any common browser today and could be years before IE implements it, and then even more years before enough people upgrade to that version.

The best way you can work around it is to have extra divs:

<body>
    <div id="bgTopDiv">
        content here
    </div>
</div>

body {
    background-image: url(images/bg.png)
}
#bgTopDiv {
    background-image: url(images/bgTop.png);
    background-repeat: repeat-x;
}
nickf
Multiple backgrounds are supported by the latest versions of Firefox, Chrome, Safari and Opera. It will be supported in IE9, too. http://en.wikipedia.org/wiki/Comparison_of_layout_engines_(Cascading_Style_Sheets)
Šime Vidas
+2  A: 

Yes, it is possible, and has been implemented by popular usability testing website Silverback. If you look through the source code you can see that the background is made up of several images, placed on top of each other.

Here is the article demonstrating how to do the effect can be found on Vitamin. A similar concept for wrapping these 'onion skin' layers can be found on A List Apart.

EnderMB
A: 

What are you guys talking about? You can actually have two background images without having to have another div. Just use background-image: url(), url(); background-repeat: background-position:

Erza_Scarlet
+1  A: 

Current version of FF and IE and some other browsers support multiple background images in a single CSS2 declaration. Look here http://dense13.com/blog/2008/08/31/multiple-background-images-with-css2/ and here http://www.quirksmode.org/css/multiple_backgrounds.html and here http://nicolasgallagher.com/multiple-backgrounds-and-borders-with-css2/

For IE, you might consider adding a behavior. Look here: http://css3pie.com/

Shannon