← Back to HTML & CSS, page 7

html-css-0121 · Medium mid

How do you serve different background images by screen size and density, and when is a background image the wrong choice?

Change background-image inside min-width and min-resolution media queries, or use image-set(), and always set a background colour. Backgrounds have no alt text and no loading attribute, so use img or picture for content and backgrounds for decoration.

Full answer

A CSS background image is chosen by CSS rules, so you can change it with media queries.

  • By width: set a small image first, and replace it inside @media (min-width: 700px) with a larger one. Only the image of the matching rule is downloaded in the usual case.
  • By density: @media (min-resolution: 2dppx) selects a high-density image for sharp screens. image-set() does the same inside one declaration: background-image: image-set(url(a.jpg) 1x, url(b.jpg) 2x). Check the support table for the unprefixed form, and put a plain url() declaration before it as a fallback.
  • Always set a background-color close to the image average, so text stays readable before the image loads or if it fails.

Limits of background images, which matter in an interview:

  • They are not part of the content. Screen readers ignore them, there is no alt, and they do not show in print by default.
  • They cannot be lazy loaded with an attribute, and the browser finds them late, after it has parsed the CSS. For the main picture on a page, this delays the largest paint. A real img with srcset is found early and works with loading and fetchpriority.
  • Use img or picture for content such as a photo of a product or a person. Use a background for decoration, such as a pattern or a header texture.

At a width of 800px the large image is used, and at 400px the small one. The hero has a role and a label here because it carries meaning, which is a sign that an img element would be better.

If an image is the main content, use img with alt.

html
<style>
  .hero { height: 80px; background-color: #678; background-size: cover; background-image: url(/img/hero-small.jpg); }
  @media (min-width: 700px) { .hero { background-image: url(/img/hero-large.jpg); } }
  .dense { height: 40px; background-image: url(/img/pattern-1x.png); }
  @media (min-resolution: 2dppx) { .dense { background-image: url(/img/pattern-2x.png); } }
</style>
<div class="hero" id="hero" role="img" aria-label="Students in a classroom"></div>
<div class="dense" id="dense"></div>

The image files are placeholders. The check read the computed background image at 400px and 800px wide, and at a pixel ratio of 2. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Switch background-image in media queries.
  • min-resolution and image-set serve sharper images.
  • Set a background colour as a fallback.
  • Backgrounds have no alt text and are found late.
  • Content belongs in img; decoration in CSS.

Common mistakes

  • Using a background image for important content, which gives it no alt text.
  • Forgetting a background colour, so text is unreadable while the image loads.
  • Using a large background image for the first paint with no way to set its priority.

Follow-ups

  • Why is a background image found later than an img element?
  • What does image-set() do?

Tips

  • Show both ways to switch: media query width and resolution.
  • End on the rule: content is an img, decoration is a background.