Answer 16 of 50
Optimizing Images for Retina and High-Resolution Displays
An image looks blurry on a high-density screen when the browser has fewer image pixels than device pixels to fill. On a 2x display, each CSS pixel is drawn with a 2 by 2 block of device pixels, so the same area holds four times as many pixels as on a standard screen. The fix is to offer larger versions of the image and let the browser choose, without sending those larger files to screens that cannot use them.
Device pixel ratio in numbers
Device pixel ratio (DPR) is the number of device pixels per CSS pixel along one side.
| DPR | Device pixels covering one CSS pixel | Image pixels needed for a 400 by 300 CSS-pixel slot |
|---|---|---|
| 1 | 1 (1 by 1) | 400 by 300 |
| 2 | 4 (2 by 2) | 800 by 600 |
| 3 | 9 (3 by 3) | 1200 by 900 |
The pixel count grows with the square of the ratio, which is why a 3x version of a photo can be several times the file size of the 1x version. “Retina” is Apple’s marketing name for its high-density screens, and those screens do not all share one density, so plan by DPR rather than by brand name. How that image weight adds to a page’s data transfer and energy use is covered in reducing the carbon footprint of web design.
Let the browser choose with srcset and sizes
For an image that appears at a fixed size, density descriptors are enough:
<img src="badge-1x.png"
srcset="badge-1x.png 1x, badge-2x.png 2x"
width="120" height="120"
alt="Certified installer badge">
For an image whose displayed width changes with the layout, width descriptors plus sizes let the browser do the arithmetic:
<img src="room-800.jpg"
srcset="room-400.jpg 400w, room-800.jpg 800w, room-1200.jpg 1200w, room-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
width="1600" height="1067"
alt="Living room with oak floors and a gray sofa">
Here sizes tells the browser the image fills the viewport width on narrow screens and half of it otherwise; the browser multiplies that by the screen’s DPR and picks a candidate. It is free to choose differently: MDN’s img reference says “The user agent selects any of the available sources at its discretion” and names user preferences and bandwidth conditions as examples of what can shape that choice. If you see a larger file in the network panel than you expected, that is allowed behavior, not a bug.
MDN’s guide to responsive images in HTML and web.dev’s serve responsive images cover the syntax in more depth.
Always include width and height
Every example above carries width and height. MDN explains why: “Including height and width enables the aspect ratio of the image to be calculated by the browser prior to the image being loaded”, so the space is reserved and the layout does not jump. The same page adds that explicit dimensions are “especially important for lazy-loaded ones.” Set them to the intrinsic size of the largest file (or any pair with the right ratio) and let CSS such as max-width: 100%; height: auto; handle the displayed size.
Different crops for different screens
When a wide hero photo loses its subject on a phone, a different crop is a design decision, not a resolution one. The picture element handles both at once:
<picture>
<source media="(max-width: 600px)"
srcset="hero-portrait-1x.jpg 1x, hero-portrait-2x.jpg 2x">
<img src="hero-landscape-1x.jpg"
srcset="hero-landscape-1x.jpg 1x, hero-landscape-2x.jpg 2x"
width="1440" height="600"
alt="Technician installing a heat pump outside a brick house">
</picture>
For images set in CSS, such as section backgrounds, the CSS image-set() function offers the same density choice.
Treat the hero image differently from the rest
Two attributes pull in opposite directions, and getting them backwards is a self-inflicted delay:
- Below-the-fold images:
loading="lazy"defers them until the visitor scrolls near them. - The main above-the-fold image: never lazy-load it. MDN describes
fetchprioritywith the valuehighas a way to “Fetch the image at a high priority relative to other images”, which suits the image that is likely to be the page’s Largest Contentful Paint element.
How that choice shows up in Core Web Vitals is covered in how Core Web Vitals should shape design decisions.
How many densities to produce
As a working rule rather than a measured standard:
| Image type | Densities to offer | Reason |
|---|---|---|
| Icons and logos | None, use SVG | Vectors render sharply at any density |
| UI images and small thumbnails | 1x and 2x | Small display size makes a 3x version hard to tell apart |
| Product photos and hero images | 1x, 2x, and a larger width for 3x phones | These are the images visitors inspect closely |
| Decorative backgrounds | 1x and 2x, compressed harder | Detail matters less than load time |
Compare the 2x and 3x versions on a real high-density phone before deciding; if you cannot tell them apart at the displayed size, drop the larger one. Format choice for each type, including when SVG beats a bitmap, is covered in choosing SVG, PNG, or another image format.
Generating the variants
Producing four widths of every image by hand does not last. Three options, in increasing order of automation:
- CMS-generated sizes. A CMS may create several widths on upload; check which widths yours makes and whether its theme outputs them in
srcset. - Build-time generation. An image step in the build creates every width and format once and serves them as static files; it costs storage, not ongoing fees.
- An image CDN. You upload one large source and request sizes and formats by URL; it is the most flexible and adds a recurring cost.
Whichever you pick, start from a source at least as large as the biggest variant you plan to serve. A small upload can be downsized but not usefully enlarged, so set a minimum upload size where quality matters and tell contributors what it is.
Serve each image at the density the screen can use, give every img its dimensions, prioritize the hero and lazy-load the rest, and stop at 2x wherever you cannot see the difference.