This chapter covers the addition of multiple background images to a single element, alongside explanations of the following properties:
In CSS, you can incorporate multiple background images for an element using the background-image property. These images are listed separated by commas, and they are layered atop one another, with the first image closest to the viewer.
Consider the following example featuring two background images: The initial image showcases a flower, positioned at the bottom-right, while the subsequent image displays a paper background, aligned to the top-left corner.
Example
#example1 { |
You can specify multiple background images using either the individual background properties, as demonstrated above, or the background shorthand property.
The subsequent example employs the background shorthand property, yielding the same outcome as the previous example.
Example
#example1 { background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat; } |
The CSS background-size property enables you to define the dimensions of background images, which can be specified using lengths, percentages, or keywords such as “contain” or “cover”.
In the subsequent example, the background image is resized to a considerably smaller size compared to the original image, utilizing pixels.
Below is the provided code snippet:
Example
#div1 { background: url(img_flower.jpg); background-size: 100px 80px; background-repeat: no-repeat; } |
The background-size property also accepts two additional values: contain and cover.
When “contain” is used, the background image is scaled to fit the content area as large as possible while ensuring both its width and height fit within the element. Consequently, depending on the image’s proportions and the container’s dimensions, there may be uncovered areas.
On the other hand, “cover” scales the background image so that it completely covers the content area, ensuring both its width and height exceed or equal those of the container. Consequently, portions of the background image may not be visible within the container.
The subsequent example demonstrates the application of “contain” and “cover“.
Example
#div1 { background: url(img_flower.jpg); background-size: contain; background-repeat: no-repeat; } #div2 { background: url(img_flower.jpg); background-size: cover; background-repeat: no-repeat; } |
When dealing with multiple backgrounds, the background-size property allows for the specification of multiple values (separated by commas) to define the size of each background individually.
In the following example, three background images are designated, each with a distinct background-size value.
Example
#example1 { background: url(img_tree.gif) left top no-repeat, url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat; background-size: 50px, 130px, auto; } |
Here’s how to achieve a full-page background image on a website, meeting the specified requirements:
To implement this, utilize the <html> element, which inherently spans at least the height of the browser window. Apply a fixed and centered background to the <html> element, adjusting its size using the background-size property.
Example
html { background: url(img_man.jpg) no-repeat center fixed; background-size: cover; } |
Alternatively, you can employ various background properties on a <div> element to craft a hero image, which typically features a large image overlaid with text, and position it as desired on the webpage.
Example
.hero-image { background: url(img_man.jpg) no-repeat center; background-size: cover; height: 500px; position: relative; } |
The CSS background-origin property determines the starting position of the background image. It offers three distinct values:
The subsequent example demonstrates the application of the background-origin property.
Example
#example1 { border: 10px solid black; padding: 35px; background: url(img_flwr.gif); background-repeat: no-repeat; background-origin: content-box; } |
The CSS background-clip property defines the area where the background is displayed.
It offers three options:
The following example demonstrates the usage of the background-clip property.
Example
#example1 { border: 10px dotted black; padding: 35px; background: yellow; background-clip: content-box; } |