background
==========

The **`background`** shorthand CSS property sets all background style properties at once, such as color, image, origin and size, or repeat method. Component properties not set in the `background` shorthand property value declaration are set to their default values.

Try it
------

Constituent properties
----------------------

This property is a shorthand for the following CSS properties:

* `background-attachment`
* `background-clip`
* `background-color`
* `background-image`
* `background-origin`
* `background-position`
* `background-repeat`
* `background-size`
Syntax
------


```css
/\* 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:

* Each layer may include zero or one occurrences of any of the following values:
    + `<attachment>`
    + `<bg-image>`
    + `<position>`
    + `<bg-size>`
    + `<repeat-style>`
* The `<bg-size>` value may only be included immediately after `<position>`, separated with the '/' character, like this: "`center/80%`".
* The `<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`.
* The `<background-color>` value may only be included in the last layer specified.
### Values

`<attachment>`

See `background-attachment`. Default: `scroll`.

`<box>`

See `background-clip` and `background-origin`. Default: `border-box` and `padding-box` respectively.

`<background-color>`

See `background-color`. Default: `transparent`.

`<bg-image>`

See `background-image`. Default: `none`.

`<position>`

See `background-position`. Default: 0% 0%.

`<repeat-style>`

See `background-repeat`. Default: `repeat`.

`<bg-size>`

See `background-size`. Default: `auto`.

The following three lines of CSS are equivalent:


```css
background: none;
background: transparent;
background: repeat scroll 0% 0% / auto padding-box border-box none transparent;
```

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.

* MDN Understanding WCAG, Guideline 1.1 explanations
* Understanding Success Criterion 1.1.1 | W3C Understanding WCAG 2.0
Formal definition
-----------------

|  |  |
| --- | --- |
| Initial value | as each of the properties of the shorthand:* `background-image`: `none`
* `background-position`: `0% 0%`
* `background-size`: `auto auto`
* `background-repeat`: `repeat`
* `background-origin`: `padding-box`
* `background-clip`: `border-box`
* `background-attachment`: `scroll`
* `background-color`: `transparent`
 |
| Applies to | all elements. It also applies to `::first-letter` and `::first-line`. |
| Inherited | no |
| Percentages | as each of the properties of the shorthand:* `background-position`: refer to the size of the background positioning area minus size of background image; size refers to the width for horizontal offsets and to the height for vertical offsets
* `background-size`: relative to the background positioning area
 |
| Computed value | as each of the properties of the shorthand:* `background-image`: as specified, but with `url()` values made absolute
* `background-position`: as each of the properties of the shorthand:
    + `background-position-x`: A list, each item consisting of: an offset given as a combination of an absolute length and a percentage, plus an origin keyword
    + `background-position-y`: A list, each item consisting of: an offset given as a combination of an absolute length and a percentage, plus an origin keyword
* `background-size`: as specified, but with relative lengths converted into absolute lengths
* `background-repeat`: a list, each item consisting of two keywords, one per dimension
* `background-origin`: as specified
* `background-clip`: as specified
* `background-attachment`: as specified
* `background-color`: computed color
 |
| Animation type | as each of the properties of the shorthand:* `background-color`: a color
* `background-image`: discrete
* `background-clip`: a repeatable list
* `background-position`: a repeatable list
* `background-size`: a repeatable list
* `background-repeat`: discrete
* `background-attachment`: discrete
 |

Formal syntax
-------------
```

background =   
  <bg-layer>#? , <final-bg-layer>    
  
<bg-layer> =   
  <bg-image>                      ||  
  <bg-position> [ / <bg-size> ]?  ||  
  <repeat-style>                  ||  
  <attachment>                    ||  
  <visual-box>                    ||  
  <visual-box>                      
  
<final-bg-layer> =   
  <bg-image>                      ||  
  <bg-position> [ / <bg-size> ]?  ||  
  <repeat-style>                  ||  
  <attachment>                    ||  
  <visual-box>                    ||  
  <visual-box>                    ||  
  <'background-color'>              
  
<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     
  
<visual-box> =   
  content-box  |  
  padding-box  |  
  border-box     
  
<image> =   
  <url>       |  
  <gradient>    
  
<length-percentage> =   
  <length>      |  
  <percentage>    
  
<url> =   
  <url()>  |  
  <src()>    
  
<url()> =   
  url( <string> <url-modifier>\* )  |  
  <url-token>                        
  
<src()> =   
  src( <string> <url-modifier>\* )    
  
```

Examples
--------

### Setting backgrounds with color keywords and images

#### HTML


```html
<p class="topbanner">
  Starry sky<br />
  Twinkle twinkle<br />
  Starry sky
</p>
<p class="warning">Here is a paragraph</p>
<p></p>
```


#### CSS


```css
.warning {
  background: pink;
}

.topbanner {
  background: url("starsolid.gif") #99f repeat-y fixed;
}
```


#### Result

Specifications
--------------

| Specification |
| --- |
| CSS Backgrounds and Borders Module Level 3 # the-background |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `box-decoration-break`
* Using gradients
* Using multiple backgrounds