Sub topic 11
Turning an image into a link
This chapter assumes you can write an a element with an href, add an img with src and alt, and tell relative paths from absolute URLs. It covers how to combine the two elements, what a screen reader announces for the result, and the mistakes that make linked images hard to use.
The basic pattern
An a element can contain any inline content, and an img is inline content. To make an image clickable, put the img inside the a:
<a href="/">
<img src="images/logo.svg" alt="Example Co. home" width="120" height="40">
</a>Clicking anywhere on the image follows the link. Nothing else changes. The img keeps its own attributes, and the a keeps its own, including target and rel.
The img element is a void element. It has no closing tag and no content, so you cannot put an a inside it. The nesting always goes a outside, img inside.
Alt text becomes the link text
A link needs a name, which is the text a screen reader reads when it reaches the link. A text link gets its name from its content. An image-only link has no text, so the browser uses the image's alt text as the name.
This changes what good alt text means. For an ordinary image, alt describes the picture. For an image that is the only content of a link, alt should describe where the link goes or what it does. The same guidance applies as for text links: the name should make sense out of context.
| Image-only link | alt text | Verdict |
|---|---|---|
| Site logo in the header | `Example Co. home` | Good. It names the destination. |
| Site logo in the header | `logo` | Weak. It says what the image is, not where the link goes. |
| Site logo in the header | `Example Co. logo` | Acceptable, but "home" tells the user what will happen. |
| Download button graphic | `Download the user guide (PDF)` | Good. |
| Any image link | missing `alt` | Bad. Some screen readers read the file name or the full URL. |
| Any image link | `alt=""` with no other text in the link | Bad. The link has no name at all. |
When the link also contains text
If the link contains visible text next to the image, the text already provides the name. If the image has a meaningful alt as well, a screen reader announces both, and the user hears the same thing twice. In that case the image is redundant, so give it an empty alt:
<a href="/articles/">
<img src="images/icon-articles.svg" alt="" width="24" height="24">
Articles
</a>The link is announced as "Articles". The empty alt tells assistive technology to skip the icon. Do not leave alt out here. A missing attribute is different from an empty one, and some tools will then read the file name.
Thumbnails that open the full image
A common pattern is a small image that links to a larger version. Here the image is informative and the link action matters, so the alt text covers both:
<a href="images/red-fox-large.jpg">
<img src="images/red-fox-small.jpg" alt="Red fox standing in snow (view full size)" width="320" height="213" loading="lazy">
</a>Keep the alt text short. If a long description is needed, put it in a caption or in the surrounding text instead.
Linked images inside a figure
When an image has a caption, you can link the image and leave the caption as plain text. Place the a inside the figure, around the img only:
<figure>
<a href="/reports/2024-sales.html">
<img src="images/chart.png" alt="Bar chart showing sales growing each quarter of 2024. Open the full report." width="800" height="450">
</a>
<figcaption>Quarterly sales, 2024</figcaption>
</figure>Do not wrap the whole figure in an a just to make the image clickable. That would make the caption part of the link too, and the link name would become long and repetitive.
Logo links in a header
A frequent real-world case is a logo that links to the home page, placed above the navigation:
<header>
<a href="/">
<img src="images/logo.svg" alt="Example Co. home" width="120" height="40">
</a>
<nav aria-label="Main">
<ul>
<li><a href="/articles/">Articles</a></li>
<li><a href="/contact.html">Contact</a></li>
</ul>
</nav>
</header>The logo link sits outside the nav list because it is a brand link rather than a menu item. Some sites also include a "Home" item in the menu. If you do, both links go to the same place, so it is fine to keep both but make sure each has a clear name.
Common errors
- Missing
alt. The image link is announced by file name or URL, such as "logo dot svg". Always write the attribute, even if its value is empty. - Empty
altand no other text. The link has no accessible name. Screen reader users hear only "link". Use an emptyaltonly when text in the same link supplies the name. - Describing the picture instead of the destination.
alt="Blue circle with a white E"tells the user nothing about what the link does. - Starting alt with "link to" or "image of". Screen readers already announce "link" and "graphic". Saying it again adds noise.
- Several adjacent links to the same URL. If an image and a title both go to the same article, merge them into one
a, or give the image an emptyaltand its own link only if it needs to be separate. Duplicates slow down keyboard users, who must tab through each one. - Hiding the focus outline. Keyboard users rely on the browser's focus indicator to see which linked image is active. If you restyle it in CSS, replace it with something equally visible.
- Dropping
widthandheight. A linked image still shifts the layout while loading. Set both attributes, as you would for any other image.
A quick check
Look at each image link on a page and ask two questions. First, if the image disappeared, would the link still make sense from its text alone? Second, if the link contains other text, is the image's alt empty? Two yes answers mean the link is accessible.
Exercise
A page has this code. A screen reader announces "Pricing, Pricing, link". What is wrong, and how do you fix it? <a href="/pricing.html"><img src="images/tag.svg" alt="Pricing" width="24" height="24"> Pricing</a>
Show answer
The image and the visible text both supply the name "Pricing", so it is read twice. The icon is redundant, so give it an empty alt: <img src="images/tag.svg" alt="" width="24" height="24">. The link is then announced once, using the visible text.
Exercise
You want a small photo of a lighthouse (images/lighthouse-small.jpg, 300 by 200) to open images/lighthouse-large.jpg when clicked. Write the markup with suitable alt text.
Show answer
<a href="images/lighthouse-large.jpg"><img src="images/lighthouse-small.jpg" alt="White lighthouse on a rocky coast (view full size)" width="300" height="200"></a>. The alt text describes the image and the link action, and width and height are set to avoid layout shift.