What is background-image
?
The background-image
property sets one or more background images on an element. It's commonly used for hero sections, patterns, and full-page backgrounds without extra markup.
selector {
background-image: url("image.jpg");
}
Applying a Background Image
You can apply background images to any block-level element — typically <body>
, <section>
or <div>
.
body {
background-image: url("background.jpg");
}
Common Background Properties
Control how the image appears using related properties:
background-repeat
—repeat
,no-repeat
,repeat-x
,repeat-y
background-size
—auto
,cover
,contain
, or explicit sizesbackground-position
—center
,top
,left
etc.background-attachment
—scroll
,fixed
,local
.hero-section {
background-image: url("hero-banner.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
background-attachment: fixed;
}
Multiple Background Images
CSS allows multiple, comma-separated background layers — the first image is top-most.
.container {
background-image: url("pattern.png"), url("main-bg.jpg");
background-position: top left, center;
background-repeat: repeat, no-repeat;
background-size: auto, cover;
}
Gradient as a Background Image
Gradients are images too — useful for overlays and color transitions without extra assets.
.gradient-bg {
background-image: linear-gradient(to right, #ff7e5f, #feb47b);
}
Best Practices
- Optimize images (compression, WebP) to speed up page loads.
- Use
background-size: cover
for responsive full-width backgrounds. - Combine gradients with images for depth and contrast.
- Always include a fallback
background-color
for slow connections or missing images.
Full-Page Background Example
body {
background: url("nature.jpg") no-repeat center center fixed;
background-size: cover;
}
Conclusion
The background-image
property is a simple but powerful tool. Mastering its companion properties (background-size
, background-position
, background-repeat
, and gradients) helps you create modern, responsive designs with minimal markup.