CSS Backgrounds
CSS Backgrounds: Colors, Images, Position, and Size
CSS backgrounds paint the area behind an element's content and padding, and they are how you build everything from a flat dark card to a full-width hero banner. Getting a few properties right (color, image, repeat, position, and size) keeps your design sharp on any screen shape.
The Core CSS Background Properties
Each property controls one part of how a background looks:
background-colorfills the element with a solid or semi-transparent color. Set one even when you use an image, so the element still looks right while the image loads or if it fails.background-imageaccepts eitherurl()for an image file or a gradient such aslinear-gradient().background-repeatcontrols tiling. Images repeat by default, so useno-repeatfor banners and logos.background-positiondecides which part of the image stays in view. It accepts keywords (center, top right) or values like50%30%.background-sizescales the image.coverfills the wholecontainerand crops any overflow, while contain shows the entire image and may leave space.
Example: A Hero Banner With a Fallback Color
.hero-banner {
background-color: #0f172a;
background-image:
linear-gradient(rgba(15, 23, 42, 0.65), rgba(15, 23, 42, 0.85)),
url("/images/hero-workspace.webp");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
color: #f8fafc;
padding: 64px 24px;
border-radius: 12px;
}To check that it works, temporarily point the url() at a file that doesn't exist. You should still see the dark #0f172afill, which confirms the fallback color is doing its job.
Layering Multiple Backgrounds
background-image accepts several comma-separated layers. The first layer sits on top, so in the example above the translucent gradient covers the photo. This dark overlay keeps light headline text readable even where the photo has bright patches. Adjust the rgbaalpha values to make the overlay lighter or heavier.
The CSS Background Shorthand
You can combine most of these properties in one declaration:
.card {
background: #0f172a url("/images/bg.webp") center / cover no-repeat;
}Note the slash between position and size. center/cover is valid, but size can't appear without position before it. The shorthand also resets any background property you leave out, so a laterbackground rule can silently wipe out an earlier background-size. When you only need to change one thing, use the individual property instead.
Common Mistakes to Avoid
Using
background-attachment: fixed on mobile. Fixed backgrounds can jitter or not work at all on iOS Safari. Keep the defaultscrollfor small screens, and applyfixedonly inside a desktop media query if you want a parallax effect.Using a CSS background for meaningful images. Search engines and screen readers can't read CSS backgrounds. For product photos, diagrams, or anything that carries information, use an HTML
<img>with descriptivealttext. Keep CSS backgrounds for decoration.Getting image paths wrong in external CSS. A relative path inside a stylesheet resolves from the CSS file's location, not from the HTML page. A path in
/css/app.cssis relative to the/css/folder. A path starting with/, like /images/hero.webp, is resolved from the site root, which is usually the safer choice.
Frequently Asked Questions
What is the difference between background-size: cover and contain?
cover scales the image until it fills the container edge to edge, cropping whatever overflows. contain scales it until the whole image fits inside, which can leave space on the sides or top and bottom.
Why does my background image not show up?
The usual causes are a wrong file path, a container with no height (an empty element collapses to zero), or a typo in the filename. Check the browser's Network tab for a failed image request.
Should I use a CSS background or an <img> tag?
Use an <img> tag when the image is part of the content and needs alt text. Use a CSS background for decoration such as patterns, gradients, and hero overlays.
Can I put text over a background image and keep it readable?
Yes. Layer a semi-transparent gradient over the image, as shown in the hero example, and make sure the text color has enough contrast against it.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.