The background
shorthand CSS property sets all background style properties at once, such as color, image, origin and size, or repeat method.
The background
shorthand CSS property sets all background style properties at once, such as color, image, origin and size, or repeat method.
This property is a shorthand for the following CSS properties:
/* Using a <background-color> */ background: green; /* Using a <bg-image> and <repeat-style> */ background: url("test.jpg") repeat-y; /* Using a <box> and <background-color> */ background: border-box red; /* A single image, centered and scaled */ background: no-repeat center/80% url("../img/image.png"); /* Global values */ background: inherit; background: initial; background: revert; background: revert-layer; background: unset;
The background
property is specified as one or more background layers, separated by commas.
The syntax of each layer is as follows:
<attachment>
<bg-image>
<position>
<bg-size>
<repeat-style>
<bg-size>
value may only be included immediately after <position>
, separated with the '/' character, like this: "center/80%
".<box>
value may be included zero, one, or two times. If included once, it sets both background-origin
and background-clip
. If it is included twice, the first occurrence sets background-origin
, and the second sets background-clip
.<background-color>
value may only be included in the last layer specified.<attachment>
<box>
See background-clip
and background-origin
.
<background-color>
See background-color
.
<bg-image>
See background-image
.
<position>
See background-position
.
<repeat-style>
See background-repeat
.
<bg-size>
See background-size
.
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 | as each of the properties of the shorthand:
|
---|---|
Applies to | all elements. It also applies to ::first-letter and ::first-line . |
Inherited | no |
Percentages | as each of the properties of the shorthand:
|
Computed value | as each of the properties of the shorthand:
|
Animation type | as each of the properties of the shorthand:
|
background =
[ <bg-layer># , ]? <final-bg-layer>
<bg-layer> =
<bg-image> ||
<bg-position> [ / <bg-size> ]? ||
<repeat-style> ||
<attachment> ||
<box> ||
<box>
<final-bg-layer> =
<'background-color'> ||
<bg-image> ||
<bg-position> [ / <bg-size> ]? ||
<repeat-style> ||
<attachment> ||
<box> ||
<box>
<bg-image> =
<image> |
none
<bg-position> =
[ left | center | right | top | bottom | <length-percentage> ] |
[ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] |
[ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ]
<bg-size> =
[ <length-percentage [0,∞]> | auto ]{1,2} |
cover |
contain
<repeat-style> =
repeat-x |
repeat-y |
[ repeat | space | round | no-repeat ]{1,2}
<attachment> =
scroll |
fixed |
local
<box> =
border-box |
padding-box |
content-box
<image> =
<url> |
<gradient>
<length-percentage> =
<length> |
<percentage>
<p class="topbanner"> Starry sky<br/> Twinkle twinkle<br/> Starry sky </p> <p class="warning">Here is a paragraph<p>
.warning { background: pink; } .topbanner { background: url("starsolid.gif") #99f repeat-y fixed; }
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
SVG_image_as_background |
1 |
12 |
4 |
9 |
9.5 |
3.1 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
background |
1 |
12 |
1 |
4 |
3.5 |
1 |
2 |
18 |
4 |
10.1 |
1 |
1.0 |
background-clip |
21 |
12 |
22 |
9 |
15 |
5.1 |
3 |
25 |
22 |
14 |
4 |
1.5 |
background-origin |
21 |
12 |
22 |
9 |
15 |
5.1 |
3 |
25 |
22 |
14 |
4 |
1.5 |
background-size |
21 |
12 |
9 |
9 |
21 |
5.1 |
3 |
25 |
18 |
14 |
4 |
1.5 |
multiple_backgrounds |
1 |
12 |
3.6 |
9 |
10.5 |
1.3 |
2 |
18 |
4 |
14 |
1 |
1.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/background