HTML Images and Responsive Images: alt Text, figure, srcset, and picture
An image starts with one element and one honest description. From there, srcset, sizes, and picture answer a single question: which file should the browser download on this screen? This guide walks through alt text, figure and figcaption, resolution switching, and art direction, then gives you six practice questions with full explanations.
An image on a web page starts with one element: <img>, carrying a src that points at the file and an alt attribute that describes the image in words. Everything else in responsive images — srcset, sizes, and <picture> — exists to answer one follow-up question: which file should the browser download when screens differ in width and pixel density? Learn to answer that question deliberately and the rest of the topic falls into place.
alt text describes the job, not the pixels
Write alt for what the image does in context. An informative photo gets a short description of what matters about it. An image that is the only content inside a link gets alt text naming the destination or action, because that text becomes the link's accessible name. A purely decorative image gets an empty alt="" — the attribute is present, with nothing inside — which tells assistive technology to skip it. That is not the same as leaving alt off entirely: W3C's Web Accessibility Initiative warns that when the attribute is absent, some screen readers announce the image's file name instead, which almost never helps anyone. For the same reason, a decorative image should carry no title attribute either.
figure and figcaption
<figure> marks self-contained content that the surrounding prose refers to and that could move elsewhere in the document without breaking the flow — an image, a diagram, a code sample, a pull quote. It does not have to contain an image at all. <figcaption> supplies the caption and must be the first or last child of the figure, never buried in the middle, and there is only ever one of them. A caption is visible to every reader; alt text stands in for the image. They do different jobs, so a caption is not a reason to drop alt.
srcset and sizes: resolution switching
When the same picture just needs to arrive at a bigger or smaller file size, keep one <img> and list candidates in srcset. Use width descriptors (photo-800.jpg 800w) when the image's layout width changes with the viewport, and pair them with sizes, which tells the browser how wide the image's slot will be before layout is known. Use pixel-density descriptors ([email protected] 2x) when the image always displays at a fixed size and only the screen's density varies; a candidate written with no descriptor at all counts as 1x. Never mix the two kinds in one srcset, and note that sizes belongs only on an element whose candidates all use w descriptors. If you use w descriptors and omit sizes, the browser falls back to a default source size of 100vw and assumes the image fills the viewport — usually over-downloading.
picture for art direction
<picture> is for when the image itself should change: a tight crop on phones, a wide establishing shot on desktop, or a modern format like AVIF with a JPEG fallback. It holds zero or more <source> elements followed by exactly one <img>, which must come last and carries the alt. Inside <picture>, a <source> must use srcset and must not use src. The browser walks the sources top to bottom and takes the first one whose media and type both pass; if none do, it falls back to the <img>. Leave the <img> out and no image renders at all.
This concept sits alongside the rest of the markup material in the WGU D276 Web Development Foundations study guide. Reference details come from MDN's responsive images guide.
Practice: Images And Alt Text
-
A page uses a small ornamental swirl graphic as a divider between sections. It carries no information and is not a link. What is the correct markup for accessibility?
- Include the image with an empty alt attribute: alt=""
- Leave the alt attribute off entirely so assistive technology ignores the image
- Use alt="decorative" so the purpose is documented
- Leave alt off but add title="ornamental swirl divider" instead
Show answer & explanation
Correct answer: Include the image with an empty alt attribute: alt="". An empty alt attribute is the standard signal that an image is decorative: the attribute is present but has no content, so assistive technology skips it — this is exactly W3C WAI technique H67, which pairs null alt text with no title attribute. Omitting alt is the tempting wrong answer, but a missing attribute is not the same as an empty one; WAI notes that some screen readers then announce the image's file name. alt="decorative" gets read aloud as the word "decorative," and a title attribute can be announced or surfaced as a tooltip, which is the opposite of what a divider graphic needs.
-
You write <img srcset="p-400.jpg 400w, p-800.jpg 800w, p-1600.jpg 1600w" src="p-800.jpg" alt="A red canoe"> and forget the sizes attribute. What does the browser assume about how wide the image will be displayed?
- It assumes the default source size of 100vw — the full viewport width
- It ignores srcset entirely and downloads the file named in src
- It always downloads the largest candidate, p-1600.jpg
- It downloads the first candidate listed, p-400.jpg
Show answer & explanation
Correct answer: It assumes the default source size of 100vw — the full viewport width. When srcset uses width descriptors and sizes is absent, the HTML Standard's parse-a-sizes-attribute algorithm returns a default of 100vw, so the browser plans for an image that spans the whole viewport and usually picks a file larger than needed. Falling back to src is the plausible-sounding wrong answer, but when the candidates carry w descriptors the src is not even considered as a candidate — it only serves browsers that do not understand srcset at all.
-
Which snippet is valid markup for serving an AVIF image with a JPEG fallback?
- <picture><source srcset="hero.avif" type="image/avif"><img src="hero.jpg" alt="Sunrise over the bay"></picture>
- <picture><source src="hero.avif" type="image/avif"><img src="hero.jpg" alt="Sunrise over the bay"></picture>
- <picture><img src="hero.jpg" alt="Sunrise over the bay"><source srcset="hero.avif" type="image/avif"></picture>
- <picture><source srcset="hero.avif" type="image/avif"><source srcset="hero.jpg" type="image/jpeg"></picture>
Show answer & explanation
Correct answer: <picture><source srcset="hero.avif" type="image/avif"><img src="hero.jpg" alt="Sunrise over the bay"></picture>. Inside <picture>, a <source> must use srcset and must not use src, and the single <img> must come after all the sources. The second option is the trap most people fall into by pattern-matching from <video>, where <source src="…"> is correct — but the spec says src "must not be present" on a source whose parent is picture. The third option puts the img first, violating the content model, and the fourth omits the img entirely, so nothing renders.
-
On phones you want a tightly cropped portrait of a subject's face; on wide screens you want the full landscape shot showing the whole scene. What is the right HTML approach?
- A <picture> element with <source media="…"> entries for each layout, ending in a fallback <img>
- One <img> with srcset width descriptors and a sizes attribute listing the breakpoints
- One <img> with srcset pixel-density descriptors such as 1x and 2x
- Two <img> elements, with CSS media queries setting display:none on whichever one should not appear
Show answer & explanation
Correct answer: A <picture> element with <source media="…"> entries for each layout, ending in a fallback <img>. Changing the actual crop or composition per layout is art direction, and <picture> with media conditions on each <source> is the mechanism built for it — the browser uses the first source whose media query matches. srcset with sizes is the tempting answer because it also responds to viewport width, but it only chooses between different-sized versions of the same image; it cannot swap in a different crop. Hiding a second <img> with CSS is worse still, since the browser may download both files.
-
Which statement about <figure> and <figcaption> is true?
- A <figcaption> may appear as either the first child or the last child of its <figure>
- A <figure> is only valid if it contains at least one <img>
- A <figure> may contain several <figcaption> elements, one per image
- A <figcaption> replaces the need for an alt attribute on the image inside the figure
Show answer & explanation
Correct answer: A <figcaption> may appear as either the first child or the last child of its <figure>. The content model allows a single figcaption either before or after the figure's other content, so both orderings are valid. The last option is the most seductive mistake: a caption and alt text serve different audiences and different purposes — the caption is visible commentary for everyone, while alt substitutes for an image that cannot be seen — so writing a caption is not a reason to leave alt off. A figure also does not have to hold an image, and it may hold only one figcaption.
-
A site logo always renders at exactly 200 CSS pixels wide, and you want a crisper file on high-density screens. Which markup is both valid and right for that goal?
- <img src="logo-200.png" srcset="logo-200.png 1x, logo-400.png 2x" width="200" height="60" alt="Acme logo">
- <img src="logo-200.png" srcset="logo-200.png 200w, logo-400.png 2x" alt="Acme logo">
- <img src="logo-200.png" srcset="logo-200.png 1x, logo-400.png 2x" sizes="200px" alt="Acme logo">
- <picture><source media="(min-width: 200px)" srcset="logo-400.png"><img src="logo-200.png" alt="Acme logo"></picture>
Show answer & explanation
Correct answer: <img src="logo-200.png" srcset="logo-200.png 1x, logo-400.png 2x" width="200" height="60" alt="Acme logo">. When the displayed width is fixed and only density varies, pixel-density descriptors are the right tool and no sizes attribute belongs on the element. The third option looks tidy and browsers will not break on it, but it is non-conforming: the HTML Standard requires that if a sizes attribute is present, every image candidate string carry a width descriptor, so sizes cannot be combined with x descriptors. The second option mixes a w descriptor with an x descriptor, which the spec forbids outright, and the fourth switches files on viewport width, which says nothing about pixel density.
A worked example: one hero image, four files
Say your hero image spans the full width on phones, half the width on tablets, and is capped at 600px on desktop. You export four widths and describe the layout to the browser:
<img src="hero-800.jpg"
srcset="hero-480.jpg 480w, hero-800.jpg 800w,
hero-1200.jpg 1200w, hero-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw,
(max-width: 1200px) 50vw,
600px"
width="800" height="450"
alt="A red canoe pulled up on a gravel shore">
On a 500px-wide phone with a 2x screen, the browser reads sizes top to bottom, stops at the first matching condition, and gets a slot of 100vw = 500px. It needs roughly 500 × 2 = 1000 device pixels, so it typically downloads hero-1200.jpg — the smallest candidate that is big enough. Order matters here: the browser ignores everything after the first match, so a stray (max-width: 1200px) placed first would swallow the phone case too.
Mistakes people actually make
- Mixing descriptor types.
srcset="a.jpg 800w, b.jpg 2x"is invalid. Pickwplussizes, orxalone with nosizes. - Reaching for
<picture>too early. If the image content is identical and only the file size changes,srcseton a plain<img>is simpler and lets the browser optimize freely. Save<picture>for a different crop or a different format. - Dropping
widthandheight. Those attributes let the browser compute the aspect ratio and reserve the right box before the file arrives, which prevents the page from jolting as images load. Give the dimensions of the file named insrc— the ratio is what the browser uses — and let CSS control the rendered size. - Expecting
srcsetto resize anything. It only chooses a file. The rendered size still comes from your CSS. - Captioning instead of describing. A
<figcaption>reading "Figure 3" tells a screen reader user nothing about the image; thealtstill has to carry the content.
Reasoning about the tricky cases
Two rules resolve most confusion. First, ask what changes: if only the file changes, use srcset; if the image changes, use <picture>. Second, ask what alt is replacing: if the image disappeared, what would the reader lose? Nothing means alt=""; a link destination means describe the destination; information means describe the information.
One nuance worth knowing: the HTML Standard says candidate selection happens in an implementation-defined manner, so a browser may keep a larger file it already has cached rather than fetch your "ideal" one. Treat srcset as a well-informed hint, not a command. And sizes="auto", which lets the browser use the element's own laid-out width, is only valid together with loading="lazy", because only then is layout guaranteed to be known before the image loads. Try these ideas against more markup in the D276 practice questions.
Want someone to walk you through this?
Book 1-on-1 prep for WGU D276 — original coaching, never exam content.
Stuck on this topic? WhatsApp us on +1 646 980 4914.
Working through the whole course? Read the full WGU D276 study guide, or see every WGU D276 practice topic.