Layering multiple background images (CSS3)

http://www.w3.org/TR/2005/WD-css3-background-20050216/#layering

Layering multiple background images

The properties ‘background-image’, ‘background-origin’, ‘background-clip’, ‘background-repeat’, ‘background-size’, and ‘background-position’ may have multiple comma-separated values. Excepting the case that ‘background-image’ is ‘none’, if the values are specified as follows:

backgound-image: w1,…wM
backgound-repeat: x1,…xR
backgound-size: y1,…yS
backgound-position: s1,…sP

the number of layers is N = max(M, R, S, P) [shouldn’t it be M instead?].

Each of the properties is interpreted as if it had N values, by repeating the specified values like this:

backgound-image: w1,…wM, w1,…wM, w1,… /* N values */
backgound-repeat: x1,…xR, x1,…xR, x1,… /* N values */
backgound-size: y1,…yS, y1,…yS, y1,… /* N values */
backgound-position: s1,…rP, s1,…rP, s1,… /* N values */

This set of declarations:

background-image: url(flower.png), url(ball.png), url(grass.png);
background-position: center center, 20% 80%, top left;
background-origin: border, content;

has exactly the same effect as this set with the origin values repeated (bolded for clarity):

background-image: url(flower.png), url(ball.png), url(grass1.png);
background-position: center center, 20% 80%, top left;
background-origin: border, content, border;

Likewise, this set of declarations:

background-image: url(red.png), url(blue.png);
background-repeat: repeat-x, repeat-y, repeat-y;
background-position: 20% 25%, 40% 10%, 50% 15%, 70% 40%, 90% 35%;

has the same effect as:

background-image: url(red.png), url(blue.png), url(red.png),
    url(blue.png), url(red.png);
background-repeat: repeat-x, repeat-y, repeat-y, repeat-x, repeat-y;
background-position: 20% 25%, 40% 10%, 50% 15%, 70% 40%, 90% 35%;

There are other ways to add missing values: repeat the last value at the end, repeat the first value at the start, fill either at the end or at the start with the initial value…

Each of the images is repeated, sized, and positioned according to the corresponding value in the other properties. The first image in the list is the layer closest to the user, the next one is painted behind the first, and so on.

If ‘background-image’ is ‘none’, there are no layers (N = 0).

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s