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?

Solution 1

CSS3 allows this sort of thing and it looks like this:

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

The current versions of all the major browsers now support it, however if you need to support IE8 or below, then the best way you can work around it is to have extra divs:

    <div id="bgTopDiv">
        content here
    background-image: url(images/bg.png);
    background-image: url(images/bgTop.png);
    background-repeat: repeat-x;

Solution 2

The easiest way I have found to use two different background images in one div is with this line of code:

body {
    background:url(image1.png) repeat-x, url(image2.png) repeat;

Obviously, that does not have to be for only the body of the website, you can use that for any div you want.

Hope that helps! There is a post on my blog that talks about this a little more in depth if anyone needs further instructions or help -

Solution 3

use this

body {
background: url(images/image-1.png), url(images/image-2.png),url(images/image-3.png);
background-repeat: no-repeat, repeat-x, repeat-y;
background-position:10px 20px , 20px 30px ,15px 25px;

a simple way to adjust you every image position with background-position: and set repeat property with background-repeat: for every image individually

Solution 4

Current version of FF and IE and some other browsers support multiple background images in a single CSS2 declaration. Look here and here and here

For IE, you might consider adding a behavior. Look here:

Solution 5

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.

Solution 6

If you want multiple background images but don't want them to overlap, you can use this CSS:

body {
  font-size: 13px;
  font-family:Century Gothic, Helvetica, sans-serif;
  color: #333;
  text-align: center;
  padding: 25px;

#topshadow {
  height: 62px
  margin: -62px
  background-image: url(images/top-shadow.png);

#pageborders {

#bottomshadow {

#page {
  text-align: left;
  margin:62px, 0px, 20px;
  background-color: white;

with this HTML structure:


<?php body_class(); ?>>

  <div id="topshadow">

  <div id="pageborders">

    <div id="page">

Solution 7

#example1 {
    background: url( left top no-repeat, url( right bottom no-repeat, url( left top repeat;
    padding: 15px;
    background-size: 150px, 130px, auto;
background-position: 50px 30px, 430px 30px, 130px 130px;
<!DOCTYPE html>


<div id="example1">
<h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>


We can easily add multiple images using CSS3. we can read in detail here

Solution 8

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.