Although a little out of fashion at the moment, it used to be a fairly common design requirement to build a page with a different background image at the top of the page than at the bottom. Or perhaps to use different background images for the top and bottom of a content section within a page. Back in the day, with CSS2.1, achieving the effect typically required additional markup (one element for the header background and another for the footer background).
With CSS3 you can stack as many background images as you need on an element.
Here's the syntax:
.bg {
background:
url('../img/1.png'),
url('../img/2.png'),
url('../img/3.png');
}As with the stacking order of multiple shadows, the image listed first appears nearest to the top in the browser. You can also add a general color for the background in the same declaration if you wish, like this:
.bg {
background:
url('../img/1.png'),
url('../img/2.png'),
url('../img/3.png') left bottom, black;
}Specify the color last and this will show below every image specified above.
Browsers that don't understand the multiple backgrounds rule (such as Internet Explorer 8 and below) will ignore the rule altogether, so you may wish to declare a 'normal' background property immediately before a CSS3 multiple background rule as a fallback for really old browsers.
With the multiple background images, as long as you're using PNG files with transparency, any partially transparent background images that sit on top of another will show through below. However, background images don't have to sit on top of one another, nor do they all have to be the same size.
To set different sizes for each image, use the background-size property. When multiple images have been used, the syntax works like this:
.bg {
background-size: 100% 50%, 300px 400px, auto;
}The size values (first width, then height) for each image are declared, separated by commas, in the order they are listed in the background property. As in the example above, you can use percentage or pixel values for each image alongside the following:
If you have different background images, at different sizes, the next thing you'll want is the ability to position them differently. Thankfully, the background-position property facilitates that too.
Let's put all this background image capability together, alongside some of the responsive units we have looked at in previous chapters.
Let's create a simple space scene, made with a single element and three background images, set at three different sizes, and positioned in three different ways:
.bg-multi {
height: 100vh;
width: 100vw;
background:
url('rosetta.png'),
url('moon.png'),
url('stars.jpg');
background-size: 75vmax, 50vw, cover;
background-position: top 50px right 80px, 40px 40px, top center;
background-repeat: no-repeat;
}You'll see something like this in the browser:

We have the stars image at the bottom, then the moon on top, and finally an image of the Rosetta space probe on top. View this for yourself in example_06-06. Notice that if you adjust the browser window, the responsive length units work well (vmax, vh, and vw) and retain proportion, while pixel based ones do not.
There is a shorthand method of combining the different background properties together. You can read the specification for it at http://www.w3.org/TR/css3-background/. However, my experience so far has been that it produces erratic results. Therefore, I recommend the longhand method and declare the multiple images first, then the size, and then the position.
Read the W3C documentation on multiple background elements at http://www.w3.org/TR/css3-background/.