Home >Web Front-end >HTML Tutorial >Image Link in HTML

Image Link in HTML

PHPz
PHPzOriginal
2024-09-04 16:42:50927browse

Image Link In HTML is found in almost all pages as they help us navigate from one page to another on a website. A popular combination is to use the HTML anchor tag with HTML’s img tag . With this combination, we can allow the movement of users from one page to another by clicking on an image. Before we dive further into this topic, let’s first understand the working and rendering of anchor and image elements individually and then combine them to achieve a linked image.

HTML Anchor Tag

The HTML Anchor tag is used to create HTML hyperlinks to other web pages or multimedia content hosted on the web. Let’s refer to the syntax below to understand how anchor tags work and their basic attributes

Click here!!

In the above example, the “href” attribute specifies the URL of the webpage to which we would like to redirect the user while click on the text “Click here!!”.

Let us see the complete code below :

Image Link in HTML

Output 

–>

With the above example, you will be able to make the following observations

  1. An unvisited link will appear underlined and in blue color. For, e.g. This is an unvisited Link
  2. A visited link will appear underlined and in purple color. For, e.g. This is an already visited Link
  3. An active link appears underlined and in red color. For, e.g. This is an active Link

HTML Image Tag

While browsing the internet, I am sure you must have come across several web pages which have various forms of multimedia included in them. Especially images are a popular form of multimedia that can be found in almost all interactive web pages and websites today. Let us understand the image tag and its implementation in HTML with the example below:

Syntax

<img src="smiley.gif" alt="Smiley face" width="42" height="42">

Let us now understand how each of the attributes in the img tag work:

  1. src: The src attribute defines the image file path we are trying to load with this tag. It can be linked to an image hosted on the web with a format like example.com/images/dummy.png or an image file locally hosted on the same server as the web page.
  2. alt: The alt attribute defines the text and description of the image that we would like to display in case if the images fail to load due to network connectivity or any other issue.
  3. Width and height: The width and height of both attributes define the width and height of the image we’d like to display on the web page. Else the image would work with by default 100% height and width.

Now let’s see the complete HTML code required to load an image on a webpage. Save the following image by the name “sunset.png” in a folder called “image_test” in your local drive.

Image Link in HTML

Now in the same folder, let’s create an HTML file named sunset.html with the following HTML code :

Image Link in HTML

Now go to a browser on your machine and type in the path of the .html file. My files are stored in a D drive, so the path for me would be

D:/image_test/sunset.html

And now we can see that the rendered HTML page has loaded the sunset image on our browser. With the help of CSS and

tag, we can also display text as per our requirement around the image. Both the anchor and img tags are compatible with all browsers like Google Chrome, Safari, Microsoft Edge, Firefox and Internet Explorer.

Linked Images in HTML

Now that we have understood with examples how anchor tag and image tag work individually, let us now understand how we can combine the two functionalities to achieve a scenario where we would like the users to be redirected to a new webpage with the click of an image. To make an image clickable and redirect the user to another webpage, we simply need to nest the image in an anchor tag. In the below example, we will try to enlist the top 3 web search engines used across the globe. In our list, we will show the logos of the 3 search engines, and by clicking on any of the logos, the user will be redirected to the respective search engine page. Let us create a folder named “redirection test”, and in the same folder, let’s save the below images

1. Google

Image Link in HTML

2. Yahoo

Image Link in HTML

3. Bing

Image Link in HTML

Now we will create a .html by the name imageredirection.html in the same file. The imageredirection.html will contain the following code.

Image Link in HTML

Now we need to access the HTML page from the browser, for which I will type my local path “D:/redirectiontest/ imageredirection.html”. the browser will then render the HTML file to generate the following result:

–>

The users will be able to navigate to the respective search engine by clicking their logo. From the above example, we can observe that HTML is a simple and flexible language that allows us to combine multiple tags together and achieve a complex functionality like this. The combination of using img and anchor tag is a popular combination.With additional HTML coding we can also add different HTML elements like displaying linked images in an ordered or unordered list using

    or
      . The extreme flexibility and simplicity that HTML provides with each version released help UI and UX designers design interactive and intuitive web pages that we see while browsing the internet for day to day activities.

      Recommended Article

      This has been a guide to Image Link in HTML. Here we discuss the Different Types of HTML Tags Along with Syntax. You can also go through our other suggested articles to learn more –
      1. HTML Attributes
      2. HTML Format Tags
      3. HTML Image Tags
      4. HTML Frames

      The above is the detailed content of Image Link in HTML. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:HTML Hide ElementNext article:HTML Hide Element