CSS: CSS Background

🗓 Last updated: 2026-11-30👤 Written by: Casey✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

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

Properties in this section: background-color background-image background-position background-size background-attachment background height

1. Background Color

background-color sets the element's background color, supporting any valid color value:

▶ Example

▶ Try it Yourself

2. Background Image

background-image sets a background image for elements:

▶ Example

▶ Try it Yourself

Multiple background images can be set, separated by commas, with earlier ones on top:

▶ Example

▶ Try it Yourself

(1) Background Image vs <img> Tag


3. Background Repeat

Controls whether background images repeat/tile:

▶ Example

▶ Try it Yourself

4. Background Size

Controls background image dimensions:

▶ Example

▶ Try it Yourself

5. Background Position

Controls background image position within element:

▶ Example

▶ Try it Yourself

6. Background Shorthand

The background shorthand combines all background properties:

▶ Example

▶ Try it Yourself

Properties in this section: background-color background-image background-position background-size background-attachment background height

❓ 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

📝 Exercises

  1. Create a full-width banner using background shorthand with centered, non-repeating image.
  2. Create a parallax effect using background-attachment: fixed with at least two sections.
  3. Create a multi-background overlay: gradient mask + base image on the same element.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏