CSS Background Image
In this page:
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.
- 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
<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.
- 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
<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
<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.
- 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
<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
<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>
- Using a wrong path in
url(), so the image does not load and no error appears on the page. - Not setting a fallback
background-color, so the text is unreadable if the image fails. - Expecting an element with no content or height to show the background image, when it has no size to display.
Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- CSS Colors
- CSS RGB Colors
- CSS HEX Colors
- CSS HSL Colors
- CSS Color Keywords
- CSS Backgrounds
- CSS Background Color
- CSS Background Image
- CSS Background Repeat
- CSS Background Attachment
- CSS Background Shorthand
- CSS Multiple Backgrounds
- CSS Background Size
- CSS Background Origin
- CSS Background Clip
- CSS Borders
- CSS Border Style
- CSS Border Width
- CSS Border Color
- CSS Border Sides
- CSS Border Shorthand