CSS Backgrounds
CSS Backgrounds are properties that control the background layer of elements, including background-color, background-image, background-position, background-size, background-repeat, and background-attachment, allowing complex layered background effects.
How do you set a background image in CSS?
Use background-image: url("photo.jpg"), then background-size: cover to fill the element without distortion and background-position: center to centre it. Add background-repeat: no-repeat so the image doesn't tile. You can layer several backgrounds, separated by commas.
.hero {
background-image: url("/hero.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
/* Shorthand with a dark overlay */
.hero {
background:
linear-gradient(rgb(0 0 0 / .5), rgb(0 0 0 / .5)),
url("/hero.jpg") center / cover no-repeat;
}Background Types
Colors
- • Solid colors
- • Transparent
- • RGBA values
Images
- • Photos
- • Patterns
- • SVG graphics
Gradients
- • Linear
- • Radial
- • Conic
Multiple
- • Layered
- • Blended
- • Complex
CSS Backgrounds Showcase
Explore comprehensive background techniques including colors, images, gradients, positioning, and advanced effects. Click cards to change the page background!
Code Editor
<div class="hero">Background image with overlay</div>
<div class="pattern">Repeating gradient pattern</div>Preview
Background Properties
Essential Properties
background-colorSets the background colorbackground-imageAdds background imagesbackground-sizeControls image sizingbackground-positionSets image positionAdvanced Properties
background-repeatControls image repetitionbackground-attachmentFixed or scroll behaviorbackground-clipDefines painting areabackground-originSets positioning areaBackground Shorthand Property
The shorthand property combines all background properties in a single declaration.
Your Learning Path
Master CSS backgrounds and visual effects
What You've Learned
- ▶Background colors and transparency
- ▶Background images and patterns
- ▶Linear and radial gradients
- ▶Background positioning and sizing
- ▶Multiple background layers
What's Next
- ▶CSS Borders - Frame your content
- ▶CSS Margins - Control outer spacing
- ▶CSS Padding - Manage inner spacing
- ▶CSS Box Model - Master layout fundamentals
Pro Design Tip
Use multiple background layers to create rich, complex designs. Always consider performance when using large background images or complex gradients.
CSS Backgrounds — step by step
background-color and background-image
Set solid fills with background-color or use background-image with url() for images, or gradients like linear-gradient().
🎯 Quick Check
Which background-size value ensures the entire image is always visible?
CSS Backgrounds: summary
Key points
coverfills the box and may crop the image;containshows the whole image and may leave empty space.- Background images aren't content: screen readers and search engines skip them. Use
<img>withaltfor meaningful images. - Gradients are background images, so they layer with photos.
background-attachment: fixedcreates a parallax effect, but it's slow on mobile and ignored by iOS.
Frequently asked questions
›How do I make a background image cover the whole element?
Set background-size: cover; background-position: center;. The image scales to fill the element while keeping its aspect ratio.
›Why is my background image not showing?
Check the URL path (it's relative to the CSS file, not the HTML), and make sure the element has a height. An empty element with no height has nowhere to show the image.