Course contentsShow
← Web Development
Lesson 193 of 2,9823. CSS FundamentalsFree lesson

Background Images

Apply background images using background-image, understanding url() syntax and multiple backgrounds.

Background Images

What you'll learn: How to add images to element backgrounds using CSS's background-image property and the url() function.

While background-color fills an element with solid color, background-image lets you use actual image files as the backdrop. This is perfect for hero sections, textured containers, or decorative elements where the image isn't the main content (remember: content images should use <img> tags for accessibility).

The Core Syntax

The background-image property uses the url() function to point to your image file:

.hero {
  background-image: url('path/to/image.jpg');
}

The path works like links: it can be relative ('images/banner.png') or absolute ('https://example.com/photo.jpg'). Quotes around the path are optional but recommended.

Think of it like: You're wallpapering a room. Instead of painting (background-color), you're applying patterned wallpaper (background-image).

Multiple Backgrounds

CSS lets you layer multiple background images by separating them with commas. The first image listed appears on top:

.layered {
  background-image: url('overlay.png'), url('main-photo.jpg');
}

This creates a stack where overlay.png sits above main-photo.jpg. You might use this for texture overlays, decorative patterns on photos, or complex visual effects.

Gradient Backgrounds

You can even mix gradients (which are technically images in CSS) with regular images:

.combined {
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
                    url('background.jpg');
}

This darkens the photo with a semi-transparent black overlay.

Key Takeaway: Use background-image: url('path') to add images to element backgrounds, and stack multiple backgrounds by listing them comma-separated from top layer to bottom.