background-image
Definition
The background-image property in CSS sets an image as the background of an element. It can be used to specify a single image, a repeating image, or a gradient as the background.
Examples
Setting a single image as the background:
.single {
width: 200px;
height: 200px;
background-image: url('image.jpg');
}
Setting a repeating image as the background:
.repeating {
width: 200px;
height: 200px;
background-image: url('image.jpg');
background-repeat: repeat;
}
Setting a linear gradient as the background:
.linear {
width: 200px;
height: 200px;
background-image: linear-gradient(to right, red, yellow);
}
Setting a radial gradient as the background:
.radial {
width: 200px;
height: 200px;
background-image: radial-gradient(circle, blue, green);
}
Values
| Value | Description |
|---|---|
none | No image is used as the background. This is the default value. |
<url> | The background is an image specified by the URL of an image file. |
linear-gradient(...) | The background is a linear gradient. The gradient direction and colors are specified as arguments. |
radial-gradient(...) | The background is a radial gradient. The gradient shape and colors are specified as arguments. |
Best Practices
- Use
background-imageto specify an image, a repeating image, or a gradient as the background of an element. - The default value for
background-imageisnone, which means that no image is used as the background. - Use a relative or absolute URL to specify the location of an image file as the value for
background-image. - Use the
background-repeatproperty to specify whether the background image should repeat or not. - Use the
background-sizeproperty to specify the size of the background image. - Use the
background-positionproperty to specify the position of the background image. - Use the
background-attachmentproperty to specify whether the background image should be fixed or scroll with the rest of the page. - Use
background-imagein combination with other background properties, such asbackground-color,background-clip, andbackground-origin, to create more complex and customized backgrounds.
Browser Compatibility
| Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |