CSS background
- type
- shorthand property
- applies to
- any element
- inherits
- no
- common values
- a colour, url(image.jpg), a gradient, plus size / position / repeat
Where it goes
On the box that should carry the color or image: a hero section, a card, <body> for the whole page. The image sits behind the content, so keep the text readable on top of it.
Example
.hero {
background: url(hero.jpg) center / cover no-repeat;
color: white;
}
.btn {
background: linear-gradient(to right, #0ea5e9, #6366f1);
}
.card { background-color: #fff; }Common mistakes
- Writing background: red after a background-image rule and losing the image. The shorthand resets every background property you leave out. Use background-color when you only mean the color.
- Forgetting background-size: cover (or the / cover part of the shorthand). A large photo shows at its real size and repeats, so you see the top-left corner four times.
- Putting meaningful pictures in the background. A CSS background has no alt text. If the picture is content, it is an <img>. If it is decoration, background is right.