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"
On this page

Example

#
CSS
.banner {
  background:
    radial-gradient(circle at 80% 20%, rgba(167, 139, 250, 0.35), transparent 60%),
    #0b0b12;
}
Preview

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

#
Syntax
background: <color>? <image>? <position> [ / <size> ]? <repeat>? <attachment>? <box>?;

Values

#

Values in practice

#

#0ea5e9

A background colour only.
.demo { background: #0ea5e9; }

linear-gradient(…)

A gradient as the only layer.
.demo { background: linear-gradient(135deg, #10b981, #0ea5e9); }

url(…) center / cover no-repeat

A photo centred and covering everything.
.demo { background: url("/collections/covers/jak-zostac-programista.webp") center / cover no-repeat; }

two layers and a colour

A gradient over a photo and a fallback colour underneath.
.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, not center cover.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.