- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Background Image
CSS Colors and Backgrounds
CSS Background Image
Add image and gradient backgrounds, control their source and fallback color, and keep decorative images separate from meaningful content.
background-image puts a picture behind an element's content. It is perfect for decoration, like a hero photo behind a heading. It is wrong for images that carry information, because a background has no alt text.
You will almost always use it with a few companion properties, for size, position and repeat. A plain color is covered in CSS Background Color, and stacking several images in Layered Backgrounds.
Syntax
css
selector {
background-image: url("image.jpg");
}Example
css
body {
background-image: url("background.jpg");
}That puts the image behind the whole page.
The properties you need with it
| Property | What it controls | Example |
|---|---|---|
| background-image | The image itself | background-image: url("img.jpg"); |
| background-repeat | Whether it tiles. The default is to repeat! | background-repeat: no-repeat; |
| background-size | How big it is. cover fills the box, contain shows the whole image. | background-size: cover; |
| background-position | Which part stays visible | background-position: center; |
| background-attachment | fixed keeps it still while the page scrolls | background-attachment: fixed; |
The background shorthand sets them all at once: background: url("img.jpg") center / cover no-repeat;. The size must come straight after the position, with a slash.
Complete CSS Example for Background Image
html
<!DOCTYPE html>
<html>
<head>
<title>CSS Background Image</title>
<style>
body {
background-image: url("https://picsum.photos/500/500?grayscale");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
h1 {
color: white;
text-align: center;
}
p {
color: yellow;
text-align: center;
}
</style>
</head>
<body>
<h1>Background Image Example</h1>
<p>This page uses a background image.</p>
</body>
</html>The photo fills the whole page, centered and without repeating, and the text sits on top of it.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| background-image | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
Keep text readable
Text on a busy photo is hard to read, and the photo changes with the screen size, so the bright part may land behind your heading. Put a dark gradient over the image:
A dark overlay for readable white text
css
.hero {
background:
linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)),
url("hero.jpg") center / cover no-repeat;
background-color: #1e293b;
color: white;
}The background-color is a fallback: if the image fails to load, the white text still has a dark background behind it.
Good to know
- The image path in
url()is relative to the CSS file, not to the HTML page. This catches everyone once. - Compress large background photos, and use modern formats like WebP. A huge hero image is a common reason pages load slowly.
background-attachment: fixeddoes not work on most phones.
CSS Background Image exercises
A gradient
Give the hero a background image that runs from red on the left to blue on the right.
The HTML your code works on
<div class="hero">Welcome</div>Result
