Sub topic 3
Serving sharper images with x descriptors
This chapter assumes you know the img element with src and alt, basic CSS, and the difference between CSS pixels and device pixels. It covers srcset with x descriptors, the simplest form of resolution switching.
The problem x descriptors solve
A CSS pixel is a layout unit. On a high-density screen, one CSS pixel is drawn with several device pixels. The ratio between them is the device pixel ratio (DPR). A 400-pixel-wide image shown in a 400 CSS pixel slot looks sharp at DPR 1. At DPR 2, the browser has to stretch it across 800 device pixels, and it looks soft.
Sending an 800-pixel file to everyone fixes the blur but wastes bandwidth on 1x screens. An x descriptor lets you offer both files and let the browser choose.
Writing a density-based srcset
The srcset attribute holds a comma-separated list of candidates. Each candidate is a URL followed by a descriptor. An x descriptor says which pixel density the file is meant for.
<img
src="photo-400.jpg"
srcset="photo-400.jpg 1x, photo-800.jpg 2x"
width="400"
height="300"
alt="A lighthouse on a rocky coast at sunset">Here photo-400.jpg is for 1x screens and photo-800.jpg is for 2x screens. The src attribute is the fallback for browsers that ignore srcset. It should point to a file that works everywhere, normally the 1x version.
The width and height attributes describe the 1x display size, 400 by 300 CSS pixels. Both files must have the same aspect ratio. The 2x file is simply the same picture with twice the pixels in each direction.
What the browser does
The browser compares its own DPR with the descriptors and picks the closest match.
A few details are worth knowing:
- A candidate with no descriptor counts as
1x. - If no candidate matches the DPR exactly, the browser usually chooses the nearest one. A 3x phone with only 1x and 2x candidates will normally take the 2x file.
- The browser may take a file with a higher density than needed. It may also pick a lower one, for example on a slow or data-saving connection.
- Use the density the file actually has. Declaring a blurry file as
2xdoes not make it sharp.
When to use x descriptors
Density descriptors suit images with a fixed CSS size, such as a logo, an avatar, or a thumbnail in a fixed-width card. The slot is always the same size, so only the pixel density varies.
They do not suit fluid images. If an image fills 100% of a container that changes with the viewport, one density list cannot describe the change in size. That case needs w descriptors and sizes, which the next chapters cover.
You cannot mix x and w descriptors in the same srcset. A list that does so is invalid and the browser ignores it.
Common errors
- Forgetting the fixed size. Without
widthandheight, or a CSS width, the browser shows the image at the intrinsic size of the chosen file. A 2x file would then display at 800 CSS pixels and fill the layout. Set the display size explicitly. - Wrong aspect ratio. If the 2x file is cropped differently from the 1x file, the layout shifts depending on which loads.
- Leaving out the 1x entry. Include the base file in
srcsetas well as insrc, so the list is complete. - Upscaling. Making a 2x file by enlarging a small original adds file weight but no detail.
Exercise
An avatar is displayed at 96 by 96 CSS pixels. Write the srcset value for 1x and 2x screens, and state the pixel dimensions of each file.
Show answer
srcset="avatar-96.jpg 1x, avatar-192.jpg 2x". The 1x file is 96 by 96 pixels and the 2x file is 192 by 192 pixels. The img should also have width="96" and height="96" and a src of avatar-96.jpg.
Exercise
Why can a fluid image that fills its container not be handled well by x descriptors alone?
Show answer
An x descriptor only describes pixel density for a known display size. A fluid image changes its CSS size with the viewport, so the browser needs file widths (w descriptors) and the display width (sizes) to choose a suitable file.