Upholstering the Web: CSS Backgrounds with Images

Master the essential CSS properties for creating beautiful, responsive, and visually engaging backgrounds on the web.

Lesson ProgressStep 1 of 8
.box
0 EXP

Welcome! Let's learn how to decorate our web elements using CSS background properties.

/* Our canvas is ready! */

`background-image`: Setting the Scene

This is the starting point. The background-image property sets one or more background images for an element. The most common way to use it is with the url() function, which points to the path of your image.

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

You can also use CSS gradients (like linear-gradient()) as a background image.

System Check

What function is used to set an image path for a background?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🖼️
Image Setter

Set your first CSS background image.

📏
Size Master

Master the 'cover' and 'contain' values.

🎯
Positioning Pro

Perfectly center a background image.

✍️
Shorthand Savvy

Use the 'background' shorthand property.

Mission: Build a Hero Section

Style the `.hero` class. Give it a background image, make it cover the area, center it, and prevent it from repeating. Try the shorthand!

A.D.A. Feedback:

> Awaiting input...

Challenge: Order the Declarations

Drag these CSS declarations into the most logical order you'd write them in.

background-size: cover;
background-image: url('hero.jpg');
background-position: center;
background-repeat: no-repeat;

Challenge: Complete the Shorthand

Fill in the missing parts of the `background` shorthand property.

background: url('img.png')center;

Consult A.D.A.

Community Holo-Net

Upholstering the Web: A Deep Dive into CSS Backgrounds

When we move beyond simple colors, CSS backgrounds become one of the most powerful tools in our design arsenal. They allow us to create rich, layered, and dynamic visual experiences. Mastering properties like background-image, background-size, and background-attachment is essential for building modern, professional-looking websites.

The Core Properties: A Breakdown

Let's look at the individual properties that give us granular control over our backgrounds.

  • background-image
    This is your starting point. It sets an image (or multiple images) as the background. The value is almost always the url() function, which points to the image file. You can also use CSS gradients here.
    background-image: url('images/hero.jpg');
  • background-repeat
    By default, background images will tile themselves infinitely in all directions. To stop this, you use no-repeat. You can also repeat along a single axis with repeat-x (horizontal) or repeat-y (vertical).
  • background-position
    This property controls the starting position of your (non-repeating) image. You can use keywords (top, center, bottom, left, right) or specific values (50% 50%, 20px 10px). center (or 50% 50%) is extremely common for hero images.

The Most Important Property: `background-size`

This property defines the size of the background image and is the key to responsive backgrounds. The two most critical values are:

✔️ `background-size: cover;`

This tells the browser to **scale the image as large as possible** to completely fill (cover) the container. It will **maintain its aspect ratio** and crop any excess (either vertically or horizontally). This is the standard for full-screen hero images.

✔️ `background-size: contain;`

This tells the browser to **scale the image as large as possible** to fit *inside* the container **without being cropped**. It will also maintain its aspect ratio, but this may leave empty space (known as "letterboxing"). This is perfect for logos or image galleries.

Avoid using 100% 100%, as this will stretch and distort your image to fit the container's dimensions, breaking its aspect ratio.

Creating Depth: `background-attachment`

This property determines if the background image scrolls with the content or stays in one place.

  • scroll (Default): The background scrolls along with the element's content.
  • fixed: The background is **fixed relative to the viewport**. As you scroll down the page, the content moves over the background, creating the famous **parallax effect**.
  • local: The background is fixed relative to the element's *content*. If the element itself has a scrollbar, the background will scroll along with the content inside it.

Putting It All Together: The `background` Shorthand

Writing all these properties separately is tedious. The background shorthand property lets you set them all in one line. The syntax can be complex, but a standard hero image looks like this:

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

The most important rule: If you include background-size (e.g., `cover`), it **must** come after background-position (e.g., `center center`), and they **must** be separated by a forward slash /.

Advanced Technique: Multiple Backgrounds

You can layer multiple backgrounds on top of each other by simply providing a comma-separated list of values. The first image in the list is the topmost layer.

.layered-element {
  background-image: url('logo.svg'), url('pattern.png');
  background-repeat: no-repeat, repeat;
  background-position: center, top left;
}

In this example, `logo.svg` will be placed (centered, no-repeat) *on top* of `pattern.png` (tiled, starting at the top left).

Key Takeaway: CSS backgrounds are a complete system. By combining `image`, `repeat`, `position`, and `size`, you can responsively handle any design. Always prioritize `cover` for full-screen images and remember the special `/` syntax in the shorthand.

CSS Backgrounds Glossary

`background-image`
The CSS property used to set one or more background images for an element. Its value is typically a `url()` function or a CSS gradient.
`background-repeat`
Defines how a background image will be repeated. Common values include `repeat` (default), `no-repeat`, `repeat-x` (horizontal only), and `repeat-y` (vertical only).
`background-size`
Specifies the size of the background image. This is a crucial responsive property.
`cover`
A value for `background-size`. It scales the image (while maintaining aspect ratio) to be as large as possible so that it completely covers the background area. Parts of the image may be cropped.
`contain`
A value for `background-size`. It scales the image (while maintaining aspect ratio) to be as large as possible to fit *within* the background area. No cropping will occur, but this may leave empty space.
`background-position`
Sets the initial position of the background image. It can be defined with keywords (e.g., `center`, `top right`) or precise values (e.g., `50% 50%`, `20px 0`).
`background-attachment`
Determines if the background image's position is fixed within the viewport or scrolls along with its containing element.
`fixed`
A value for `background-attachment`. The background is fixed relative to the viewport, creating a parallax effect as the user scrolls.
`local`
A value for `background-attachment`. The background is fixed relative to the element's *content*. If the element has a scrollbar, the background scrolls with the content.
`background`
A shorthand property for setting all the individual background properties (color, image, repeat, position, size, etc.) in a single declaration.
Multiple Backgrounds
A CSS feature that allows you to layer multiple background images on a single element by providing comma-separated values for the background properties. The first image listed is the topmost layer.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building responsive, modern web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS3 specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!