background
A shorthand that sets all background properties in one declaration.
- Initial value
as for the individual properties- Inherited
- No
- Animatable
- yes, like the individual properties
- Applies to
- all elements
- JavaScript
element.style.background = "#111 url(img.webp) center / cover no-repeat"
Example
#.banner {
background:
radial-gradient(circle at 80% 20%, rgba(167, 139, 250, 0.35), transparent 60%),
#0b0b12;
}<section style="padding: 24px; border-radius: 12px; background: radial-gradient(circle at 80% 20%, rgba(167,139,250,.35), transparent 60%), #0b0b12; border: 1px solid #262626;">
<strong>New world: Nordheim</strong>
<p style="margin: 6px 0 0; color: #a3a3a3;">Svelte 5 and SvelteKit with a Viking atmosphere.</p>
</section>Definition and usage
#The background property combines the colour, image, position, size, repeat, attachment and background areas. It most often looks like this: background: #111 url(img.webp) center / cover no-repeat.
The size always comes after the position, separated by a slash. The colour may appear only in the last layer, because it sits beneath all the images.
The shorthand resets omitted components to their defaults. If you set background-size earlier and then write background: red, the size goes back to auto.
Syntax
#background: <color>? <image>? <position> [ / <size> ]? <repeat>? <attachment>? <box>?;Values
#- A background colour only.
- A gradient as the only layer.
url(…) center / cover no-repeat
A photo centred and covering everything.- A gradient over a photo and a fallback colour underneath.
Values in practice
##0ea5e9
.demo { background: #0ea5e9; }linear-gradient(…)
.demo { background: linear-gradient(135deg, #10b981, #0ea5e9); }url(…) center / cover no-repeat
.demo { background: url("/collections/covers/jak-zostac-programista.webp") center / cover no-repeat; }two layers and a colour
.demo { background: linear-gradient(rgba(0,0,0,.5), rgba(0,0,0,.5)), #111 url("/collections/covers/jak-zostac-programista.webp") center / cover no-repeat; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Don’t mix the shorthand with the individual properties in different rules. The shorthand will overwrite what you set earlier.
- Remember the slash before the size:
center / cover, notcenter cover.
Related entries
#- background-colorSets the background colour of an element.
- background-imageSets an image or gradient as an element’s background.
- background-sizeSets the size of the background image: covering everything, fitting entirely, or specific dimensions.
- background-positionSets the position of the background image within the element.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.