# background-image

> The `background-image` CSS property sets one or more background images for an element.

## How do I add a background image in CSS?

The property that sets one or more background images on an element. The images can be URLs, gradients or other formats and they are placed behind the content of the element.

- Topic: Background
- Type: property
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 1+, Safari iOS 1+

## Example

Wrong:

```css
.hero {
  background-image: url("hero.jpg");
  color: white;
}
```

Right:

```css
.hero {
  background-color: #1a1a1a;
  background-image:
    linear-gradient(rgb(0 0 0 / 50%), rgb(0 0 0 / 50%)),
    url("hero.jpg");
  color: white;
}
```

A fallback color and a dark gradient layer keep the white text readable before and after the photo loads.

## Common mistakes

- The image path is wrong because `url()` is resolved relative to the CSS file, not the HTML page.
- The image doesn't show because the element has no height.

## Key points

- A `background-color` is set next to the image, so text stays readable while the image loads or fails.
- Images that carry meaning use `<img>` with `alt`, not `background-image`.
- Text over a photo has an overlay gradient or a solid backing that keeps enough contrast.

## Interview questions

### Can I use multiple background images on one element?

Yes. Separate them with commas; the first image is drawn on top and later ones are drawn below it.

### Should I use background-image or img for content images?

Use `<img>` for images that carry meaning, because it supports `alt` text, lazy loading and responsive sources. Use `background-image` for decoration.

### Why is my background image not showing?

Usually the `url()` path is wrong relative to the CSS file, or the element has no size because it is empty.

## Related cards

- [background](https://wonderium.dev/css/background.md)
- [background-size](https://wonderium.dev/css/background-size.md)
- [url()](https://wonderium.dev/css/url.md)
- [image-set()](https://wonderium.dev/css/image-set.md)
- [`<img>`](https://wonderium.dev/html/img.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/background-image
- Last updated: 2026-11-25
