Sub topic 4
Describing candidates with w descriptors
This chapter assumes you can write an img element with src and alt, and that you know the difference between CSS pixels and device pixels. It also assumes you have seen srcset with x descriptors. Here you will learn the w descriptor, which is the form you will use for most fluid layouts.
Why x descriptors stop being enough
An x descriptor says "use this file on a screen with this pixel density." That works when the image always displays at one fixed CSS size, such as a 400-pixel logo.
Most content images are not fixed. A photo might fill the full width of a phone, half the width of a tablet, and a third of a desktop window. The same file would need different densities in each case. You cannot express that with 1x and 2x.
The w descriptor solves this by moving the decision to the browser. You describe each file by its own width. The browser then works out how many pixels it needs and picks the best match.
The syntax
A srcset value is a comma-separated list of candidates. Each candidate is a URL, whitespace, and a descriptor.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 60em) 33vw, (min-width: 40em) 50vw, 100vw"
width="1600"
height="1200"
alt="A lighthouse on a rocky coast at sunset">Here 400w means "this file is 400 pixels wide." The w stands for width.
The rules for the descriptor are strict:
- It is a whole, positive number followed directly by
w.800wis valid.800.5w,800px, and800are not. - It describes the file's intrinsic width, which is its real width in image pixels. It does not describe how wide the image will be shown on the page.
- You cannot mix
wandxdescriptors in the samesrcset. A mixed list is invalid and the browser discards the broken candidates. - Each candidate must have a unique descriptor value.
What the descriptor is not
The most common mistake is to read 800w as "show this at 800 pixels wide." It is only a label for the file. The display width comes from your CSS layout and from the sizes attribute, which the next chapter covers.
Think of srcset with w descriptors as a menu with sizes printed on it. It does not say which item to order.
What the browser does with the numbers
The browser knows three things when it chooses a file:
- The width of each candidate, from your
wdescriptors. - How wide the image slot will be, in CSS pixels. This comes from
sizes. - The device pixel ratio (DPR) of the screen.
It multiplies the slot width by the DPR to get the number of image pixels it needs. Then it picks a candidate that covers that need.
Take a slot that is 400 CSS pixels wide:
| Device pixel ratio | Pixels needed | Likely choice from 400w, 800w, 1600w |
|---|---|---|
| 1 | 400 | `photo-400.jpg` |
| 2 | 800 | `photo-800.jpg` |
| 3 | 1200 | `photo-1600.jpg` |
At DPR 3 the browser needs 1200 pixels. No candidate has exactly 1200, so it picks the smallest file that is large enough, which is the 1600-pixel one.
The table shows the typical outcome. The specification lets the browser choose differently. It may prefer a file already in its cache, or pick a smaller file on a slow connection. Treat your srcset as a set of options, not as a command.
That line above is intentionally not used. The diagram follows.
The role of src
When you use w descriptors, src is not one of the candidates. A browser that understands srcset with w ignores src when it chooses.
Keep src anyway. It serves browsers that do not support srcset, and it is the value some tools read first. Point it at a medium-sized file that is acceptable everywhere. In the example above, that is the 800-pixel file.
What happens without sizes
If you write w descriptors and leave out sizes, the browser assumes sizes="100vw". That means "this image fills the full viewport width."
This is a safe default but often a wasteful one. A photo that takes up a third of a desktop screen is treated as if it took up all of it. The browser then downloads a file that is up to three times wider than needed.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
alt="A lighthouse on a rocky coast at sunset">This markup is valid and works. It just does not tell the browser the true layout width. You will fix that in the next chapter.
Matching descriptors to real files
The browser trusts your descriptor. It does not open the file to check. If you label a 780-pixel file as 800w, the browser calculates densities from the label, and the image can end up displayed at a slightly wrong natural size.
Check real widths before you write the markup. With ImageMagick 7, this prints each file name and its width:
magick identify -format "%f %w\n" photo-*.jpgExample output:
photo-1600.jpg 1600
photo-400.jpg 400
photo-800.jpg 800Use those numbers as your descriptors. Generating the files is covered in a later chapter.
Choosing which widths to offer
You do not need a file for every width. A few well-spread candidates are enough. Each extra file adds work for your build and storage, and gives the browser only a small benefit.
Some guidelines:
- Cover the range from the smallest slot at DPR 1 to the largest slot at your highest realistic DPR. A 400-pixel slot on a DPR 1 phone and a 800-pixel slot at DPR 2 give you the bottom of the range.
- Space candidates by roughly a factor of 1.5 to 2. A gap of 400, 800, and 1600 is a common choice.
- Do not offer files wider than the original source. Upscaling adds bytes without adding detail.
- Avoid very small steps, such as 800w and 850w. The saving is tiny and the cache is less likely to be reused.
Common errors
| Mistake | Example | Result | Fix |
|---|---|---|---|
| Unit other than w | `photo.jpg 800px` | Candidate is invalid | Write `800w` |
| Decimal width | `photo.jpg 800.5w` | Candidate is invalid | Use a whole number |
| Mixing x and w | `a.jpg 400w, b.jpg 2x` | Invalid list | Use one kind only |
| Missing comma | `a.jpg 400w b.jpg 800w` | Second candidate is lost | Separate with commas |
| Label does not match the file | `a.jpg 800w` but the file is 780 wide | Wrong density calculation | Measure and relabel |
| No sizes | `srcset` only | Treated as `100vw` | Add `sizes` |
Putting it together
The finished pattern has three parts that work as a team:
srcsetlists the files and their real widths.sizesstates how wide the image will be in the layout.widthandheightgive the aspect ratio so the page does not jump while the image loads.
You have now covered the first part. The sizes attribute is next, and after that you will trace the full selection process step by step.
Exercise
An image has srcset="a.jpg 500w, b.jpg 1000w, c.jpg 2000w" and no sizes attribute. The viewport is 600 CSS pixels wide and the device pixel ratio is 2. Which file will a browser most likely choose, and why?
Show answer
c.jpg. With no sizes attribute the browser assumes 100vw, so the slot is 600 CSS pixels. Multiplied by DPR 2, it needs 1200 image pixels. b.jpg is only 1000 pixels wide, so the smallest candidate that is large enough is c.jpg at 2000w. A browser may still choose differently, for example because of a cached file.
Exercise
Find the two errors in this srcset and correct them: srcset="small.jpg 480, large.jpg 960.5w, huge.jpg 1920px"
Show answer
Every descriptor is wrong. 480 has no unit, so it needs the w suffix: 480w. 960.5w is a decimal, so use a whole number such as 960w, after checking the real width of the file. 1920px uses the wrong unit, so write 1920w. The corrected value is srcset="small.jpg 480w, large.jpg 960w, huge.jpg 1920w".