Course contentsShow
← Web Development
Lesson 427 of 2,9827. Responsive DesignFree lesson

Responsive Images in CSS

Using image-set() in CSS for resolution switching in background images.

Responsive Images in CSS

You'll learn how to make background images adapt to different screen resolutions using CSS's image-set() function.


While you've mastered responsive images in HTML with srcset and picture, CSS background images need their own solution. When you set a background-image in CSS, the browser loads just one image file—but what if you want to serve a high-resolution image to Retina displays and a standard one to regular screens?

What is image-set()?

The image-set() function lets you provide multiple image options with different resolutions directly in your CSS. The browser picks the most appropriate one based on the device's pixel density, similar to how density descriptors (x) work in HTML's srcset.

Think of it like a restaurant menu with portion sizes: you offer small, medium, and large, and customers (browsers) choose what fits their appetite (screen resolution).

Basic Syntax

.hero {
  background-image: image-set(
    url("hero-1x.jpg") 1x,
    url("hero-2x.jpg") 2x
  );
}

The 1x means standard resolution, 2x means double pixel density (Retina/HiDPI displays). Browsers automatically select the best match.

Browser Prefixes

Currently, you'll need a prefixed version for wider support:

.hero {
  background-image: -webkit-image-set(
    url("hero-1x.jpg") 1x,
    url("hero-2x.jpg") 2x
  );
  background-image: image-set(
    url("hero-1x.jpg") 1x,
    url("hero-2x.jpg") 2x
  );
}

This ensures high-resolution background images look crisp on modern displays while keeping file sizes reasonable for standard screens—performance and quality working together.

Key Takeaway: Use image-set() in CSS to provide multiple resolution options for background images, letting browsers choose the best match for each device's pixel density.