How to Create a Hyperlink in HTML

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

The world wide web is a collection of interconnected web pages that are accessible through the internet. But have you wondered how are these web pages interconnected with each other?

This happens because of the presence of hyperlinks in HTML. Hyperlinks are a set of anchor tags that are used to point to another HTML file, text, audio, video, webpage, etc. Basically, a hyperlink is what makes a web.

Let's take an example of a popular webpage Wikipedia. As soon as we click on some of the hyperlinks present in the Wikipedia webpage, it redirects us to another specified webpage.

hyperlink-example

Hyperlinks allow us to link the documents to other documents as well as a link to specific parts of a document. Almost any content on the web can be converted to a link that when clicked goes to another webpage.

Syntax

The anchor tag <a>..</a> is used to define a hyperlink in HTML. The syntax to specify the links in HTML is as follows -

The href attribute is the most important attribute of the anchor tag as it contains the link to the destination.

The Text in the syntax is the text that is visible to the user as a link that when clicked takes the user to the destination webpage.

As we have seen above the syntax for creating a hyperlink in HTML. Now we will be discussing an example code for the same.

Output

create-hyperlink-in-html-output

Clicking on the text "Go to Google.com" takes us to Google’s home page which is linked in the href attribute inside the anchor tags.

The target attribute in HTML is used to tell the browser where to open the linked document when the link is clicked. However, by default, the linked documents open up in the current browser window but if you want to open it in another browser window or somewhere else then you need to specify the target attribute.

The target attribute has four main values that are specified as follows

ValueDescription
_selfIt is used to open the linked document in the same browser window or tab in which it was clicked. However, it is not necessary to externally specify this value as it is by default set to _self only.
_blankIt is used to open the linked document in a new browser window.
_parentIt is used for opening the linked document in the parent browser window.
_topIt is used to open the linked document in the full browser window.

Therefore, by using the target attribute inside the anchor tag, we can open the linked document in different browser windows as per our needs.

Absolute URLs vs. Relative URLs

There are two types of URLs that you will hear about while using the web. The two types are listed below -

  • Absolute URL - It is used to specify the full web address of the destination website in the href attribute. The full address contains the full path of the website along with the protocol and the domain address of the website.

    Let's say we have to open the index.html page in the site directory which has a domain name of example.com. Therefore, the absolute URL for the webpage would be https://www.example.com/site/index.html where HTTPS is the protocol for the website.

    Let's take an example of an absolute url in HTML.

    Output example-of-absolute-url-in-html Therefore, as you can see we have given the full URL to the href attribute.

  • Relative URL - It is used to specify a local link in HTML that is relative to the page that we are in. For example, we have an image in the site directory and we are already working in the site folder. Then, we just need to specify the name of the image say, "apple.jpg" in the href attribute because this image file is relative to the directory we are currently in.

    Let us understand the relative url with the help of the code.

    Output

    example-of-relative-url-in-html

    Therefore, in the relative url, we are not required to write the "https://www" part of the website.

More Examples

To use an image as a link in HTML, we will use the img tag inside the anchor tag in HTML. We will specify the address to the destination in the href attribute whereas the link the image in the src attribute of the img tag.

Let us see the code to use an image as a link in HTML.

Output

use-image-as-link-in-html-examples

Therefore, clicking on google's logo image in the above example would take us to google's home page.

We will use mailto: inside the href to link an email address to a link in HTML.

However, clicking on the linked text opens the user's email program to let him send an email.

Let us see the code to use an email address as a link in HTML.

Output

example-email-address-as-link

To use a button as a link, we have added some javascript in HTML. Javascript allows you to specify what things happen during certain events and one such event is the onclick event.

It is used inside the button tag and its value is specified to the location of the destination. Therefore, as soon as the user clicks on the button, it will take it to the location that is specified in the onclick event.

Let us see the code to use a button as a link in HTML.

Output

button-as-link-examples

The title attribute is used to show some extra information about the text. This extra information is often shown as a tooltip when the user hovers the mouse over the text.

Let us see the code the creating a link title in HTML.

Output

creating-link-title-examples

Creating a Navigation Menu

The navigation menu is a set of multiple webpages. These individual web pages are linked using the anchor tags.

Let us see the code for creating a navigation menu in HTML.

Output

creating-navigation-menu-in-html-example

Creating Bookmark Anchors

Bookmarks are marking the content that you are actually reading for your future reference. These bookmarks are really helpful if the website is long.

To create a bookmark, we first create a bookmark by adding an id to a section and then linking that id to the anchor tags so that clicking on the link will take the user to the specific section.

Let us see the code for creating bookmark anchors in HTML.

Output

creating-bookmark-anchors-example

And as soon as you click on the link “Go to Section 4”, you will be redirected to section 4 of the page.

creating-link-title-examples2

If you want to download a resource instead of opening it in a browser window then you should use the download attribute inside the anchor tags. The value of this download attribute can be a default save filename.

However, the link to the resource to be downloaded must be present in the href attribute of the anchor tag.

Let us see the code for creating a downloadable link using the anchor tag in HTML.

Output

creating-download-link-using-anchor-tag-example

In the above example, we are downloading the latest version of firefox for windows. Clicking on the link will start downloading the exe file for the same.

Best Practices

  • We should use the correct link wordings so that the purpose of the link is clear enough to make the user understand where it would take us to.
  • Do not use URLs as a part of your text as they look ugly.
  • Keep your link text as short as possible.

For more information about the best practices to use a hyperlink in HTML, you can visit Link best practices.

Learn More

To learn more about links, you can visit the article HTML Links.

Conclusion

  • Hyperlinks in HTML are used to jump from one document to another.
  • These hyperlinks are specified using the anchor tags <a></a> in HTML.
  • The most important attribute that is used with the anchor tag is the href which contains the destination address of the target webpage.
  • The target attribute is used to specify where to open the linked document in HTML.
  • The target attribute has four values - _self, _blank, _parent, and _top.
  • You can use the links as a button, email, and an image as well.