Srcset offers several copies of an image at different sizes. Sizes tells the browser how wide the image is likely to appear so it can choose an appropriate file. Think about what the picture adds to the story, not just whether it looks nice on the screen.
What You Need to Know
A 480w candidate is an image 480 pixels wide, not necessarily one that always displays at 480 CSS pixels. The browser also considers device pixel density.
For srcset with width descriptors, sizes tells the browser the expected display width. It can pick an unexpected file if sizes is inaccurate.
Pictures can make a page much easier to understand, but they are useful only when the file loads and the important meaning is also available as text.
Example: See It in Action
Here is a simple example to experiment with. Type it, save the file, and check what the browser displays.
This is a short HTML fragment. To try it as a webpage, put it inside the body element of a complete HTML document.
<img
src="images/city-800.jpg"
srcset="images/city-480.jpg 480w, images/city-800.jpg 800w, images/city-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw, 600px"
alt="City skyline at sunrise"
width="800"
height="450">
Let’s Understand the Example
- Img displays an image. Src chooses the file and alt provides a useful text alternative.
- Srcset provides alternative image files for different screen sizes or pixel densities.
- Sizes provides a browser hint about an image display width or icon size, depending on the element.
- Src tells the browser where to find an image or other embedded resource.
- Alt supplies a text alternative for an image. Empty alt is appropriate for purely decorative images.
An image appears only if its file path is correct. If it is informative, its alt text should share the same useful meaning; if it is decoration, empty alt text is often right.
What Should You See?
If the image file is available, the picture appears in the page. If not, the browser may show a broken image or fallback text. The example alt text is “City skyline at sunrise”. Check that the text alternative fits the picture’s purpose.
Cover the picture and read the surrounding text aloud. If essential information has disappeared, add a useful text alternative or nearby explanation.
Try It Yourself
- Add a new 1600w candidate and predict when it might be selected.
- Save your changes, open or refresh the page, and compare the result with the original example.
- Tell a friend what your change did. If you are not sure, undo it, refresh the page, and compare.
Common Beginner Mistakes
- Writing 480w for a file that is not actually 480 pixels wide.
- Never guess at an image description. Write alternative text that matches the picture and its purpose on this page.
Key Takeaways
- Srcset offers several copies of an image at different sizes.
- Img displays an image. Src chooses the file and alt provides a useful text alternative.
- For srcset with width descriptors, sizes tells the browser the expected display width. It can pick an unexpected file if sizes is inaccurate.