A link may contain an image, which makes the picture clickable. Give the image alternative text that describes the destination or action when the image is the link's only label. The important question is always where the link leads and whether the person reading the page can predict it.
What You Need to Know
Do not place a button inside that link or wrap unrelated interactive controls in one large anchor. Keep the click target understandable.
Links are the pathways through a website. The words on a link should help someone choose where to go before clicking it.
Example: See It in Action
Try this markup in a fresh practice file. The next section explains the parts worth paying attention to.
This is a short HTML fragment. To try it as a webpage, put it inside the body element of a complete HTML document.
<a href="gallery.html">
<img src="images/gallery-preview.jpg" alt="View the photo gallery">
</a>
<p>The image's text describes the link destination.</p>
Let’s Understand the Example
- An a element becomes a working link when it has an href. Its visible text should tell readers where they will go.
- Img displays an image. Src chooses the file and alt provides a useful text alternative.
- A p groups a paragraph of related text. Start another paragraph for a new thought.
- Href contains the destination of a link. A value starting with # points to an id on the same page.
- Src tells the browser where to find an image or other embedded resource.
- Alt supplies a text alternative for an image. Empty alt is appropriate for purely decorative images.
An image appears only if its file path is correct. If it is informative, its alt text should share the same useful meaning; if it is decoration, empty alt text is often right.
What Should You See?
The page should include “The image's text describes the link destination.”. If it looks different from what you expected, open developer tools and compare the source with the elements the browser created.
Imagine this link is read out of context. Would its text still explain the destination? Rename unclear links before trying to make them look nicer with CSS.
Try It Yourself
- Replace the image alt with text that explains where clicking goes.
- Save your changes, open or refresh the page, and compare the result with the original example.
- Tell a friend what your change did. If you are not sure, undo it, refresh the page, and compare.
Common Beginner Mistakes
- Using alt="picture" when the image link really leads to a photo gallery.
- Avoid dead links, empty href values and clickable-looking text without a real destination.
Key Takeaways
- A link may contain an image, which makes the picture clickable.
- An a element becomes a working link when it has an href. Its visible text should tell readers where they will go.
- Do not place a button inside that link or wrap unrelated interactive controls in one large anchor. Keep the click target understandable.