background-image

Sets an image or gradient as an element’s background.

Initial value
none
Inherited
No
Animatable
no
Applies to
all elements
JavaScript
element.style.backgroundImage = "linear-gradient(#000, #333)"
On this page

Example

#
CSS
.hero {
  background-image:
    linear-gradient(to right, rgba(0, 0, 0, 0.8), transparent),
    url("/img/hero.webp");
  background-size: cover;
  background-position: center;
}
Preview

Definition and usage

#

The background-image property accepts an image via url() or a gradient: linear-gradient(), radial-gradient() or conic-gradient(). In CSS, gradients are images too, so they need no files.

You can provide several layers separated by commas. The first in the list sits on top. This lets you, for example, lay a semi-transparent gradient over a photo so the text on it stays readable.

A background image is decoration: it has no alternative text and screen readers skip it. Insert content that matters for understanding the page as an <img>.

Syntax

#
Syntax
background-image: none | url(…) | <gradient> [, …]*;

Values

#

Values in practice

#

none

No background image.
.demo { background-image: none; }

url("/collections/covers/jak-zostac-programista.webp")

A photo as the background.
.demo { background-image: url("/collections/covers/jak-zostac-programista.webp"); }

linear-gradient(…)

A smooth transition between colours along a line.
.demo { background-image: linear-gradient(135deg, #7c3aed, #0ea5e9); }

radial-gradient(…)

Colours radiating from the centre.
.demo { background-image: radial-gradient(circle at 30% 30%, #f59e0b, #7c3aed 70%); }

gradient + photo

A darkening gradient on top and a photo underneath.
.demo { background-image: linear-gradient(to top, rgba(0,0,0,.85), transparent), url("/collections/covers/jak-zostac-programista.webp"); }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Always protect text on a photo with a semi-transparent gradient layer. Photos have both light and dark areas.
  • For large background images use WebP or AVIF, they are much lighter than JPEG.

Related entries

#

Practise it in lessons

#

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