Density Descriptors (x) in srcset
You'll learn how to serve high-resolution images to retina and high-DPI screens using x descriptors.
What Are Density Descriptors?
Density descriptors let you provide multiple versions of the same image at different resolutions for screens with different pixel densities. You write them as 1x, 2x, 3x, etc., where the number represents how many device pixels correspond to each CSS pixel.
Think of it like printing photos: a standard screen is like a regular print, but a retina display is like a high-quality glossy print that needs more detail (more pixels) to look crisp.
When to Use x Descriptors
Use density descriptors when:
- Your image has a fixed CSS size (like a logo that's always 200px wide)
- You want sharper images on high-DPI displays (retina screens, modern phones)
- You're not making the image fluid or responsive in size
Basic Syntax
<img
src="logo.png"
srcset="logo.png 1x, logo-2x.png 2x, logo-3x.png 3x"
alt="Company logo"
width="200"
>
Here's what happens:
- Standard screens (1x density): get
logo.png - Retina/high-DPI screens (2x density): get
logo-2x.png(twice the pixels) - Ultra-high-DPI screens (3x density): get
logo-3x.png(three times the pixels)
The browser automatically picks the best match based on the device's screen density.
The Difference from w Descriptors
Unlike w descriptors (which work with fluid layouts), x descriptors are for fixed-size images where you know the exact display dimensions. The 1x file might be 200×100px, while the 2x version is 400×200px — but both display at the same CSS size.
Key Takeaway: Use density descriptors (1x, 2x, 3x) in srcset to serve appropriately sharp images for retina displays when your image has a fixed CSS size.