Home >Web Front-end >HTML Tutorial >Image Link in HTML
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.
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
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 :
Output
–>
With the above example, you will be able to make the following observations
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:
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.
Now in the same folder, let’s create an HTML file named sunset.html with the following HTML code :
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.
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
2. Yahoo
3. Bing
Now we will create a .html by the name imageredirection.html in the same file. The imageredirection.html will contain the following code.
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
The above is the detailed content of Image Link in HTML. For more information, please follow other related articles on the PHP Chinese website!