The background-image
property in CSS is used to set an image as the background of an element. It allows you to specify one or multiple background images for an element.
š¹ Syntax
š¹ Example Usage
✅ Setting a Single Background Image
-
no-repeat
: Prevents the image from repeating. -
cover
: Makes the image cover the entire screen.
š¹ Multiple Background Images
You can apply multiple background images by separating them with a comma.
-
image1.png
appears in the top-left. -
image2.jpg
appears in the bottom-right.
š¹ Background Image with Linear Gradient
-
The gradient blends with the image.
š¹ Other Background Properties
Property | Description |
---|---|
background-repeat | repeat , no-repeat , repeat-x , repeat-y |
background-size | auto , cover , contain , 100px 100px |
background-position | top , center , left bottom , 50% 50% |
background-attachment | scroll , fixed , local |
š¹ Short-hand Property
Instead of writing multiple properties, you can use shorthand:
Order: background: image repeat position/size attachment;
šÆ Summary
-
The
background-image
property sets an image as a background. -
You can use gradients, multiple images, and control positioning.
-
The shorthand
background
property simplifies writing styles.