A hyperlink in HTML is a clickable element on a webpage that directs users to another resource, such as another page, a different location within the same page, or even an external file. It acts as a bridge, allowing navigation and interaction on the web.
Understanding the <a> Tag
The core of a hyperlink in HTML is the <a>
tag, also known as the anchor tag. This tag is used to create the link itself. The most vital attribute within this tag is href
, which specifies the destination of the link.
Key Attributes
Attribute | Description | Example |
---|---|---|
href |
Specifies the URL of the page the link goes to. | <a href="https://www.example.com">Visit Example</a> |
target |
Specifies where to open the linked document. Common values include _blank (new tab), _self (same tab, default), _parent , and _top . |
<a href="https://www.example.com" target="_blank">Visit Example</a> |
How Links Appear by Default
As stated in the reference, by default, web browsers typically display links in a distinctive way:
- An unvisited link is underlined and blue.
- A visited link is underlined and purple.
- An active link (when you click on it) is underlined and red.
These styles can be changed using CSS.
Types of Links
Hyperlinks are not just for external websites; they have other uses:
- External Links: Linking to other websites on the internet.
- Example:
<a href="https://www.google.com">Visit Google</a>
- Example:
- Internal Links: Linking to other pages within the same website.
- Example:
<a href="about.html">About Us</a>
- Example:
- Anchor Links: Linking to a specific part of the same page.
- Example:
<a href="#section2">Go to Section 2</a>
- Example:
- Email Links: Creating a link that opens the user's email client.
- Example:
<a href="mailto:[email protected]">Email Us</a>
- Example:
- Download Links: Linking to downloadable files.
- Example:
<a href="document.pdf" download>Download PDF</a>
- Example:
Practical Insights
- User Experience: Use clear and concise link text. Avoid "click here" and instead use descriptive text that explains where the link will lead the user.
- SEO: Descriptive and keyword-rich link text can also help with search engine optimization.
- Accessibility: Make sure your links are easily distinguishable, especially for users with visual impairments. CSS can be used to adjust their visual appearance.
Example of a Basic Link
<a href="https://www.example.com">This is a link to Example.com</a>
This code creates a link that says "This is a link to Example.com" and, when clicked, takes the user to https://www.example.com
.