Sub topic 2
The img element as the baseline and fallback
This chapter assumes you know basic HTML, including the img element with its src and alt attributes. It also assumes you know what a CSS pixel is. Every responsive technique in this tutorial builds on the img element, so we start with what it does on its own and what it must keep doing.
What img guarantees
A minimal image looks like this:
<img
src="photo-800.jpg"
width="800"
height="600"
alt="A lighthouse on a rocky coast at sunset">The element has three jobs that stay the same when you add responsive features:
srcnames a file the browser can always load.altgives a text equivalent for screen readers, for search engines, and for when the file fails to load.widthandheightgive the image's intrinsic size, so the browser can reserve space before the file arrives. A later chapter covers this in detail.
Responsive markup adds hints on top of this. It never replaces these three jobs.
src is the fallback
The src attribute is the fallback in two senses.
First, a browser that does not understand srcset or picture ignores them and loads src. Old browsers and some simple tools such as feed readers behave this way. The page still shows an image.
Second, a modern browser uses src when no other candidate applies. With srcset, the browser considers src as one candidate. With picture, the browser uses the img when no source element matches.
So pick a src that is a safe default. A mid-sized JPEG is a good choice. A very large file wastes bandwidth for everyone on the fallback path. A very small file looks blurry.
img is required inside picture
The picture element is only a wrapper that holds source elements. The img inside it does the actual work. The browser picks a source, then applies the result to the img.
<picture>
<source type="image/webp" srcset="photo.webp">
<img src="photo.jpg" width="800" height="600" alt="A lighthouse on a rocky coast at sunset">
</picture>Three consequences follow:
- A
picturewith noimgshows nothing. Theimgmust be the last child. alt,width,height,loading, and CSS classes go on theimg, not onpictureorsource.- CSS that targets the image should select the
img. Stylingpicturedoes not size the picture's content.
Common errors
- Missing
alt. Without it, screen readers may read the file name. Usealt=""for purely decorative images, so assistive tools skip them. - Putting
altonsource. The attribute is ignored there. - Putting
imgbefore thesourceelements. The browser stops at theimgand never checks the sources. - Using a huge
src. Every fallback visitor downloads it.
Checking your baseline
Before adding any responsive features, confirm that the plain img works. Remove srcset and sizes in your mind. Would the page still be correct? If yes, your baseline is sound. You then add hints that improve the result without risking a broken image.
Exercise
A developer writes a picture element with two source elements and places the img element first, before the sources. The page shows the img's src every time. Why, and how do you fix it?
Show answer
The browser reads the picture's children in order and uses the img as soon as it reaches it, so the later source elements are never considered. Move the img to be the last child of picture, after all source elements.
Exercise
Which element should carry the alt attribute in a picture element, and why?
Show answer
The img element. The source elements only supply candidate files. The img is the element that is rendered and exposed to assistive tools, so alt, width, height, and loading belong there.