Build responsive image candidates from actual image widths
Admin ·
A width-based srcset tells a browser which copies of an image are available and how wide each copy is. A sizes attribute describes how much space the image will occupy in the layout. Use the real dimensions of your exported images when building those attributes.
Build three candidates for the harbor image
Choose Load example and Generate srcset. The width query parameter is w. The existing format=webp and fit=cover fields stay in each address, and the candidate widths appear in order as 480w, 768w and 1280w.
The example requests an 800-pixel fallback. The closest actual candidate has a width of 768 pixels, so its URL also appears in src. Check that the image server returns the declared intrinsic width at every address.
| Setting | Example output |
|---|---|
| Candidate descriptors | 480w, 768w, 1280w |
| Requested fallback | 800 pixels |
| Selected src | 768-pixel candidate |
| sizes | (max-width: 640px) 100vw, 640px |
WHATWG HTML Standard, Srcset AttributesWHATWG HTML Standard, Sizes Attributes
Check the displayed width at each breakpoint
The first rule applies at a viewport width of 640 pixels or less and declares a displayed width of 100vw. At a 375-pixel viewport, that is 375 CSS pixels. At a wider viewport, the default source size is 640px.
Use the width your CSS layout actually assigns. The browser combines the source size with factors such as device pixel ratio when choosing a candidate. Review the served image at your target breakpoints after publishing.
WHATWG HTML Standard, Sizes AttributesWHATWG HTML Standard, Selecting an Image Source
Use versioned filenames for static images
Switch URL method to URL template and enter /images/harbor-{width}.jpg. With the same width list, the generated addresses end in harbor-480.jpg, harbor-768.jpg and harbor-1280.jpg.
Choose HTML image element for an img tag, including alt text when the image conveys information. Choose srcset value to paste just the candidate list into an existing template. The preview lists the addresses as text.
WHATWG HTML Standard, Srcset AttributesWHATWG HTML Standard, Text Alternatives for Images
Common questions
Why does the HTML show an escaped ampersand?
The HTML output uses & inside attribute values. An HTML parser reads the real ampersand. The srcset value output uses literal ampersands for template systems that escape their own attributes.
What happens if my image server already uses the width parameter?
Query mode replaces occurrences of the chosen width field. It keeps the other supplied query fields.
Can I give a candidate a percentage width in sizes?
Use an allowed CSS length for source size, such as 640px or 50vw. The generator accepts px, vw, em, rem, vmin and vmax.