To set a background color and image in CSS, use background-color for a solid fill and background-image: url(...) for a picture, or combine both in the background shorthand. The color is painted first and the image is layered on top, so the color acts as a fallback while the image loads or if it fails. This guide covers the css background image basics every page needs.
You will also learn how to layer several images, how the cascade treats the shorthand, and how to keep text readable on top of a picture.
background-color fills the area behind an element's content and padding, and it extends under the border by default. It accepts any CSS color value: a named color, hex, rgb(), hsl(), or a modern space such as oklch().
The default value is transparent, so an element shows whatever is behind it until you set a color. The last rule uses an alpha channel, which makes the fill semi-transparent.
background-image takes one or more image sources. The most common is url() pointing to a file, but gradients are also valid image values.
By default the image is placed at the top-left corner of the padding box and tiles in both directions if it is smaller than the element. If the file cannot be loaded, the browser shows the background-color instead, which is why you should always set both.
Background images are decorative. They have no alternative text, so use an img element in your HTML for content that carries meaning, such as a product photo.
The background shorthand sets color, image, position, size, repeat, attachment and more in one declaration.
Here center / cover means position center and size cover. The slash is required between position and size. The color may only appear in the last layer when you use several layers.
A shorthand resets every background sub-property you leave out to its initial value. If an earlier rule set background-size: cover and a later rule uses background: url(...) without a size, the size is reset to auto. This is a common source of "my image suddenly stopped covering" bugs.
| Property | Purpose | Default |
|---|---|---|
background-color | Solid fill | transparent |
background-image | One or more images or gradients | none |
background-repeat | Tiling behavior | repeat |
background-position | Where the image starts | 0% 0% |
background-size | Image dimensions | auto |
background-attachment | Scroll or fixed behavior | scroll |
You can list several images separated by commas. The first one is drawn on top and the last one at the bottom, with the color behind them all.
The first layer is a gradient that works as a dark tint over the photo. This is a simple way to make white text readable on a bright picture without editing the file. Other properties such as background-size accept a comma-separated list too, one value per layer; a single value is repeated for all layers.
The section has a fallback color, a tinted photo, and is centered with grid. The min-height matters: an element with no content and no height has nothing to paint a background on.
url() paths in a stylesheet are resolved relative to the CSS file, not the HTML page.background-color only sets the solid fill. background is a shorthand that can set the color, image, position, size, repeat and attachment together, and it resets any part you do not mention.
Yes. The color is painted underneath the image, so it shows through transparent areas of the image and acts as a fallback if the image does not load.
Usually the element has no height, the url() path is wrong relative to the stylesheet, or a later shorthand overrode your background-image. Check the Network tab and the computed styles in DevTools.
Use CSS backgrounds for decoration and img for meaningful content that needs alt text, lazy loading or srcset.
Set background-color first as a fallback, add background-image: url(...) for pictures or gradients, and use the background shorthand when you want everything in one place, remembering that it resets omitted values. Layer images with commas, add a tint for readable text, and keep decorative images small and in modern formats.