CSS Backgrounds
Every element has a background layer behind its content and padding. CSS lets you fill it with a solid color, one or more images or gradients, and control exactly how those images are sized, repeated and positioned.
Background properties
| Property | What it does | Common values |
|---|---|---|
background-color |
Solid fill color | any color, transparent |
background-image |
Image(s) or gradient(s) | url(...), linear-gradient(...), none |
background-repeat |
Tiling | repeat, no-repeat, repeat-x, repeat-y, space, round |
background-position |
Where the image starts | center, top right, 20px 40px, 50% 0 |
background-size |
Image size | auto, cover, contain, 100px 50px |
background-attachment |
Scroll behavior | scroll, fixed, local |
background-clip |
How far the background extends | border-box, padding-box, content-box, text |
background |
Shorthand for all of the above |
Background color
<!DOCTYPE html>
<html>
<head>
<style>
body { background-color: #1e2235; font-family: sans-serif; padding: 16px; }
h1 { background-color: #ffcc33; padding: 10px; }
p { background-color: rgb(255 255 255 / 0.85); padding: 10px; }
span { background-color: hotpink; color: white; }
</style>
</head>
<body>
<h1>Dark page, yellow heading</h1>
<p>A slightly see-through white paragraph with a <span>highlighted</span> word.</p>
</body>
</html>Background images
background-image accepts url(...) for image files and gradient functions, which generate an image on the fly. Since examples here cannot load external files, we use an inline SVG as a data URI — the image data is written directly into the URL.
By default an image repeats in both directions to fill the element:
<!DOCTYPE html>
<html>
<head>
<style>
.tiles {
height: 120px;
margin-bottom: 10px;
border: 2px solid #333;
background-color: #eef3ff;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Ccircle cx='20' cy='20' r='10' fill='%23264de4'/%3E%3C/svg%3E");
}
.x { background-repeat: repeat-x; }
.none { background-repeat: no-repeat; background-position: center; }
</style>
</head>
<body>
<div class="tiles"></div>
<div class="tiles x"></div>
</body>
</html>Change x to none on the second div to see a single centered dot.
Size and position
background-size: cover scales the image so it covers the whole element (some of it may be cropped). contain scales it to fit entirely inside (there may be empty space). background-position decides which part stays visible.
<!DOCTYPE html>
<html>
<head>
<style>
body { display: flex; gap: 10px; font: 14px sans-serif; }
.pic {
width: 150px; height: 150px;
border: 2px solid #444;
background-color: #fdf1d6;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='100'%3E%3Crect width='200' height='100' fill='%2387ceeb'/%3E%3Ccircle cx='160' cy='30' r='18' fill='gold'/%3E%3Cpath d='M0 100 L60 40 L110 100 Z' fill='%232e8b57'/%3E%3Cpath d='M70 100 L130 50 L200 100 Z' fill='%23226b44'/%3E%3C/svg%3E");
background-repeat: no-repeat;
display: flex; align-items: end; justify-content: center;
}
.cover { background-size: cover; background-position: center; }
.contain { background-size: contain; background-position: center; }
.right { background-size: cover; background-position: right; }
</style>
</head>
<body>
<div class="pic cover">cover</div>
<div class="pic contain">contain</div>
<div class="pic right">cover + right</div>
</body>
</html>Multiple backgrounds
You can stack several images separated by commas. The first one listed is drawn on top. A background-color always sits at the very bottom.
<!DOCTYPE html>
<html>
<head>
<style>
.hero {
height: 220px;
border-radius: 12px;
color: white;
font: bold 32px sans-serif;
display: grid; place-items: center;
background:
radial-gradient(circle at 80% 20%, rgb(255 255 255 / 0.5) 0 30px, transparent 31px),
linear-gradient(135deg, #264de4, #9b2fae);
}
</style>
</head>
<body>
<div class="hero">Layered backgrounds</div>
</body>
</html>The background shorthand
The shorthand lets you set everything in one line. Values can appear in almost any order, but size must come right after position, separated by a slash:
.banner {
background: #222 url("stars.svg") no-repeat center / cover;
}Any sub-property you leave out is reset to its default. That is a common surprise: writing background: red; after background-image: ... removes the image.
Fixed backgrounds
background-attachment: fixed pins the background to the viewport, so content scrolls over it — the classic "parallax" look. On many mobile browsers it is ignored, so treat it as an enhancement, not a requirement.
Tip: Always set a
background-coloralongside an image. If the image fails to load, or while it is loading, text will still be readable.
Test yourself
What is the default value of background-repeat?
no-repeatrepeatrepeat-xround
Which background-size value makes the image fill the whole element, cropping if needed?
containautocoverfill
With multiple background images, which one is drawn on top?
- The last one listed
- The first one listed
- The largest one
- The background-color