The background-image
CSS property sets one or more background images on an element.
The background-image
CSS property sets one or more background images on an element.
The background images are drawn on stacking context layers on top of each other. The first layer specified is drawn as if it is closest to the user.
The borders of the element are then drawn on top of them, and the background-color
is drawn beneath them. How the images are drawn relative to the box and its borders is defined by the background-clip
and background-origin
CSS properties.
If a specified image cannot be drawn (for example, when the file denoted by the specified URI cannot be loaded), browsers handle it as they would a none
value.
Note: Even if the images are opaque and the color won't be displayed in normal circumstances, web developers should always specify a background-color
. If the images cannot be loaded—for instance, when the network is down—the background color will be used as a fallback.
Each background image is specified either as the keyword none
or as an <image>
value.
To specify multiple background images, supply multiple values, separated by a comma:
background-image: linear-gradient( to bottom, rgba(255, 255, 0, 0.5), rgba(0, 0, 255, 0.5) ), url("catfront.png"); /* Global values */ background-image: inherit; background-image: initial; background-image: revert; background-image: revert-layer; background-image: unset;
none
Is a keyword denoting the absence of images.
<image>
Is an <image>
denoting the image to display. There can be several of them, separated by commas, as multiple backgrounds are supported.
Browsers do not provide any special information on background images to assistive technology. This is important primarily for screen readers, as a screen reader will not announce its presence and therefore convey nothing to its users. If the image contains information critical to understanding the page's overall purpose, it is better to describe it semantically in the document.
Initial value | none |
---|---|
Applies to | all elements. It also applies to ::first-letter and ::first-line . |
Inherited | no |
Computed value | as specified, but with url() values made absolute |
Animation type | discrete |
background-image =
<bg-image>#
<bg-image> =
<image> |
none
<image> =
<url> |
<gradient>
<url> =
url( <string> <url-modifier>* ) |
src( <string> <url-modifier>* )
Note that the star image is partially transparent and is layered over the cat image.
<div> <p class="catsandstars">This paragraph is full of cats<br />and stars.</p> <p>This paragraph is not.</p> <p class="catsandstars">Here are more cats for you.<br />Look at them!</p> <p>And no more.</p> </div>
p { font-size: 1.5em; color: #fe7f88; background-image: none; background-color: transparent; } div { background-image: url("mdn_logo_only_color.png"); } .catsandstars { background-image: url("startransparent.gif"), url("catfront.png"); background-color: transparent; }
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
background-image |
1 | 12 | 1If thebrowser.display.use_document_colors user preference in about:config is set to false , background images will not be displayed. |
4 | 3.5 | 1 | ≤37 | 18 | 4If thebrowser.display.use_document_colors user preference in about:config is set to false , background images will not be displayed. |
14 | 1 | 1.0 |
element |
No | No | 4 | No | No | No | No | No | 4 | No | No | No |
gradients |
1Some versions support only experimental gradients prefixed with-webkit . |
12 | 3.6Some versions support only experimental gradients prefixed with-moz . |
10 | 11Some versions support only experimental gradients prefixed with-o . |
4Some versions support only experimental gradients prefixed with-webkit . |
≤37Some versions support only experimental gradients prefixed with-webkit . |
18Some versions support only experimental gradients prefixed with-webkit . |
4Some versions support only experimental gradients prefixed with-moz . |
14Some versions support only experimental gradients prefixed with-webkit . |
3.2Some versions support only experimental gradients prefixed with-webkit . |
1.0Some versions support only experimental gradients prefixed with-webkit . |
image-rect |
No | No | 4 | No | No | No | No | No | 4 | No | No | No |
image-set |
21 | 79 | 9088Before Firefox 89, thetype() function is not supported as an argument to image-set() . |
No | 15 | preview | 4.4 | 25 | 9088Before Firefox 89, thetype() function is not supported as an argument to image-set() . |
14 | 1.5 | |
multiple_backgrounds |
1 | 12 | 3.6 | 9 | 10.5 | 1.3 | ≤37 | 18 | 4 | 14 | 1 | 1.0 |
svg_images |
8 | 12 | 4 | 9 | 9.5 | 5Support of SVG in CSS background is incomplete. |
≤37 | 18 | 4 | 14 | 5Support of SVG in CSS background is incomplete. |
1.0 |
<img>
<image>
, <gradient>
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/background-image