Background properties style the area behind an element's content.
| Property | Example |
|---|---|
background-color |
#f0f6ff |
background-image |
url("photo.jpg") |
background-repeat |
no-repeat, repeat-x |
background-position |
center, top right |
background-size |
cover, contain, 100px |
background-attachment |
fixed |
Shorthand
background: #eee url("bg.png") no-repeat center / cover;
Gradients
background: linear-gradient(to right, #1f386b, #4a90e2); blends colours without any image.
Example
Example — Gradient backgrounds
HTML
<style>
.box {
height: 100px;
margin-bottom: 10px;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
}
.linear { background: linear-gradient(to right, #1f386b, #4a90e2); }
.radial { background: radial-gradient(circle, #ffb347, #ff5f6d); }
</style>
<div class="box linear">Linear gradient</div>
<div class="box radial">Radial gradient</div>