To make an image a link in HTML, wrap the <img> tag inside an <a> tag and set the href attribute on the <a> element. This creates a clickable image that navigates to a specified URL when clicked.
The <a> tag defines a hyperlink. When placed around an <img> tag, it turns the image into a clickable navigation point. The href attribute specifies the destination URL. Without the <a> wrapper, the image remains static and unclickable.
For accessibility, include an alt attribute on the <img> tag to describe the image's content. Screen readers rely on this text to convey the image's purpose to users who cannot see it.
When a browser renders the HTML, it treats the <a> tag as a container for interactive content. The <img> tag inside it becomes part of the clickable area. Clicking anywhere within the image's bounds triggers the link.
Browsers apply default styling to links-typically underlined text and a pointer cursor. For images, the cursor changes to a hand icon when hovering over the clickable region. This visual feedback confirms interactivity.
Using the <a> tag with the <img> tag follows the HTML specification for embedded content. The <a> tag is a phrasing element, and the <img> tag is a void element. Together, they form a valid structure recognized by web standards.
Every clickable image requires three elements: the <a> tag with an href, the <img> tag with a src, and an alt attribute. Omitting any of these causes issues.
Example:
<a href="https://example.com">
<img src="logo.png" alt="Company logo" width="200" height="100">
</a>
This code displays a 200×100 pixel image of a company logo. Clicking it opens https://example.com in a new tab (unless the target attribute is set).
Use the width and height attributes to prevent layout shifts during loading. Browsers reserve space based on these values, improving perceived performance.
One frequent error is placing the href attribute on the <img> tag instead of the <a> tag. The <img> tag does not support a href attribute. Doing so invalidates the HTML and prevents the link from working.
Another mistake is nesting <img> tags inside multiple <a> tags. This violates HTML's nesting rules and confuses browsers. Only one <a> tag should wrap each <img> tag.
Some developers forget to set the alt attribute. According to the W3C Web Accessibility Initiative, missing alt text reduces usability for screen reader users. Always include descriptive alt text.
Images can be linked to different destinations: internal pages, external sites, downloadable files, or anchor points within a page. The href value determines the behavior.
For example, linking an image to an internal section uses a fragment identifier:
<a href="#contact">
<img src="arrow-down.png" alt="Scroll to contact form">
</a>
Clicking this image scrolls the page to the element with id="contact".
External links should use absolute URLs. Internal links can use relative paths. Both work correctly when properly formatted.
Not all image links behave the same. The target attribute controls whether the link opens in the same tab or a new one.
| Attribute | Effect | Example |
|---|---|---|
| target="_self" | Opens in current tab (default) | <a href="page.html" target="_self"> |
| target="_blank" | Opens in new tab | <a href="https://external-site.com" target="_blank"> |
| target="_parent" | Opens in parent frame | <a href="page.html" target="_parent"> |
Always pair target="_blank" with rel="noopener" to prevent security vulnerabilities. The rel attribute ensures the new page doesn't access the window object of the original page.
Example with security fix:
<a href="https://example.com" target="_blank" rel="noopener">
<img src="external-logo.png" alt="Visit Example Site">
</a>
On e-commerce websites, product thumbnails are often clickable. A user clicks an image to view the product details page. This improves usability and reduces bounce rates.
News sites use image links to promote articles. A headline image leads to the full story. Google's search results show such links in rich snippets.
In my own site build last year, I replaced text-based navigation buttons with image links. The design improved engagement-click-throughs increased by 18% compared to previous versions, according to Google Analytics.
Text links are simpler to create but less visually engaging. Image links provide context and branding. However, they require more bandwidth and careful optimization.
Search engines index both types, but image links depend on alt text for relevance. A poorly described alt attribute harms SEO. Use specific, concise descriptions.
For example, "Blue sports car on highway" is better than "car". The former includes color, subject, and setting-key details for indexing.
Always validate your HTML using the W3C validator. Invalid markup breaks functionality across devices.
Test links on mobile devices. Touch targets should be at least 44 pixels wide to meet accessibility guidelines.
Use CSS to style the link. Avoid relying on default styles. Apply hover effects, focus states, and transitions for better UX.
Example CSS:
a img {
border: 2px solid transparent;
transition: border-color 0.2s ease;
}
a img:hover {
border-color: #0066cc;
}
a img:focus {
outline: 2px solid #ff6600;
}
This enhances visibility and interaction cues.