background-image
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.
Syntax
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: unset;
Values
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.
Accessibility concerns
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.
Formal definition
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 |
Formal syntax
<bg-image>#where
<bg-image> = none | <image>
where
<image> = <url> | <image()> | <image-set()> | <element()> | <paint()> | <cross-fade()> | <gradient>
where
<image()> = image( <image-tags>? [ <image-src>? , <color>? ]! )
<image-set()> = image-set( <image-set-option># )
<element()> = element( <id-selector> )
<paint()> = paint( <ident>, <declaration-value>? )
<cross-fade()> = cross-fade( <cf-mixing-image> , <cf-final-image>? )
<gradient> = <linear-gradient()> | <repeating-linear-gradient()> | <radial-gradient()> | <repeating-radial-gradient()> | <conic-gradient()>where
| rtl
= ltr
<image-src> = <url> | <string>
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hwb()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
<image-set-option> = [ <image> | <string> ] [ <resolution> || type(<string>) ]
<id-selector> = <hash-token>
<cf-mixing-image> = <percentage>? && <image>
<cf-final-image> = <image> | <color>
<linear-gradient()> = linear-gradient( [ <angle> | to <side-or-corner> ]? , <color-stop-list> )
<repeating-linear-gradient()> = repeating-linear-gradient( [ <angle> | to <side-or-corner> ]? , <color-stop-list> )
<radial-gradient()> = radial-gradient( [ <ending-shape> || <size> ]? [ at <position> ]? , <color-stop-list> )
<repeating-radial-gradient()> = repeating-radial-gradient( [ <ending-shape> || <size> ]? [ at <position> ]? , <color-stop-list> )
<conic-gradient()> = conic-gradient( [ from <angle> ]? [ at <position> ]?, <angular-color-stop-list> )where
<rgb()> = rgb( <percentage>{3} [ / <alpha-value> ]? ) | rgb( <number>{3} [ / <alpha-value> ]? ) | rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? )
<rgba()> = rgba( <percentage>{3} [ / <alpha-value> ]? ) | rgba( <number>{3} [ / <alpha-value> ]? ) | rgba( <percentage>#{3} , <alpha-value>? ) | rgba( <number>#{3} , <alpha-value>? )
<hsl()> = hsl( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsl( <hue>, <percentage>, <percentage>, <alpha-value>? )
<hsla()> = hsla( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsla( <hue>, <percentage>, <percentage>, <alpha-value>? )
<hwb()> = hwb( [<hue> | none] [<percentage> | none] [<percentage> | none] [ / [<alpha-value> | none] ]? )
<side-or-corner> = [ left | right ] || [ top | bottom ]
<color-stop-list> = [ <linear-color-stop> [, <linear-color-hint>]? ]# , <linear-color-stop>
<ending-shape> = circle | ellipse
<size> = closest-side | farthest-side | closest-corner | farthest-corner | <length> | <length-percentage>{2}
<position> = [ [ left | center | right ] || [ top | center | bottom ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]? | [ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ] ]
<angular-color-stop-list> = [ <angular-color-stop> [, <angular-color-hint>]? ]# , <angular-color-stop>where
<alpha-value> = <number> | <percentage>
<hue> = <number> | <angle>
<linear-color-stop> = <color> <color-stop-length>?
<linear-color-hint> = <length-percentage>
<length-percentage> = <length> | <percentage>
<angular-color-stop> = <color> && <color-stop-angle>?
<angular-color-hint> = <angle-percentage>where
<color-stop-length> = <length-percentage>{1,2}
<color-stop-angle> = <angle-percentage>{1,2}
<angle-percentage> = <angle> | <percentage>
Examples
Layering background images
Note that the star image is partially transparent and is layered over the cat image.
HTML
<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>
CSS
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;
}
Result
Specifications
Specification |
---|
CSS Backgrounds and Borders Module Level 4 # background-image |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
background-image | ChromeFull support1 | EdgeFull support12 | FirefoxFull support1 | Internet ExplorerFull support4 | OperaFull support3.5 | SafariFull support1 | WebView AndroidFull support37 | Chrome AndroidFull support18 | Firefox for AndroidFull support4 | Opera AndroidFull support14 | Safari on iOSFull support1 | Samsung InternetFull support1.0 |
element() | ChromeNo supportNo | EdgeNo supportNo | FirefoxFull support4 | Internet ExplorerNo supportNo | OperaNo supportNo | SafariNo supportNo | WebView AndroidNo supportNo | Chrome AndroidNo supportNo | Firefox for AndroidFull support4 | Opera AndroidNo supportNo | Safari on iOSNo supportNo | Samsung InternetNo supportNo |
Gradients | ChromeFull support1 | EdgeFull support12 | FirefoxFull support3.6 | Internet ExplorerFull support10 | OperaFull support11 | SafariFull support4 | WebView AndroidFull support37 | Chrome AndroidFull support18 | Firefox for AndroidFull support4 | Opera AndroidFull support14 | Safari on iOSFull support3.2 | Samsung InternetFull support1.0 |
image-rect() | ChromeNo supportNo | EdgeNo supportNo | FirefoxFull support4 | Internet ExplorerNo supportNo | OperaNo supportNo | SafariNo supportNo | WebView AndroidNo supportNo | Chrome AndroidNo supportNo | Firefox for AndroidFull support4 | Opera AndroidNo supportNo | Safari on iOSNo supportNo | Samsung InternetNo supportNo |
image-set() | ChromeFull support21 | EdgeFull support79 | FirefoxNo supportNo | Internet ExplorerNo supportNo | OperaFull support15 | SafariPartial support6 | WebView AndroidFull support4.4 | Chrome AndroidFull support25 | Firefox for AndroidNo supportNo | Opera AndroidFull support14 | Safari on iOSPartial support6 | Samsung InternetFull support1.5 |
Multiple backgrounds | ChromeFull support1 | EdgeFull support12 | FirefoxFull support3.6 | Internet ExplorerFull support9 | OperaFull support10.5 | SafariFull support1.3 | WebView AndroidFull support37 | Chrome AndroidFull support18 | Firefox for AndroidFull support4 | Opera AndroidFull support14 | Safari on iOSFull support1 | Samsung InternetFull support1.0 |
SVG images | ChromeFull support8 | EdgeFull support12 | FirefoxFull support4 | Internet ExplorerFull support9 | OperaFull support9.5 | SafariFull support5 | WebView AndroidFull support37 | Chrome AndroidFull support18 | Firefox for AndroidFull support4 | Opera AndroidFull support14 | Safari on iOSFull support5 | Samsung InternetFull support1.0 |
Legend
- Full support
- Full support
- Partial support
- Partial support
- No support
- No support
- Non-standard. Check cross-browser support before using.
- See implementation notes.
- Requires a vendor prefix or different name for use.
See also
- Implementing image sprites in CSS
<img>
- Image-related data types:
<image>
,<gradient>
- Image-related functions: