CSS: CSS Background
Background styling is an important tool for beautifying web pages. CSS provides a series of background properties that let you control element background colors, background images, image repetition, size, and position.
Background Properties Quick Reference
| Property | Common Values | Purpose |
|---|---|---|
background-color |
#f5f5f5, red, rgba(255,0,0,0.3) |
Background color |
background-image |
url("bg.jpg"), linear-gradient(...) |
Background image or gradient |
background-repeat |
no-repeat, repeat, repeat-x, repeat-y |
Background repeat method |
background-position |
center, top left, 50% 50% |
Background image position |
background-size |
cover, contain, 100% auto |
Background image size |
background-attachment |
scroll, fixed, local |
Whether background scrolls with page |
background |
#333 url("bg.jpg") no-repeat center |
Shorthand, combines multiple |
1. Background Color
background-color sets the element's background color, supporting any valid color value:
▶ Example
▶ Try it Yourself2. Background Image
background-image sets a background image for elements:
▶ Example
▶ Try it YourselfMultiple background images can be set, separated by commas, with earlier ones on top:
▶ Example
▶ Try it Yourself(1) Background Image vs <img> Tag
- Background image: Suitable for decorative images, doesn't affect content semantics
<img>tag: Suitable for content-meaningful images (product photos, article illustrations), SEO and accessibility friendly
3. Background Repeat
Controls whether background images repeat/tile:
▶ Example
▶ Try it Yourself4. Background Size
Controls background image dimensions:
▶ Example
▶ Try it Yourselfcover: Fills container maintaining aspect ratio, excess cropped (most common)contain: Shows complete image maintaining aspect ratio, may have whitespace
5. Background Position
Controls background image position within element:
▶ Example
▶ Try it Yourself6. Background Shorthand
The background shorthand combines all background properties:
▶ Example
▶ Try it Yourself❓ FAQ
Q Why is my background-image not showing up?
A Three most common causes: wrong image path (relative paths resolve against the CSS file's directory, not the page), the element has height 0 (background images don't expand their container — set an explicit
height), or mismatched quotes and filename casing. Debug in order: check Network panel for a 404 on the image, check the element's actual height in Elements, then verify the path.Q What's the difference between
background-size: cover and contain?A
cover scales the image proportionally to fill the entire container, cropping the overflow; contain shows the complete image, leaving whitespace where it doesn't fit. Use cover when the background must fill with no gaps, contain when the whole image must stay visible.Q What's the order of values in the
background shorthand?A color image repeat attachment position, e.g.
background: #fff url(bg.jpg) no-repeat fixed center;. Omitted values fall back to defaults — but the shorthand resets anything you leave out, so background: url(x.jpg) alone will wipe out an earlier background-color.Q How do I make a background image fill the whole screen without distortion?
A Combine
background: url(bg.jpg) no-repeat center center / cover; with min-height: 100vh. cover scales proportionally to fill the viewport and center spreads the cropping evenly. In the shorthand, size comes after a slash following position — writing center cover directly fails to parse.Q How do I set a semi-transparent background color? rgba vs opacity?
A Use a color value with an alpha channel:
background-color: rgba(0, 0, 0, 0.5) is a 50% black overlay. The difference is scope — rgba makes only the background transparent while text stays opaque; opacity makes the whole element including its text transparent. For overlay layers always use rgba.Q Can I stack multiple background images on one element?
A Yes, separate groups with commas; earlier ones render on top:
background: linear-gradient(rgba(0,0,0,.4), rgba(0,0,0,.4)), url(bg.jpg); — the classic "gradient overlay darkens a photo" trick used in almost every banner or hero section. size, repeat and position can also be comma-separated per layer.Q My background image keeps tiling — how do I stop the repetition?
A Add
background-repeat: no-repeat. The default repeat tiles infinitely in both directions, very visible when the container is bigger than the image. repeat-x (horizontal only, good for divider textures) and repeat-y (vertical) also exist. Once cover fills the container, repeat is no longer needed.Q Why doesn't background-attachment: fixed work on mobile?
A iOS Safari and most mobile browsers don't support fixed for performance reasons. On desktop, fixed creates the "background stays put while content scrolls" parallax effect; on mobile it silently degrades to the default scroll. Alternative: host the background in a container with
position: fixed. Confirm target devices before committing to parallax.Q When should I use a background image instead of an img tag?
A Decide by whether the image carries content: product shots, article illustrations and anything search engines should index belong in
<img> (has alt, indexable); purely decorative banners, textures and overlays belong in the background (no semantics, no content flow). Ask: if the image disappeared, would understanding the page suffer? If yes use img, otherwise background.📖 Summary
- CSS background properties are rich — you can set solid colors, image backgrounds, control repetition, size, position, and scroll behavior
background-size: cover+background-position: centeris the most common combination for responsive backgrounds- Use multiple backgrounds with commas for overlay effects
- Background shorthand combines all properties in one declaration
📝 Exercises
- Create a full-width banner using
backgroundshorthand with centered, non-repeating image. - Create a parallax effect using
background-attachment: fixedwith at least two sections. - Create a multi-background overlay: gradient mask + base image on the same element.
🧩 Related Examples
Night-Sky 404 SceneA full-page night-sky gradient: a pure-CSS moon with halo, stars blinking on offset delays, and two mountain…
Geometric Minimal 404Three pure-CSS shapes — circle, triangle, square — plus an outlined 404, one line of copy and a button. The…
Marker HighlightKey phrases get a slanted gradient band like a marker stroke, expanding or deepening on hover. The trick for…