HTML Picture Element
A single img is enough for many pages. When you need different files for different screens or formats — a wide crop on desktop, a tighter crop on phones, or a modern format with a JPEG fallback — the picture element offers candidates. The browser chooses one; your fallback img keeps older clients and accessibility covered.
What picture adds
picture is a container. Inside it you place zero or more source elements and exactly one fallback img. Each source can declare when it applies (media) or what format it is (type), and where the file lives (srcset). The img is not optional decoration: it is the required fallback and the place for alt.
Direct answer
Wrap one or more <source> elements and a fallback <img> inside <picture>. Use media for viewport conditions and type for formats such as WebP. The img still needs src and alt. Beginners can start with two widths of the same photo before collecting every modern format.
Art direction with media
Art direction means different crops or layouts for different viewports — not only “the same photo smaller.” media on source lets you point wide and narrow layouts to different files.
picture with media and fallback img
html<picture>
<source media="(min-width: 800px)" srcset="images/wide.jpg">
<source media="(min-width: 400px)" srcset="images/medium.jpg">
<img src="images/small.jpg" alt="Mountain trail at sunrise">
</picture>
Reading the media sample
- Sources are listed from more specific conditions toward broader ones in common teaching patterns; the browser still evaluates and picks a match
srcsetonsourcepoints to the candidate file URL- The final
imgprovidessrcandaltwhen no source matches or whenpicturesupport is incomplete - Resize the window in practice to see which file is appropriate — use DevTools network if you want to confirm the request
Format choice with type
Sometimes the same composition ships as WebP for supporting browsers and JPEG for everyone else. type tells the browser the MIME type so it can skip unsupported formats without downloading them first.
picture with type and fallback img
html<picture>
<source type="image/webp" srcset="images/hero.webp">
<img src="images/hero.jpg" alt="Product hero on a clean desk">
</picture>
Keep day-one scope small
You do not need every format and every breakpoint on the first try. Make one picture with two sensible candidates work, keep alt honest on the img, then expand. Responsive srcset density descriptors and advanced sizes attributes come later as you grow.
alt stays on img
Do not put accessibility text only on source. Screen readers and fallback rendering rely on the img. Decorative rules from the Images session still apply: informative images need real alt; pure decoration can use alt="".
picture vs one img
Use a plain img when one file is enough. Reach for picture when you truly need different files for conditions — viewport art direction or format switching. Extra markup without a real need only complicates maintenance.
Order and fallback habits
List source elements before the img. Always include the img. If you forget the fallback, some browsers and many teaching validators will treat the pattern as incomplete. Test with a narrow window and a wide window, and with a browser that may not prefer your fancy format.
Practice in your editor
- Prepare two image widths of the same photo (or two clear practice files).
- Build a
picturewith twosourcemedia queries and oneimgfallback. - Resize the browser window and observe which file is appropriate.
- Confirm
altremains on theimg, not only in a comment. - Optionally add a WebP
sourcewithtype="image/webp"above a JPEGimgand confirm the pattern still shows an image everywhere.
Easy mistakes
- Forgetting the fallback
img - Putting
altonly onsourceinstead of onimg - Over-complicating with many formats before the basic media pattern works
- Using
picturewhen a single optimizedimgwould have been enough - Mismatched crops that surprise users when the viewport crosses a breakpoint
- Confusing
srcsetonsourcewith thehrefof a link
Summary
pictureoffers multiple image candidates; the browser picks one- Use
sourceplus a required fallbackimg mediaguides viewport choice;typeguides format choicealtstays on theimg- Start simple: two widths or one modern format plus a JPEG fallback
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top