CSS Reference ยท Colors and Backgrounds
The background property
Reference for the CSS background shorthand: layer order, the position/size slash, the single box value for origin and clip, resetting behavior and live examples.
The background property is a shorthand that sets everything about an element's background in one declaration: the color, one or more images, their position, size, repeating, attachment, and the boxes used for positioning and painting.
Because it is a shorthand, any longhand you leave out is reset to its initial value. That makes background convenient for a fresh start and dangerous for tweaking: writing background: red later in the cascade also removes an image set earlier.
.hero { background: #0f172a url("/img/stars.png") center / cover no-repeat; }
Try it
Syntax
background: [ <bg-layer> , ]* <final-bg-layer>; /* A layer */ <bg-layer> = <bg-image> || <bg-position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box> || <box> /* Only the last layer may contain a color */ <final-bg-layer> = <bg-layer> || <background-color>
- Initial value: each longhand's initial value (
transparent none repeat scroll 0% 0% / auto padding-box border-box) - Applies to: all elements
- Inherited: no
- Animatable: per longhand (colors, positions and sizes animate; images do not)
- Computed value: see the individual longhands
Values
| Part | Description |
|---|---|
background-color |
A color painted under all image layers. Allowed only in the last layer. |
background-image |
none, url(), a gradient and so on. |
background-position |
Placed after the image. If you add a size, write it after a slash: center / cover. |
background-size |
Only valid right after a position and a /. |
background-repeat |
repeat, no-repeat, space, round, repeat-x, repeat-y. |
background-attachment |
scroll, fixed, local. |
background-origin and background-clip |
Boxes. With one box value, it sets both; with two, the first is the origin and the second the clip. |
Usage notes
- Layers are comma-separated, topmost first. The first image is painted on top and the color is painted underneath all of them.
- The color goes in the last layer only. A color in an earlier layer makes the whole declaration invalid.
- Always write the slash.
background: center coveris invalid;background: center / coveris right. Size without position is also invalid. - Reset surprises. Use longhands (
background-color,background-image) when you only want to change one aspect, for example in a hover rule or a theme override. - Provide a fallback color. If an image fails to load or text sits on it, the color keeps the text readable.
- Contrast over images. Text over a photo needs a check at the lightest and darkest spots, or an overlay gradient.
- Decorative vs content. Background images are not part of the accessibility tree and cannot have alt text. Use
<img>for meaningful pictures.
Browser support
Supported in every browser. Individual features such as multiple layers, gradients and the single-box origin/clip form are available in all current browsers.