Understanding CSS Backgrounds: Small Tweaks, Big Visual Impact

Background images can either enhance a website or make it visually overwhelming.

CSS offers various properties to manage how background images are shown.

Let’s make things clear.

The Main Backgrounds Properties and How They Work

Size:

background-size controls how large your image appears inside an element. The most common values are:

  • cover: Fills the entire area, cropping the image if needed. Great for banners.
  • contain: Fits the entire image inside the element, even if there’s space left.
  • Or specific sizes like 100px or 50%, which give you more precise control.

What’s the common practice?

Use cover when you want full impact, in cases like hero sections. Use contain when you need the full image visible, like logos or icons.

Example:

The image completely covers the element.
(click on the image to open in a new tab)

And another version:

The whole image fits inside the element—even if that means some space is left unused.
(click on the image to open in a new tab)

Repeat:

By default, background images repeat to fill the space. That’s what background-repeat does.

  • repeat: Tiles in both directions (default behavior).
  • no-repeat: Shows the image just once.
  • repeat-x or repeat-y: Tiles horizontally or vertically only.

Repeating works best with patterns.

Example of a pattern image in Web Design.

But for photos or larger visuals, no-repeat usually makes more sense. Otherwise, it can look chaotic fast.

Example:

Repeats the pattern in both directions.
(click on the image to open in a new tab)

And a more practical one:

Only shows the image once.
(click on the image to open in a new tab)

Position:

You can control where the image appears using background-position.

  • Values like top left, center center, or bottom right anchor the image inside its container.
  • You can also use percentages or pixel values (50% 0 or 10px 20px) for more exact placement.

It’s useful when your background needs to highlight a particular part of the image, say, a face or focal object.

Example:

The image is pinned at the top center of the box.
(click on the image to open in a new tab)

Pixel-based positioning:

The image starts 20 pixels from the left, and 40 pixels from the top.
(click on the image to open in a new tab)

Attachment:

background-attachment decides whether the background scrolls with the content or stays put.

  • scroll: The background moves as you scroll (default).
  • fixed: The background stays in place while everything else moves.

A fixed background can give your layout a layered feel. But on mobile, it can cause performance issues.

The background stays locked in place while the content scrolls over it.
(click on the image to open in a new tab)

All Together

Here’s a complete example combining all four properties:

A beach photo that covers the entire box, appears only once, is centered perfectly, and stays fixed in place while scrolling.
(click on the image to open in a new tab)

Wrapping It All Up

Proper use of size, repeat, position, and attachment makes background images look deliberate, not accidental. Mastering them gives you the ability to design with confidence.

Good design isn’t just about how things look. It’s also about how they behave.

Although my blog doesn’t support comments, feel free to reply via email or X.