CSS Reference ยท Colors and Backgrounds
The background-image property
Reference for CSS background-image: url(), gradients, image-set() and multiple layers, with layer order, fallbacks, accessibility notes and live examples.
The background-image property paints one or more images behind an element's content. Each image can be a file referenced with url() or a generated image such as a gradient. Images are drawn on top of background-color and below the element's content.
Several images can be listed, separated by commas. The first is the topmost layer. Other background properties, such as background-size and background-repeat, take matching comma-separated lists and apply layer by layer.
.hero { background-image: url("hero.jpg"); } .fade { background-image: linear-gradient(to bottom, transparent, black); }
Try it
Syntax
background-image: <bg-image>#; <bg-image> = none | <image> <image> = <url> | <gradient> | image-set() | cross-fade() | element() background-image: url("a.png"), linear-gradient(red, blue);
- Initial value:
none - Applies to: all elements
- Inherited: no
- Animatable: no (images switch discretely; you can animate gradient stops by animating registered custom properties, or cross-fade by layering)
- Computed value: a list of images, with
url()values made absolute
Values
| Value | Description |
|---|---|
none |
No image in this layer. |
url("...") |
An image file (PNG, JPEG, WebP, AVIF, SVG, GIF). Relative URLs resolve against the style sheet, not the page. |
linear-gradient(), radial-gradient(), conic-gradient() and repeating variants |
Images generated by the browser, with no download. |
image-set() |
Offers several resolutions or formats and lets the browser pick. |
| Comma-separated list | Several layers, topmost first. |
Usage notes
- Backgrounds are decorative. They have no alt text and are invisible to assistive technology. Use
<img>for pictures that carry meaning. - Printing. Browsers often skip backgrounds when printing unless the user enables it, so do not hide important content inside one.
- Add a background color. It shows while the image loads, if it fails, and behind transparent areas. It also keeps light text readable.
- Text over images. Add an overlay gradient (a dark-to-transparent
linear-gradientas the first layer) rather than guessing at contrast. - Layers need matching lists. If
background-sizehas fewer values than there are images, its list repeats; extra values are ignored. - Performance. Large photos are the common culprit for slow pages. Serve properly sized, compressed files, and consider
image-set()for high-density screens. Gradients and small SVGs cost almost nothing. - Quote your URLs.
url("my image.png")needs quotes when the path has spaces or special characters; data URLs should percent-encode#as%23.
Browser support
url() and the three gradient types are supported in every current browser. image-set() is supported in current browsers; check a compatibility table for older ones. element() and cross-fade() have limited support, so avoid depending on them.
Related
backgroundโ shorthandbackground-size,background-repeat,background-positionlinear-gradient(),radial-gradient()- Lesson: Backgrounds
- Lesson: Gradients