🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEhtml

html Documentation

LOADING ENGINE...

<a>

AI & DATA SCIENCE // a-tag

The <a> (anchor) element creates a hyperlink to another page, a location within the same page, an email address, or a file to download.

Syntax

<a href="https://example.com">Visit Example</a>

Deep Dive Course

**`<a>`** is the element that makes the web 'hyper' — its **`href`** attribute points to the destination: an absolute URL (`https://...`), a relative path (`/about`), an in-page anchor (`#section-id`), an email (`mailto:...`), or a phone number (`tel:...`). The **`target="_blank"`** attribute opens the link in a new tab, and when you use it, you should also add **`rel="noopener noreferrer"`** for security (it prevents the new page from gaining a reference back to your window via `window.opener`).

1Understanding <a>

`<a>` is the element that makes the web 'hyper' — its `href` attribute points to the destination: an absolute URL (https://...), a relative path (/about), an in-page anchor (#section-id), an email (mailto:...), or a phone number (tel:...). The `target="_blank"` attribute opens the link in a new tab, and when you use it, you should also add `rel="noopener noreferrer"` for security (it prevents the new page from gaining a reference back to your window via window.opener).

💡

Always pair target="_blank" with rel="noopener noreferrer" — without it, the page you link to can access window.opener and potentially redirect your original tab to a phishing page (a real, exploitable vulnerability called 'tabnabbing').

editor.html
<a href="/reference/html">Browse the HTML reference</a>
<a href="#section-2">Jump to Section 2</a>
localhost:3000

2Practical Example

Here is a real-world application of <a> showing how it is used in production HTML.

editor.html
<!-- Safely opening an external link in a new tab -->
<a href="https://external-site.com" target="_blank" rel="noopener noreferrer">
  External Site
</a>
<a href="mailto:hello@example.com">Email us</a>
localhost:3000

3Best Practices

Follow these guidelines when working with <a>:

1. Use descriptive link text (not 'click here') for accessibility and SEO

2. Add rel="noopener noreferrer" whenever you use target="_blank"

3. Use mailto: and tel: links for email addresses and phone numbers so they're actionable on mobile

⚠️

Tip: Always pair target="_blank" with rel="noopener noreferrer" — without it, the page you link to can access window.opener and potentially redirect your original tab to a phishing page (a real, exploitable vulnerability called 'tabnabbing').

editor.html
<a href="/reference/html">Browse the HTML reference</a>
<a href="#section-2">Jump to Section 2</a>
localhost:3000

Examples

Example 01Basic Usage
<a href="/reference/html">Browse the HTML reference</a>
<a href="#section-2">Jump to Section 2</a>
Example 02Advanced Example
<!-- Safely opening an external link in a new tab -->
<a href="https://external-site.com" target="_blank" rel="noopener noreferrer">
  External Site
</a>
<a href="mailto:hello@example.com">Email us</a>

Best Practices

  • Use descriptive link text (not 'click here') for accessibility and SEO
  • Add rel="noopener noreferrer" whenever you use target="_blank"
  • Use mailto: and tel: links for email addresses and phone numbers so they're actionable on mobile

Interview Question

Why should target="_blank" always be paired with rel="noopener"?

Hint: Think about the window.opener JavaScript reference the new tab gets.

When a link opens in a new tab via target="_blank", by default the new page gets a live JavaScript reference to the original tab via window.opener, and can use it to navigate the original tab to a different URL — even a malicious one — without the user noticing, since their focus is on the new tab. Adding rel="noopener" (bundled with noreferrer for broader browser support) severs that reference, closing this 'tabnabbing' security hole entirely, at no functional cost.

Exercises

EasyPractice using <a> in a real scenario.
View Solution
<a href="/reference/html">Browse the HTML reference</a>
<a href="#section-2">Jump to Section 2</a>

Frequently Asked Questions

Why should target="_blank" always be paired with rel="noopener"?

When a link opens in a new tab via target="_blank", by default the new page gets a live JavaScript reference to the original tab via window.opener, and can use it to navigate the original tab to a different URL — even a malicious one — without the user noticing, since their focus is on the new tab. Adding rel="noopener" (bundled with noreferrer for broader browser support) severs that reference, closing this 'tabnabbing' security hole entirely, at no functional cost.

Related Functions

link-tagbutton-tag