01. Background Images, Positioning, Sizing & Multiple Backgrounds

🟢 Beginner

1. Definition

CSS Background properties style element backdrops: background-image, background-repeat, background-position, background-size (cover/contain), background-attachment: fixed (parallax), and multiple comma-separated background layers.

2. Why It Is Used

Background properties create rich hero header banners, texture overlays, and parallax scrolling visual effects while decoupling images from HTML document content flow.

3. How It Works

Background images are rendered behind element padding and content layers. background-size: cover scales the image proportionally so that the background area is completely covered.

4. Syntax

.hero-card {
  background: url("hero.jpg") no-repeat center / cover;
}

5. Background Size Options Table

Size ValueBehaviorBest Practice Usage
coverScales image to cover entire container (clips edges if ratios differ)Hero headers & fullscreen cards
containScales image to fit completely inside box without clippingLogo displays & product previews
autoDisplays image in its native pixel dimensionsPattern textures

6. Beginner Example

.banner {
  background-image: url("banner.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

7. Intermediate Example

/* Multiple layered backgrounds */
.hero-layered {
  background-image:
    linear-gradient(rgba(15, 23, 42, 0.8), rgba(15, 23, 42, 0.8)),
    url("photo.jpg");
  background-size: cover;
  background-position: center;
}

8. Advanced Parallax Fixed Attachment Example

.parallax-section {
  background-image: url("landscape.jpg");
  background-attachment: fixed; /* Pins image to viewport while page scrolls */
  background-size: cover;
  background-position: center;
  min-height: 400px;
}

9. Line-by-Line Code Explanation

  • linear-gradient(rgba(...), rgba(...)), url(...): Layers a semi-transparent dark gradient overlay on top of the background image to guarantee white text contrast.

10. Expected Terminal / Browser Output

A full-width hero header section featuring a darkened background photograph with crisp, high-contrast white text overlays.

11. Common Mistakes & Pitfalls

  • Forgetting overlay gradient transparency: Placing dark text over bright background images without a semi-transparent dark gradient layer ruins text contrast.

12. Important Rules & Limitations

  • Background images do not provide alt text accessibility. Use HTML <img> tags for content images.

13. Multilingual Explanation: English

Background properties format backdrops. Use cover for hero headers and layer dark gradients over images to maintain text contrast.

14. Multilingual Explanation: Hindi (Roman Script)

Background images ke liye background-size: cover aur background-position: center ka use karein. Text contrast ke liye dark gradient overlay lagayein.

15. Multilingual Explanation: Marathi (Roman Script)

Background image sathi background-size: cover vapara. Clear text contrast sathi image var linear-gradient dark layer joda.

16. Practice Questions

  1. What is the difference between background-size: cover and contain?
  2. Why should dark gradient overlays be layered over background photos?

17. Practical Coding Challenge

Create a .hero-wrapper rule with a dark linear-gradient overlay placed on top of an image URL with background-size: cover.

18. Related Topics