CSS: CSS Background

Last updated: 2026-10-13

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

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

❓ FAQ

Q What's the difference between background-size: cover and contain?
A cover fills the entire container maintaining aspect ratio, cropping excess parts. contain shows the complete image within the container, may leave whitespace. Use cover for full-bleed backgrounds, contain when you need to show the entire image.
Q Can I use multiple background images?
A Yes! Separate multiple backgrounds with commas. Earlier ones appear on top. Example: background: url("overlay.png"), url("bg.jpg");. Often combined with gradients for overlay effects.
Q What's the difference between background-attachment: fixed and scroll?
A fixed keeps the background stationary when scrolling (parallax effect), scroll moves with the page (default). Fixed creates depth perception but may affect mobile performance.

📖 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.

100%

🙏 帮我们做得更好

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

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