background-image accepts a url() pointing to an image file, or a CSS gradient function like linear-gradient() or radial-gradient() generating a background without needing an actual image file at all. Multiple backgrounds can be layered by separating several values with commas, with the first listed rendering on top and later ones behind it; by default, an image repeats, or tiles, in both directions to fill the element's entire box, a behavior controlled separately by background-repeat, and background-image alone doesn't automatically scale the image to fit the element, which is background-size's job instead.
1Understanding Background-image
background-image accepts a url() pointing to an image file, or a CSS gradient function like linear-gradient() or radial-gradient() generating a background without needing an actual image file at all. Multiple backgrounds can be layered by separating several values with commas, with the first listed rendering on top and later ones behind it; by default, an image repeats, or tiles, in both directions to fill the element's entire box, a behavior controlled separately by background-repeat, and background-image alone doesn't automatically scale the image to fit the element, which is background-size's job instead.
Always pair background-image with a background-color fallback for text-containing elements, so text remains legible even if the image fails to load, is still loading, or the element's background color needs to show through any transparent parts of the image.
.hero {
background-image: url("hero.jpg");
background-color: #333;
}2Practical Example
Here is a real-world application of Background-image showing how it is used in production CSS code.
.banner {
background-image: linear-gradient(to right, #4facfe, #00f2fe);
}3Best Practices
Follow these guidelines when working with Background-image:
1. Set a fallback background-color alongside background-image, so content remains legible if the image fails to load or has transparent regions
2. Combine background-image with background-size and background-position for full control over how the image fits and is positioned within the element
3. Use a CSS gradient function directly in background-image, rather than an actual image file, for simple gradients, avoiding an unnecessary image request
Tip: Always pair background-image with a background-color fallback for text-containing elements, so text remains legible even if the image fails to load, is still loading, or the element's background color needs to show through any transparent parts of the image.
.hero {
background-image: url("hero.jpg");
background-color: #333;
}