Href Tag in HTMLWhat is the href Attribute?In HTML, with the help of an anchor tag(<a> tag), we can create a hyperlink between one address and another. For the functionality of the <a> tag, the href attribute must be present inside the <a> tag. The main purpose of the href tag is to indicate the destination address of the hyperlink. Without the href attribute, the <a> tag cannot work. For a better understanding, looks at the below snippet. In the above snippet, there are several things that we have to notice. These are as below.
In the above snipped, the additional attributes target= "_blank" and rel= "noopener." This means the link will be opened in the browser's new tab. These attributes are not necessary as compared to the href attribute. What are Href Values?We always see that the link inside the href attribute is opened in the same or different web browser tab. We can also do different things with the help of href attributes. With the help of the href attribute, we can link a different part of the same web page or serve a protocol other than HTTPS. Let's understand the other possible value with some examples. Absolute URLWith the help of this value, we can target the different domain names on the current webpage. Let's understand this with the help of the following example. Example 1:Output: Relative URLWith the help of this value, we can set the path of the reference file on the same webpage. Example 2:Code for signup page Code for login page Output: When the user clicks on the login, the below page will be opened. The relative path does not contain the HTTPs protocol or the domain name. When the URL is absent in the path, the browser thinks it is a path for the specified file. We call this path a relative path because the link of the destination is defined relative to the current web page. URI FragmentThe URI fragment is a different part of the current web page. It starts with the symbol "#" and then contains the desired page element. We can also write the anchor tag for any portion of the HTML code. Additional ProtocolsThe relative and absolute paths use the HTTP or HTTPS protocols. We can also use other protocols with href attributes like mailto: and file: Let's understand this with the help of the below example. Example 3:Output: When the user clicks on the login, the below page will be opened. Explanation: In the above code, when the user clicks on the send mail, the mail app will be opened automatically. Next TopicHTML Beautifier |