How to Turn an Image into a Link in HTML?
Last Updated :
20 Nov, 2024
To create a clickable image in HTML, you can wrap the <img> tag inside an <a> tag with the href attribute. This makes the image a hyperlink, allowing users to navigate to the specified URL when they click on the image.
1. Redirecting to a Specific Page
To turn an image into a link in HTML, wrap the <img> tag inside an <a> tag. This makes the image clickable and it will redirect to a specific page.
Syntax
<a href="URL">
<img src="path/to/image.jpg" alt="Clickable Image">
</a>
Example: In this example, we will make an image clickable, redirecting users to the GeeksforGeeks website when the image is clicked.
HTML
<body style="text-align: center;">
<h3>
Click on GeeksforGeeks logo to
Redirect into geeksforgeeks.org
</h3>
<a href="https://geeksforgeeks.org">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png"
alt="Click to visit geeksforgeeks.org">
</a>
</body>
Output
2. Redirecting to Another Page with Styling and Hover Effects
To turn an image into a link in HTML, wrap the <img> tag inside an <a> tag. This makes the image clickable and it will redirect to another page with styling and hover effects.
Example: In this example, we will create a clickable image that opens a link in a new tab. We will also add some CSS to style the image and create a hover effect.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
text-align: center;
background-color: #f4f4f4;
}
h3 {
color: #333;
}
a {
text-decoration: none;
}
#logo {
width: 250px;
height: auto;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease-in-out;
}
#logo:hover {
transform: scale(1.2);
}
</style>
</head>
<body>
<h3>
Click on the styled logo
to visit GeeksforGeeks
</h3>
<!-- Image as a Link -->
<a href="https://geeksforgeeks.org" target="_blank"
rel="noopener noreferrer">
<img id="logo" src=
"https://media.geeksforgeeks.org/gfg-gg-logo.svg"
alt="GeeksforGeeks Logo">
</a>
</body>
</html>
Output