← Back to CSS Course | Chapter 2: Colors & Backgrounds | Lesson 8 of 21

CSS Background Image

background-image places a picture behind an element's content, which is how hero banners, textured panels, and icon-only buttons get their imagery without an <img> tag.
Syntax
css
selector {
  background-image: url("image-path");
}

Setting a Background Image

background-image: url('path/to/image.jpg') loads an image and renders it behind the element's content. Unlike an <img> element, a background image is purely decorative to assistive technology and carries no alt text, so it should never be the sole way to convey meaningful content.

Note:
  • Accepted values:
  • url("image.jpg") — image file
  • linear-gradient() — gradient as an image
  • none — no image (default)
  • image-set() — resolution-aware images
  • Several comma-separated images layer on top of each other
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: Setting a Background Image

css
<style>
.box {
  background-image: url('https://placehold.co/300x150');
  height: 150px;
}
</style>
<div class="box">Purely decorative, no alt text</div>

Layering an Image Over a Color

background-color and background-image can both be set on the same element; the image renders on top of the color, so the color only shows through any transparent areas of the image or before the image finishes loading. This is a common pattern for providing a solid fallback color behind a photo.

Note:
  • Accepted values:
  • url("...") — an image file
  • none — no image (initial value)
  • linear-gradient() — a straight-line color gradient
  • radial-gradient() — a circular or elliptical gradient
  • conic-gradient() — a gradient swept around a center point
  • several values separated by commas — stacked layers
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: Layering an Image Over a Color

css
<style>
.box {
  background-color: #333;
  background-image: url('https://placehold.co/300x150');
  height: 150px;
}
</style>
<div class="box">Color shows through transparent parts or before load</div>

Stacking Multiple Images

background-image accepts a comma-separated list of url() values, stacking them with the first listed on top and the last at the bottom, similar to layers in a design tool. Each layer can have its own repeat/position/size set via matching comma-separated lists on the other background-* properties.

Example: Stacking Multiple Images

css
<style>
.box {
  background-image: url('https://placehold.co/100x100'), url('https://placehold.co/300x150');
  background-repeat: no-repeat, no-repeat;
  height: 150px;
}
</style>
<div class="box">First image layered on top of the second</div>

Gradients as Background Images

CSS gradient functions like linear-gradient() and radial-gradient() are valid background-image values, not separate properties -- meaning a gradient can be layered with a photo using the same comma-separated stacking, commonly used to darken part of a photo for readable text overlay.

Note:
  • Accepted values:
  • url("...") — an image file
  • none — no image (initial value)
  • linear-gradient() — a straight-line color gradient
  • radial-gradient() — a circular or elliptical gradient
  • conic-gradient() — a gradient swept around a center point
  • several values separated by commas — stacked layers
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: Gradients as Background Images

css
<style>
.box {
  background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url('https://placehold.co/300x150');
  height: 150px;
  color: white;
}
</style>
<div class="box">Gradient darkens the photo underneath for readable text</div>

When Background Images Fail

If a background image fails to load, the element simply falls back to showing its background-color (or transparent, if none is set) with no broken-image icon, unlike a broken <img>. This graceful degradation is one reason background images are preferred for purely decorative imagery.

Example: When Background Images Fail

css
<style>
.box {
  background-color: #ccc;
  background-image: url('missing-image.jpg');
  height: 150px;
}
</style>
<div class="box">Falls back to the background-color, no broken icon</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using a wrong path in url(), so the image does not load and no error appears on the page.
  2. Not setting a fallback background-color, so the text is unreadable if the image fails.
  3. Expecting an element with no content or height to show the background image, when it has no size to display.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.