The background-image property in CSS allows you to place an image behind any HTML element. It’s commonly used for banners, cards, hero sections, and full-page backgrounds to improve design and branding.
You can apply background-image on almost any element: the entire <body>, a specific <section>, buttons, cards, or even small inline blocks. Usually this property is written in an external CSS file, but it can also be used in a <style> block or in inline styles using the style attribute.
Background images are purely visual and do not affect the HTML structure or accessibility directly, so always make sure important information is still available as real text or HTML content, not only inside images.
Together, these properties let you control how the image appears: whether it fills the element, repeats like tiles, stays fixed while scrolling, or sits in a specific corner. You usually combine background-image with background-repeat, background-position, background-size, and sometimes background-attachment to get the exact effect you want.
/* Basic syntax for background image */
selector {
background-image: url('image.jpg');
}
The url() function can take:
url('images/bg.png')url('/assets/hero.jpg')url('https://example.com/bg.png')File names with spaces should be wrapped in quotes, for example: url("my background.png"). In real projects, you’ll often keep images in a dedicated images or assets folder to keep things organised.
Single Image
/* Single background image */
.box {
background-image: url('https://via.placeholder.com/400x200');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
Here, .box is any element (like a <div>) that will display one background image. The image will be centered inside the box, not repeated, and scaled to cover the entire area, which means some cropping may occur to keep the image’s aspect ratio intact.
.boxImagine a card or banner like:
<!-- HTML structure -->
<div class="box">
<h2>Welcome</h2>
<p>This card uses a full background image.</p>
</div>
With the above CSS, the entire .box area would be filled edge-to-edge with the image, and the text content would appear on top of the image.
Multiple Images
/* Multiple background layers */
.banner {
background-image: url('img1.png'), url('img2.png');
background-repeat: no-repeat, no-repeat;
background-position: left top, right bottom;
}
In this example, the first URL img1.png is the topmost layer, and img2.png is behind it. Properties like background-repeat and background-position accept comma-separated values, each one matching the corresponding image.
.bannerThink of a hero section where:
This box shows a centered background image using contain.
contain keeps the entire image visibleno-repeat prevents tilingcenter keeps image alignedIf you changed background-size to cover, the image would fill the entire box, possibly cropping some parts. If you removed no-repeat, the image could start tiling (repeating) horizontally and vertically depending on the element’s size.
cover for full-screen designsbackground-image for decorative graphics and use <img> tags for meaningful content that needs alt text.<body> element with background-size: cover;.background-color as a fallback for when the image fails to load.background-position values like top, bottom, left, right, and center.