CSS3 Backgrounds
Background properties paint the area behind an element's content and padding. With them you can set a solid colour, place one or more images, control how images repeat and scale, and decide whether they scroll with the page. They are used for hero sections, card textures, decorative patterns and icons beside text.
The individual properties
background-colorfills the box with a colour and sits underneath any image.background-imagetakesurl(...)or a gradient.background-repeatchoosesrepeat,no-repeat,repeat-x,repeat-y,spaceorround.background-positionplaces the image, using keywords likecenteror values likeright 20px top 10px.background-sizesets the image size;coverandcontainare the two most used keywords.background-attachmentdecides whether the image scrolls with the element or stays fixed.background-originandbackground-clipdecide whether painting starts at the border, padding or content edge.
Cover versus contain
cover scales the image until it fills the whole box, cropping whatever spills over. It is right for decorative photos where filling the area matters more than seeing every pixel. contain scales the image so all of it fits, which may leave empty bands at the sides. Use it for logos or diagrams where cropping would lose meaning.
Layering several backgrounds
CSS3 allows a comma-separated list of backgrounds on one element. The first layer in the list is painted on top. A common trick is placing a semi-transparent gradient over a photo so white text stays readable.
.hero {
min-height: 60vh;
color: white;
background:
linear-gradient(rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.2)),
url("images/city-night.jpg") center / cover no-repeat;
background-color: #1d2533; /* shown while the image loads */
}
.search-input {
padding-left: 36px;
background: url("icons/search.svg") no-repeat 10px center / 18px 18px;
}
Notice the shorthand order: position, then a slash, then size. The slash is required when you include a size in the background shorthand.
For high-resolution screens, the image-set() function lets the browser choose between versions of a background image, such as a normal and a double-density file. That keeps photos sharp on phones without forcing every visitor to download the largest file.
Shorthand resets and other gotchas
The background shorthand resets every background property you do not mention. If you set background-color first and then write background: url(...) later in the same rule, the colour returns to transparent. Either put all values in the shorthand or use only the individual properties.
Relative image paths are resolved from the stylesheet's location, not from the HTML page. A path that works in an inline style may break once moved into a CSS file in another folder.
Also, a background image is decoration only. Screen readers do not announce it and it has no alt text, so meaningful pictures such as product photos belong in an img element. Finally, background-attachment: fixed is ignored or janky on many mobile browsers, so do not rely on it for important effects.
Keep in mind
Always pair a background image with a fallback colour that keeps text readable, use cover for photos and contain for logos, and remember that the first layer listed is the one on top.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.