Multiple backgrounds

Table of contents

  1. 1. Example
  2. 2. See also
Table of contents
  1. 1. Example
  2. 2. See also

With CSS3, you can apply multiple backgrounds to elements. These are layered atop one another with the first background you provide on top and the last background listed in the back. Only the last background can include a background color.

Specifying multiple backgrounds is easy:

.myclass {
  background: background1, background 2, ..., backgroundN;
}

You can do this with both the shorthand background property and the individual properties thereof except for background-color. That is, the following background properties can be specified as a list, one per background: background , background-attachment , background-clip , background-image , background-origin , background-position , background-repeat , background-size .

Example

In this example, three backgrounds are stacked: the Firefox logo, a linear gradient, and an image with flowers:

.multi_bg_example {
  background: url(http://demos.hacks.mozilla.org/openweb/resources/images/logos/firefox-48.png),
        -moz-linear-gradient(left, rgba(255, 255, 255, 0),  rgba(255, 255, 255, 1)),
        url(http://demos.hacks.mozilla.org/openweb/resources/images/patterns/flowers-pattern.jpg);
  background-repeat: no-repeat, no-repeat, repeat;
  background-position: bottom right, left, right;
}
Screen Shot Live Demonstration
css_multibg.png  

As you can see here, the Firefox logo (listed first) is on top, followed by the gradient, which is layered atop the flowered background. Each subsequent sub-property (background-repeat and background-position ) applies to the corresponding backgrounds. So the first listed value for background-repeat applies to the first (frontmost) background, and so forth.

Tags (5)

Edit tags

Attachments (1)

FileSizeDateAttached by 
 css_multibg.png
Layers screenshot
29.16 kB19:04, 23 Dec 2009SheppyActions

Attach file