HTML – Image Links

By Published 2 min read

In HTML, you can create image links, which are clickable images that lead to other web pages or resources. This is a powerful way to make your website more visually engaging and interactive. Here’s how you can create image links:

  1. Basic Image Link:
    To create a basic image link, you can use the <a> (anchor) element and wrap an <img> (image) element within it. Here’s an example:
<!DOCTYPE html>
<html>
<head>
    <title>Basic Image example</title>
</head>
<body>
    <a href="https://www.example.com">
        <img src="background.jpg" alt="Description of the image" width="400">
    </a>
</body>
</html>
Code language: HTML, XML (xml)

Output :

In this example, the <a> element surrounds the <img> element, making the image clickable. When a user clicks on the image, it will take them to the specified URL.

  1. Image Link with Hover Effects:
    You can add hover effects to your image links using CSS to provide visual feedback to users when they hover over the image. Here’s an example:
<!DOCTYPE html>
<html>
<head>
    <title>Image Link with Hover Effects</title>
</head>
<body>
    <style>
        .image-link{
            padding: 10px;
            border: 2px solid transparent;
            transition: border-color 0.3s;
            width: 400px;
        }
        .image-link:hover {
            border-color: #007bff; /* Change border color on hover */
        }
    </style>
    <div class="image-link">
        <a href="https://www.example.com" >
            <img src="background.jpg" alt="Description of the image" width="400">
        </a>
    </div>
</body>
</html>Code language: HTML, XML (xml)

Output :

In this example, when a user hovers over the image, the border color changes to blue, providing a visual cue that the image is clickable.

  1. Image Link with Custom Styling:
    You can customize the styling of image links further by applying CSS styles to them. This allows you to control the appearance, such as size, padding, and background.
<!DOCTYPE html>
<html>
<head>
    <title>Image Link with Hover Effects</title>
</head>
<body>
    <style>
        .image-link {
            display: block; /* Make the link a block element for better styling */
            text-align: center;
            background-color: #f0f0f0;
            padding: 10px;
        }
        .image-link img {
            max-width: 100%; /* Ensure the image fits within the container */
        }
    </style>
 
    <a href="https://www.example.com" class="image-link">
        <img src="background.jpg" alt="Description of the image" width="400">
    </a>
</body>
</html>
Code language: HTML, XML (xml)

Output :

This example styles the image link with a light gray background, centered text, and padding for better visual presentation.

Image links are a valuable addition to web design, as they combine the visual appeal of images with the functionality of hyperlinks. They can be used for a variety of purposes, including navigation, calls to action, and image galleries.

Khan Waseem is a tech lead and full-stack engineer in Bengaluru, building hospitality and SaaS software with Node.js, React and React Native. See the resume