HTML Images: Sources and Alternative Text
Embed an image
The void element img displays an image resource. Its src is a URL, such as a relative file path. Its alt is the text equivalent of the image in this context; do not add a closing tag. The examples use embedded image data to avoid external files. In a real site a short URL such as images/hill.svg is more readable.
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" alt="Green hill beneath a blue sky">A green hill appears against a blue background. The text alternative conveys the same subject.
Distinguish decoration from information
If the adjacent words already convey everything and the image is only decoration, set alt="". An omitted alt attribute is not the same as an intentional empty alternative. Do not repeat a caption unnecessarily; describe information not otherwise conveyed.
<p> <img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2224%22%20height%3D%2224%22%20viewBox%3D%220%200%2024%2024%22%3E%3Ccircle%20cx%3D%2212%22%20cy%3D%2212%22%20r%3D%228%22%20fill%3D%22gold%22%2F%3E%3C%2Fsvg%3E" alt="" width="24" height="24"> Workshop is open.</p>The icon adds decoration; the sentence supplies the message.
Name a functional image
When an image is the only content of a link, its alternative should identify the link destination or action. A complex chart may need a short alternative plus a longer explanation in nearby text.
<a href="#route"> <img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" alt="View hiking route"></a><h2 id="route">Hiking route</h2>The image acts as a link with a meaningful accessible name.
Practice and next steps
Write alt text for a meaningful picture, then decide how it would change if the picture became a link.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Images: Sources and Alternative Text — Knowledge Check
Check the core concepts from this lesson.
An image repeats a nearby sentence and adds decoration only. What alternative is suitable?
An image is the only content of a link to a map. What should its alt convey?
Which statement about img is correct?
An informative chart conveys a trend not stated elsewhere. What should its alternative communicate?
What is wrong with using a closing </img> tag?