HTML links, or hyperlinks, are elements in a web page that users can click on to navigate to another web page or resource. They are created using the <a> (anchor) tag in HTML. Here is a basic example:
<a href="https://www.abc.com">Visit Example</a>
In this example:
- The <a> tag defines the link.
- The href attribute specifies the URL of the page the link goes to.
- The text between the opening and closing <a> tags (“Visit Example”) is the clickable part of the link.
Attributes of the <a> Tag
- href: Specifies the URL of the page the link goes to.
- target: Specifies where to open the linked document (e.g.,
_blank for a new tab/window,_self for the same frame,_parent for the parent frame,_top for the full body of the window). - title: Provides additional information about the link, often shown as a tooltip when the mouse hovers over the link.
- rel: Specifies the relationship between the current document and the linked document (e.g., nofollow, noopener, noreferrer).
Example with more Attributes
<a href="https://www.abc.com" target="_blank" title="Visit Example" rel="noopener noreferrer">Visit Example</a>
Types of Links
- Absolute URLs: Full web addresses (e.g.,
https://www.abc.com/page1.html). - Relative URLs: Links relative to the current page (e.g.,
page2.html,../images/pic.jpg). - Email Links: mailto: followed by an email address (e.g.,
mailto:someone@example.com)
Linking to a Specific Part of a Page
You can also link to a specific part of the same or another page using an anchor. This is done by setting an id on the target element and linking to it with a # prefix:
<!-- Target element with an id -->
<h2 id="section1">Section 1</h2>
<!-- Link to the target element -->
<a href="#section1">Go to Section 1</a>
External Links vs. Internal Links
External Links: Point to a different website (e.g., https://www.google.com).
Internal Links: Point to a different page on the same website (e.g., about.html).
HTML links – Interview Questions
Q 1: Which tag is used for links?
Ans: <a> tag.
Q 2: Which attribute defines the URL?
Ans: href.
Q 3: How to open link in a new tab?
Ans: Using target="_blank".
Q 4: What is an internal link?
Ans: A link within the same website.
Q 5: What is an external link?
Ans: A link to another website.
HTML links – Objective Questions (MCQs)
Q1. Which tag is used to create a hyperlink in HTML?
Q2. Which attribute specifies the URL in a hyperlink?
Q3. How do you open a link in a new tab?
Q4. How do you create an email link in HTML?
Q5. Which attribute can be used to provide a tooltip for a link?