How to Use CSS3 Multiple Backgrounds

Some of the new CSS3 features that is really popular today is being able to use multiple background images. Before CSS3 came along, you would have to have several elements or divs stacked on top of each other to create a similar effect. With CSS3, we’re now able to apply several backgrounds into one element.

Here is a sample of CSS that shows how to implement multiple background images.

CSS

[CSS]
.element {
background:
url(‘path/to/image.png’) no-repeat center top, /* this will be at the top */
url(‘path/to/image2.png’) no-repeat 100px 40px, /* this will be in the middle */
url(‘path/to/image3.jpg’) no-repeat; /* this will be on the bottom */
}

[/CSS]

You’ll notice that each image is separated by a comma following the background image properties. You’ll also notice that I’ve commented how the images will be stacked within the background image element showing that the first background image will be the image at the top and the following background images will be subsequently below them.

One thing to consider is that older browsers(I’m looking at you IE6/7/8) will not recognize multiple backgrounds and will show nothing instead, so a good way is to include a fallback background image before applying multiple backgrounds. Here’s an example:

[CSS]
.element {
background: url(‘path/to/image_fallback.png’); /* fallback for older browsers */
background:
url(‘path/to/image.png’) no-repeat center top, /* this will be at the top */
url(‘path/to/image2.png’) no-repeat 100px 40px, /* this will be in the middle */
url(‘path/to/image3.jpg’) no-repeat; /* this will be on the bottom */
}

[/CSS]

Conclusion

While it’s now quite easy to finally allow for multiple background images in CSS, we must take care to also make sure that those users (who may be using older browsers) are still able to at least see some kind of fallback image.

2 thoughts on “How to Use CSS3 Multiple Backgrounds

  1. Very good info, but to little explanations on how it works, I would recommend to read this one too for additional information: multiple backgrounds in CSS3. I also heard that, PIE can make it work in older version of IE, which do not support multiple background. Did you try it? Can you show the example?

Leave a Reply

Your email address will not be published. Required fields are marked *