Here's an interesting way to add multiple background images to an element using CSS2. Before anyone gets too excited, since this technique uses generated content it doesn't work with IE6 nor IE7. Maybe IE8 will support generated content, but who knows.

Still it's fun to try, and if you can ignore IE, then this might be useful sometimes. It works in Firefox 2/3 and Opera 9 under Windows XP, at least.

In this article I'll first guide you through a basic example, and then we'll create a nicer box.

Tutorial: a basic example

Let's say we have a DIV with a tiled background image. We would like to add four more images, one at each corner of the DIV. We also want to be able to apply this to any box, which means we don't know the box's width nor height.

The idea is to use the :before and :after pseudo classes to add the extra images. We can actually add two images for each of those selectors, which makes a total of five images for the DIV! Let's see the code:

Let's analyze the CSS code. The first statement simply assigns a background image to the DIV, which will be repeated to cover all its area.

The second statement is where the fun begins: first we set the display property to block, so we can add both the left and right corner images. We also set the height to match the height of the images. Finally we set the font size to 0: this is because if the corner images are small and/or the font size is resized to very big, the images can move down, away from the corners of the box. Give it a try and you'll see what I mean. All these rules apply to both :before and :after, so we combine them in one statement.

And then the nice trick to add two images instead of just one: the first one we add with the content property, using a url as the value. The second image we add with the background property, and position it to the top right.

You can see the result page here. Try resizing the browser and you'll see that the corner images stay in their place independently of the size of the DIV. Nice!

A nicer example

Let's use this technique to create a box with an image at the center, and four fancy images on each corner. The box doesn't have a fixed width or height, so we need to put each image in the corner independently. The result should look like this:

The code is very similar to what we saw in the first example. The difference here is that we want to add some padding to the left and right of the DIV. The problem then is that the generated content will also be affected by this padding, moving the corner images towards the center of the DIV. This has an easy solution: simply compensate the DIV's padding with negative margins in both :before and :after. This brings back the images to the corners.

Have a look at the result here. Again, resizing the browser is safe, since the corner images will reposition themselves when the DIV dimensions change.

I think this technique allows for a huge variety of applications, both with fixed width and non-fixed width boxes. Unfortunately the fact that IE doesn't support generated content turns it into just an interesting experiment, but with very limited use (if any) in real applications. Hopefully IE8 will change things, but even then we'll still have to deal with IE7 for a long time (maybe even IE6).

Note that all this won't be much use once CSS3 becomes a reality, since it will offer multiple backgrounds as part of the specification.