Practical Exercise 10: Links with Images in HTML
Learn to create links with images in HTML easily. This practical exercise teaches you how to use combined <a> and <img> tags to improve the visual navigation of your web pages.
Activity
Create an HTML document where an image serves as a link.
Interactive Solution
Solution with Complete HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Links with Images</title>
</head>
<body>
<h1>Link with Image</h1>
<a href="https://div.zone/" target="_blank">
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Image description"
width="360"
height="200"
/>
</a>
</body>
</html>
Exercise Variants
Variant 1: Image with Secure Link
When the image is a link, the same link security rules apply. Add rel and a title for the tooltip.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Secure Image Link</title>
</head>
<body>
<h1>Our Sponsors</h1>
<a
href="https://div.zone/"
target="_blank"
rel="noopener noreferrer"
title="Visit DivZone - AI-powered web page generator"
>
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="DivZone logo, AI-powered web page generator"
width="360"
height="200"
/>
</a>
</body>
</html>
rel="noopener noreferrer": protects against tabnabbing (reinforcement from exercise 3).title: shows a tooltip when the user hovers over the image.alt: describes the image and link destination for screen readers.
Variant 2: Clickable Image Gallery
Create a gallery where each image links to a different site, with uniform size.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Project Gallery</title>
</head>
<body>
<h1>My Projects</h1>
<figure>
<a href="https://div.zone/" target="_blank" rel="noopener noreferrer">
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Project 1: DivZone website"
width="300"
height="167"
/>
</a>
<figcaption>Project 1: DivZone</figcaption>
</figure>
<figure>
<a href="https://github.com" target="_blank" rel="noopener noreferrer">
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Project 2: GitHub Profile"
width="300"
height="167"
/>
</a>
<figcaption>Project 2: GitHub</figcaption>
</figure>
<figure>
<a href="https://developer.mozilla.org/en-US/" target="_blank" rel="noopener noreferrer">
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Project 3: Documentation on MDN"
width="300"
height="167"
/>
</a>
<figcaption>Project 3: MDN Web Docs</figcaption>
</figure>
</body>
</html>
Variant 3: Image Links with target="_blank" vs target="_self"
The behavior of an image link is identical to that of a text link.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Link Behavior</title>
</head>
<body>
<h1>Target comparison on images</h1>
<p>Opens in a new tab:</p>
<a href="https://div.zone/" target="_blank" rel="noopener noreferrer">
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Link that opens in a new tab"
width="200"
height="111"
/>
</a>
<p>Opens in the same tab (default behavior):</p>
<a href="https://div.zone/" target="_self">
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Link that opens in the same tab"
width="200"
height="111"
/>
</a>
</body>
</html>
Don't use target="_blank" without rel="noopener noreferrer". Your users' security is as important as the visual design.
Mini-Challenge: Put It to the Test
Create a project gallery containing:
- An
<h1>with the title "My Web Projects" - At least 3 image links, each pointing to a different site
- All images with descriptive
alt,width, andheight - Each link with
target="_blank"andrel="noopener noreferrer" - A
titleon each<a>describing the destination - Group each image link with
<figure>and add<figcaption>
Try to build it without looking at the previous examples. If you get stuck, go back to the variant you need.
Common Mistakes with Links and Images
| Mistake | Why It's a Problem |
|---|---|
Empty alt on images that are links | The user doesn't know where the link goes. alt should describe the destination, not just the image |
Not closing <a> properly | If </a> is poorly nested, parts of the page become unintentionally clickable |
Broken link in href | If the URL doesn't exist or is misspelled, the link goes nowhere. Always verify URLs |
Forgetting rel="noopener noreferrer" | Security vulnerability: the destination page can manipulate your site |
| Using huge images for small links | The user downloads unnecessary megabytes. Resize images to actual display size |
Frequently Asked Questions
Does an image link need a different alt than a regular image?
Yes. The alt of an image link should describe the action or destination of the link, not just the image. For example, instead of alt="DivZone Logo", use alt="Visit the DivZone website". This helps screen reader users understand that the image is clickable and where it leads.
Can I link an image to WhatsApp?
Yes, using the https://wa.me/ protocol in href. Example:
<a href="https://wa.me/14155551234" target="_blank" rel="noopener noreferrer">
<img src="whatsapp-icon.png" alt="Contact us on WhatsApp" width="64" height="64" />
</a>
This opens a WhatsApp conversation with the specified number. It's a widely used technique on landing pages and commercial sites.
Links with images mastered? Create a visual portfolio with DivZone AI where each image is a link to a different section. You'll see how the generator combines <a> and <img> tags in a real project. Learn by doing!