The CSS background
shorthand property lets you adjust all of the available background style options at once, including color image, origin and size, repeat method, and other features. background
can be used to set the values for one or more of: background-clip
, background-color
, background-image
, background-origin
, background-position
, background-repeat
, background-size
, and background-attachment
.
/* 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");
The background
CSS shorthand property assigns explicit given values and sets missing properties to their initial values.
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:
|
Media | visual |
Computed value | as each of the properties of the shorthand:
|
Animation type | as each of the properties of the shorthand:
|
Canonical order | order of appearance in the formal grammar of the values |
The background
property is specified as one or more background layers, separated by commas.
The syntax of each layer is as follows:
<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>
background-attachment
<box>
background-clip
and background-origin
<background-color>
background-color
<bg-image>
background-image
<position>
background-position
<repeat-style>
background-repeat
<bg-size>
background-size
.[ <bg-layer> , ]* <final-bg-layer>where
<bg-layer> = <bg-image> || <position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box>{1,2}
<final-bg-layer> = <bg-image> || <position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box> || <box> || <'background-color'>where
<bg-image> = none | <image>
<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> | 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-boxwhere
<image> = <url> | <image()> | <image-set()> | <element()> | <cross-fade()> | <gradient>
<length-percentage> = <length> | <percentage>where
<image()> = image( [ [ <image> | <string> ]? , <color>? ]! )
<image-set()> = image-set( <image-set-option># )
<element()> = element( <id-selector> )
<cross-fade()> = cross-fade( <cf-mixing-image> , <cf-final-image>? )
<gradient> = <linear-gradient()> | <repeating-linear-gradient()> | <radial-gradient()> | <repeating-radial-gradient()>where
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
<image-set-option> = [ <image> | <string> ] <resolution>
<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> )where
<rgb()> = rgb( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] )
<rgba()> = rgba( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] )
<hsl()> = hsl( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] )
<hsla()> = hsla( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] )
<side-or-corner> = [ left | right ] || [ top | bottom ]
<color-stop-list> = <color-stop>#{2,}
<ending-shape> = circle | ellipse
<size> = closest-side | farthest-side | closest-corner | farthest-corner | <length> | <length-percentage>{2}where
<alpha-value> = <number> | <percentage>
<hue> = <number> | <angle>
<color-stop> = <color> <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("https://mdn.mozillademos.org/files/11983/starsolid.gif") #99f repeat-y fixed; }
Specification | Status | Comment |
---|---|---|
CSS Backgrounds and Borders Module Level 3 The definition of 'background' in that specification. | Candidate Recommendation | The shorthand property has been extended to support multiple backgrounds and the new background-size , background-origin and background-clip properties. |
CSS Level 2 (Revision 1) The definition of 'background' in that specification. | Recommendation | No significant changes |
CSS Level 1 The definition of 'background' in that specification. | Recommendation | Initial definition |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1 | 12 | 1 | 4 | 3.5 | 1 |
Multiple backgrounds | 1 | 12 | 3.6 | 9 | 10.5 | 1.3 |
SVG image as background | 31 | 12 | 9 | 9 | 21 | 5.1 |
Values of background-size longhand |
21 | 12 | 9 | 9 | 21 | 5.1 |
Values of background-origin longhand |
21 | 12 | 22 | 9 | 21 | 5.1 |
Values of background-clip longhand |
21 | 12 | 22 | 9 | 21 | 5.1 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | 2.1 | ? | Yes | 4 | 5 | 3.2 | ? |
Multiple backgrounds | 2.1 | ? | Yes | 4 | ? | 3.2 | ? |
SVG image as background | 3 | ? | Yes | 4 | Yes | 4.2 | ? |
Values of background-size longhand |
3 | ? | Yes | 18 | Yes | 4 | ? |
Values of background-origin longhand |
3 | ? | Yes | 22 | No | 4 | ? |
Values of background-clip longhand |
3 | ? | Yes | 22 | No | 4 | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/background