Home >Web Front-end >CSS Tutorial >background-image (CSS property)
This style rule assigns a background image to the element with ID "example" :
#example { background-image: ➥ url(images/bg.gif); }
To make a background image cover the entire element, you can use the ‘background-size’ property with the value ‘cover’. This will scale the image as large as possible to fill the container, while maintaining its aspect ratio. Here’s an example:
body {
background-image: url('image.jpg');
background-size: cover;
}
This will make the image cover the entire body of the webpage.
The ‘background-position’ property in CSS allows you to position a background image. You can specify the position in terms of percentages, pixels, or keywords like ‘top’, ‘bottom’, ‘left’, ‘right’, ‘center’. Here’s an example:
body {
background-image: url('image.jpg');
background-position: right top;
}
This will position the image at the top right corner of the body.
The ‘background-repeat’ property in CSS allows you to control the repetition of a background image. The values can be ‘repeat’, ‘repeat-x’, ‘repeat-y’, or ‘no-repeat’. Here’s an example:
body {
background-image: url('image.jpg');
background-repeat: repeat-x;
}
This will repeat the image horizontally across the body.
CSS allows you to set multiple background images for an element by specifying each image in the ‘background-image’ property, separated by commas. The first image in the list is the topmost layer. Here’s an example:
body {
background-image: url('image1.jpg'), url('image2.jpg');
}
This will set two background images for the body.
The ‘background-attachment’ property in CSS allows you to make a background image fixed, so it doesn’t scroll with the rest of the page. Here’s an example:
body {
background-image: url('image.jpg');
background-attachment: fixed;
}
This will make the background image fixed.
You can set a background image from an external URL by specifying the URL in the ‘background-image’ property. Here’s an example:
body {
background-image: url('https://example.com/image.jpg');
}
This will set the background image from the specified URL.
You can set a fallback color for a background image by specifying the color in the ‘background-color’ property. This color will be displayed if the background image cannot be loaded. Here’s an example:
body {
background-image: url('image.jpg');
background-color: #f00;
}
This will set a red fallback color for the background image.
The ‘background-blend-mode’ property in CSS allows you to blend a background image with a background color. Here’s an example:
body {
background-image: url('image.jpg');
background-color: #f00;
background-blend-mode: multiply;
}
This will blend the background image with the red background color using the ‘multiply’ blend mode.
You can set a background image for a specific element by specifying the element selector before the ‘background-image’ property. Here’s an example:
#myElement {
background-image: url('image.jpg');
}
This will set the background image for the element with the id ‘myElement’.
You can set a background image for a pseudo-element by specifying the pseudo-element selector before the ‘background-image’ property. Here’s an example:
div::before {
content: "";
background-image: url('image.jpg');
}
This will set the background image for the ‘::before’ pseudo-element of the ‘div’ element.
The above is the detailed content of background-image (CSS property). For more information, please follow other related articles on the PHP Chinese website!