Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 122 ยท Images and Media

Background Images in HTML Pages

Add background images with CSS, control size, position and repeat, layer gradients for readable text, and learn when to use img instead of a background.

HTML has no element for a background image. The old <body background="paper.png"> attribute is obsolete (see Deprecated and Obsolete Features). Today a background is a CSS feature: you put a picture behind an element's content with the background-image property, and the element's text and children are drawn on top.

Backgrounds are perfect for decoration: textures, banners, hero sections, patterns. They are the wrong tool for content. That difference decides which of the two image tools you should reach for, so this lesson starts there.

<img> or background?

Ask one question: would a person miss this picture if it vanished?

If the image is... Use Why
A photo in an article, a product picture, a chart, a logo <img> It carries meaning, so it needs alt text, can be lazy-loaded, saved, printed and found by search engines.
A texture, a gradient, a decorative banner behind a heading CSS background It has no meaning to describe; screen readers ignore it.

CSS backgrounds have no alt attribute. If you put important information in one, blind users never get it, and the picture disappears when someone turns on a high-contrast mode or prints the page. If the image matters, use <img> (see Images).

The basics

Give an element a background image with background-image: url(...). The path follows the same rules as links (see File Paths), but a path inside a stylesheet is relative to the stylesheet, not to the HTML page.

HTML
<style>
  .banner {
    background-image: url("/samples/sunset.svg");
    padding: 4rem 1rem;
  }
</style>
<header class="banner">
  <h1>Welcome</h1>
</header>

By default the image sits in the top-left corner at its natural size and repeats in both directions until the element is full. Four properties change that.

Repeat, size, position and attachment

  • background-repeat: repeat (default), repeat-x, repeat-y or no-repeat. Seamless patterns tile; single pictures usually should not.
  • background-size: auto (natural size), a length such as 120px, cover (scale until the element is fully covered, cropping if needed) or contain (scale until the whole image fits, leaving gaps).
  • background-position: where the image sits, for example center, top right or 20% 80%. With cover, this decides which part gets cropped.
  • background-attachment: scroll (default, moves with the element) or fixed (stays put while the page scrolls, a parallax-like effect). Fixed backgrounds are poorly supported on many phones, so do not rely on them.
Repeat, cover and contain

Notice background-color in the demo: always set a color that matches the image's overall tone. It shows while the image is loading, and it keeps text readable if the image fails to load.

The background shorthand

All of these can be combined in one declaration. Order matters in only one place: background-size must follow background-position after a slash.

CSS
.hero {
  background: #1e293b url("/images/hero.jpg") center / cover no-repeat;
}

Read it as: color #1e293b, image hero.jpg, position center, size cover, no repeat. Beware that the shorthand resets every background property you do not mention, so a later background: red removes your image.

Layering gradients for readable text

Text over a photo is a classic accessibility problem: some parts of the picture are bright, some dark, and the contrast changes along the line. A reliable fix is to lay a semi-transparent gradient over the image. The background-image property accepts a comma-separated list, and the first image is on top.

A hero section with a dark overlay

The gradient is itself an image generated by CSS, so no extra file is downloaded. Gradients on their own make fine backgrounds too: background-image: linear-gradient(135deg, #2563eb, #7c3aed);.

Backgrounds that adapt

Phones and desktops need different pictures, and a 2,000-pixel hero is wasteful on a small screen. Backgrounds support the same ideas as Responsive Images:

  • Use background-size: cover so one image fits any shape.
  • Swap the file in a media query, for example a smaller file for narrow screens.
  • Use image-set() to offer the same picture at several densities.
CSS
.hero { background-image: url("hero-small.jpg"); }

@media (min-width: 800px) {
  .hero { background-image: url("hero-large.jpg"); }
}

Three more tips:

  • Give the element a height or padding. A background does not make an empty <div> any taller.
  • Background images are not lazy-loaded by an attribute, and they are discovered only after the CSS is parsed, so a hero background that is the main visible content can load later than an <img>. If it is the most important thing on screen, consider a real <img> with object-fit: cover and fetchpriority="high" (see Loading Resources Efficiently).
  • Small icons repeated all over a page can be joined into one sprite or inlined as SVG to save requests.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which attribute puts a background image on the body in modern HTML?

    The background attribute on body is obsolete. Backgrounds are set with CSS.
  2. 2. A photo of your company's product helps customers decide what to buy. How should you add it?

    The picture carries information, so it needs alt text, and an img can be lazy-loaded, saved and indexed. CSS backgrounds have no text alternative.
  3. 3. What does background-size: cover do?

    cover keeps the aspect ratio and fills the box. contain is the one that shows the whole picture.
  4. 4. Why layer a semi-transparent gradient above a photo behind text?

    The first image in the list is on top, so a translucent dark gradient evens out the background under the text.

Summary

  • Backgrounds are CSS: background-image, background-repeat, background-size, background-position; the <body background> attribute is obsolete.
  • Use <img> for content and CSS backgrounds for decoration, because backgrounds have no alt text.
  • cover fills and crops; contain fits and leaves gaps; no-repeat stops tiling.
  • Always set a background-color fallback and make sure the element has size of its own.
  • A comma-separated list layers images, first on top, so a gradient overlay keeps text readable.
  • Swap large background files for smaller ones with media queries on narrow screens.

Next up: serving the right image to every screen with Responsive Images.