Multiple backgrounds in CSS allow you to layer two or more background images on top of each other within a single HTML element. This powerful feature lets you create complex and visually interesting designs without needing to edit the images themselves. You can control the position, size, and repetition of each background layer independently.
Example 1: Basic Layering
/* style.css */
.multiple-backgrounds {
width: 100%;
height: 400px;
background-image: url('image1.png'), url('image2.png'); /* Layer images, the first is on top */
background-repeat: no-repeat, no-repeat; /* Control repeat for each image */
background-position: center, bottom right; /* Position each image independently */
}
Explanation: This code layers two background images. The first image (image1.png
) is placed in the center and on top, while the second image (image2.png
) is positioned at the bottom right corner underneath the first.
Example 2: Combining Images and Gradients
/* style.css */
.gradient-and-image {
width: 100%;
height: 400px;
background-image: url('pattern.svg'), linear-gradient(to right, rgba(255,0,0,0.5), rgba(0,0,255,0.5)); /* An image on top of a gradient */
background-position: top left; /* Position the image */
background-repeat: repeat; /* Repeat the pattern image */
}
Explanation: Here, a semi-transparent repeating pattern (pattern.svg
) is layered over a linear gradient. This technique is great for adding texture to colored backgrounds.
Example 3: Controlling Background Size
/* style.css */
.sized-backgrounds {
width: 100%;
height: 500px;
background-image: url('foreground.png'), url('background-sky.jpg');
background-size: 50%, cover; /* Set size for each background, first value for first image */
background-repeat: no-repeat;
background-position: bottom center;
}
Explanation: The background-size
property is used to control each layer's dimensions. The foreground image is set to 50% of the container's width, while the sky image covers the entire element.
Example 4: Using the background
Shorthand Property
/* style.css */
.shorthand-backgrounds {
width: 100%;
height: 450px;
background:
url('icon.svg') no-repeat top left / 50px 50px, /* First layer with size */
url('texture.png') repeat, /* Second layer */
#466368; /* Base color */
}
Explanation: The shorthand background
property allows you to define all properties for multiple layers in a single declaration, separated by commas. A solid background color can be set as the final layer.
Example 5: Creating a Frame Effect
/* style.css */
.frame-effect {
padding: 40px;
background-image: url('corner-tl.png'), url('corner-tr.png'), url('corner-bl.png'), url('corner-br.png');
background-repeat: no-repeat;
background-position: top left, top right, bottom left, bottom right;
background-color: #f0f0f0;
}
Explanation: This example uses four different images, one for each corner, to create a decorative frame around the element's content. The padding
ensures the content doesn't overlap the frame images.
Example 6: Animating a Background Layer
/* style.css */
@keyframes move-clouds {
from { background-position: 0 0, center; }
to { background-position: 500px 0, center; }
}
.animated-background {
height: 400px;
background-image: url('clouds.png'), url('sky.jpg');
background-repeat: repeat-x, no-repeat;
background-size: auto, cover;
animation: move-clouds 20s linear infinite;
}
Explanation: You can animate the background-position
of one layer while keeping another static. In this case, the clouds.png
layer moves horizontally, creating a parallax effect against the static sky.jpg
.
Example 7: Full Page Hero Section
/* style.css */
.hero-section {
height: 100vh;
background:
linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), /* Dark overlay for text readability */
url('hero-image.jpg') no-repeat center center / cover; /* High-quality hero image */
color: white;
display: flex;
align-items: center;
justify-content: center;
}
Explanation: This technique is common for hero sections. A semi-transparent dark gradient is layered over a full-screen background image (cover
size) to ensure that any text on top is legible.