Skip to main content

Html Anchor Tag

HTML Anchor Tag | Rustcode

HTML Anchor Tag

The Anchor Tag

The HTML Anchor Tag, represented by <a>, is one of the most fundamental elements on the web. It's used to create hyperlinks, which connect one web page to another, or to different sections within the same page. Without anchor tags, the web as we know it—a vast interconnected network of information—would not exist.

Key Attributes of <a>:

  • href (Hypertext Reference): This is the most crucial attribute. It specifies the URL (Uniform Resource Locator) of the page or resource the link goes to. It can be an absolute URL (e.g., https://www.example.com) or a relative URL (e.g., /about-us.html).
  • target: Specifies where to open the linked document. Common values include:
    • _self (default): Opens the linked document in the same frame/window.
    • _blank: Opens the linked document in a new window or tab.
    • _parent: Opens the linked document in the parent frame.
    • _top: Opens the linked document in the full body of the window.
  • title: Provides extra information about the element, which is typically displayed as a tooltip when the user hovers over the link.
  • download: Prompts the user to download the linked URL instead of navigating to it. You can optionally provide a filename as its value.
  • rel (Relationship): Specifies the relationship between the current document and the linked document. Useful for SEO and security (e.g., rel="noopener noreferrer" for target="_blank" links).

Practical Examples

Basic External Link

Link to an external website. This is the most common use of the anchor tag.

<a href="https://www.rustcode.com">Visit Rustcode</a>

Opening Link in New Tab

Use target="_blank" to open the linked page in a new browser tab or window, preventing the user from leaving your current page.

<a href="https://www.google.com" target="_blank" rel="noopener noreferrer">Search on Google (New Tab)</a>

Security Tip: When using target="_blank", always add rel="noopener noreferrer". This prevents security vulnerabilities like tabnabbing, where the new page can gain partial access to your original page.

Link with Tooltip (Title Attribute)

The title attribute provides a descriptive text that appears as a tooltip when the user hovers over the link.

<a href="https://www.wikipedia.org" title="Go to Wikipedia for information">Wikipedia (Hover Me)</a>

Email Link (mailto:)

Create a link that opens the user's default email client, pre-filling the recipient's address.

<a href="mailto:info@rustcode.com?subject=Inquiry from Website&body=Hello Rustcode team,">Send Email to Rustcode</a>

Pro Tip: You can add parameters to mailto: links for subject, body, CC, and BCC. Use ?subject=YourSubject for the subject and &body=YourMessage for the body.

Phone Call Link (tel:)

Create a clickable phone number. On mobile devices, this will prompt the user to dial the number. On desktop, it might open a softphone application.

<a href="tel:+919876543210">Call Us: +91 98765 43210</a>

Link to a Section on the Same Page

Use an anchor tag with an href="#id-of-element" to create navigation that scrolls to a specific part of the current page. The target element must have a matching id attribute.

<a href="#rc-section-target">Go to The Target Section Below</a>
<!-- Later in the page -->
<div id="rc-section-target">...</div>

Note: When linking to a section, the browser will automatically scroll to the element with the matching ID. For smooth scrolling, you can add html { scroll-behavior: smooth; } to your CSS.

Download Link

The download attribute tells the browser to download the linked file instead of navigating to it. You can suggest a filename by providing a value to the attribute.

<a href="/path/to/document.pdf" download="MyDocument.pdf">Download PDF</a>

Important: For the `download` attribute to work, the `href` must point to a resource on the same origin, or the resource must have appropriate CORS (Cross-Origin Resource Sharing) headers configured if from a different origin.

You Scrolled to the Target Section!

This is the destination of the "Link to a Section" example.

Back to Top

Comments