CSS backgrounds allow you to style the behind-area of any HTML element. You can add solid colors, images, gradients, patterns, or even create layered backgrounds. Background properties help enhance design, readability, and the overall visual appeal of your website.
The background-color property sets a solid color behind an element.
box {
background-color: lightblue;
}
You can add an image behind an element using:
box {
background-image: url("image.jpg");
}
This image can be repeated, positioned, or sized based on your needs.
Controls whether the image repeats:
box {
background-image: url("pattern.png");
background-repeat: no-repeat;
}
Used to control image placement:
box {
background-position: center top;
}
Controls the display size of background images.
box {
background-size: cover;
}
Defines scrolling behavior:
box {
background-attachment: fixed;
}
You can combine all background properties:
box {
background: url("bg.jpg") no-repeat center/cover fixed;
}
Order doesn’t have to be exact, but this format is commonly used.
CSS supports layered backgrounds:
box {
background:
url("overlay.png") no-repeat center,
url("main-bg.jpg") center/cover;
}