Using html href attribute in anchor tag

The href attribute of HTML

A few main points about href attribute:

  • The href is an attribute of the a tag.
  • The href is the most important attribute of hyperlink or <a> tag.
  • The href stands for Hypertext Reference. Where H is for Hypertext and Ref for the Reference.
  • In the href attribute, you specify the target page. The target page can be an internal link within the current web site or some external website.
  • You can even specify some other part of the same web page like this tutorial uses. On the top of this page under the “Quick Reach” section, you can see a few links that are linked to the same page by using “#”.

For example:

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

Where href specifies the path or URL where the user will be taken as “A href example” is clicked.

href online examples with HTML code

Following are a few example of using the href attribute in the anchor tag of HTML. You can see the code snippet as well.

A Basic HTML link

An HTML link with target as _blank

Example of HTML image link

A mailto link example

How to specify path in href attribute

There are two ways to specify the path in the HTML href attribute if your are linking to the current website’s page.

  1. The first way is to use the complete path or URL.
  2. The second option is to use the relative path. In this way, you do not need to specify from the “http://”.

See the examples to learn more about it.

Specify full path in HTML <a> href

The example below specifies full path in the href attribute:

Specify relative path for in href

The example below sets relative path in href:

You can see, we did not specify the root domain “https://www.tutorialscollection.com/” in that example. However, when you hover over the link and look at the status bar, you can see the full path from the root domain.

Note that, in case of external links you have to specify the full path in the href attribute of HTML.

Also see Tables of HTML | HTML tutorial

Was this article helpful?

Related Articles

Leave A Comment?