Skip to content

Image as Hyperlinks

In HTML, you can use images as hyperlinks by embedding an <img> tag inside an <a> (anchor) tag. This allows the image to act as a clickable link, just like text links. This technique is commonly used for creating navigation buttons, banners, or clickable icons on web pages.

Syntax

To create an image hyperlink, you wrap the <img> tag with an <a> tag. The href attribute of the <a> tag specifies the destination URL.

<a href=”URL”>

    <img src=”image_source” alt=”description”>

</a>

Example

Here’s a step-by-step example to illustrate how you can use an image as a hyperlink:

1. Basic Image Hyperlink

In this example, we will create a hyperlink using an image that links to another websit

<!DOCTYPE html>

<html>

<head>

    <title>Image as Hyperlink Example</title>

</head>

<body>

    <p>Click the image below to visit drbtaneja.com:</p>

    <a href=”https://www.drbtaneja.com”>

        <img src=”https://drbtaneja.com” alt=”Example Website”>

    </a>

</body>

</html>

  • <a href=”https://www.drbtaneja.com”>: This creates a hyperlink that points to https://www.drbtaneja.com.
  • <img src=”drbtaneja.com/xyz.jpg” alt=”Example Website”>: This specifies the image to display. The src attribute contains the URL of the image, and the alt attribute provides alternative text if the image cannot be displayed.

2. Image Hyperlink with CSS Styling

You can also style the image using CSS to make it more visually appealing.

<!DOCTYPE html>

<html>

<head>

    <title>Styled Image as Hyperlink Example</title>

    <style>

        .link-image {

            border: 2px solid #000;

            border-radius: 8px;

            width: 150px;

            height: auto;

        }

    </style>

</head>

<body>

    <p>Click the image below to visit Example.com:</p>

    <a href=”https://www.example.com”>

        <img src=”https://via.placeholder.com/150″ alt=”Example Website” class=”link-image”>

    </a>

</body>

</html>

  • CSS Styling:
    • .link-image: This CSS class adds a border, border radius, and sets the width of the image.
    • border: 2px solid #000;: Adds a solid black border around the image.
    • border-radius: 8px;: Rounds the corners of the image.
    • width: 150px; height: auto;: Sets the width of the image to 150 pixels and adjusts the height automatically to maintain the aspect ratio.

Important Considerations

Accessibility: Always use the alt attribute to provide descriptive text for the image. This helps screen readers and improves accessibility for visually impaired users.

Responsive Design: Ensure that the image scales appropriately on different devices and screen sizes. You can use CSS or HTML attributes to make the image responsive.

SEO: Search engines use the alt attribute to understand the content of the image, so make sure it is descriptive and relevant.

By using images as hyperlinks, you can create visually engaging and interactive elements on your web pages that enhance user experience and navigation.