English العربية Português Japanese

HTML Links

Links (hyperlinks) are the threads that connect web pages — without links, the Internet wouldn't be a "web". With the <a> tag, you can navigate between pages, jump to specific sections within a page, download files, or send emails.

Basic Link Syntax

A link is defined by the <a> tag (anchor), where the href attribute (hypertext reference) specifies the destination address:

HTML
<a href="https://www.example.com">Visit Example Site</a>
▶ Try It Yourself

The code above renders as clickable text "Visit Example Site" in the browser, styled by default as blue text with an underline. When clicked, the browser navigates to the URL specified in href.

💡 Tip: Link text should clearly tell users what to expect after clicking. For example, "Click here" is less helpful than "View the complete user guide" — it's easier for users to decide and more search-engine friendly.

Opening Behavior: the target Attribute

By default, links open in the current page (replacing it). Use the target attribute to open in a new tab or window:

HTML
<a href="https://www.example.com" target="_blank">Open in New Tab</a>

<a href="/html/index.html" target="_self">Open in Current Page (Default)</a>
▶ Try It Yourself

Common target values:

⚠️ Security Note: When using target="_blank", it's recommended to add rel="noopener" or rel="noreferrer" to the link. This prevents the new page from accessing the original page's object via window.opener, avoiding potential security risks: <a href="..." target="_blank" rel="noopener">Link</a>.

Absolute vs. Relative Paths

Link destinations can be absolute paths or relative paths. The difference is simple — absolute paths include the full URL, while relative paths specify a location relative to the current page:

HTML
<!-- Absolute path: full URL -->
<a href="https://www.google.com">Visit Google</a>

<!-- Relative path: relative to current file -->
<a href="basics.html">HTML Basics</a>

<!-- Relative path: parent directory -->
<a href="../index.html">Back to Home</a>
▶ Try It Yourself

Anchor Links: Jump Within a Page

Anchor links let you jump directly to a specific section on the same page — perfect for long pages with "Back to Top" or table-of-contents navigation. It takes just two steps: assign an id to the target element, then link to it with #id.

HTML
<!-- Table of contents -->
<a href="#section-intro">Jump to Introduction</a>
<a href="#section-details">Jump to Details</a>

<!-- Target sections (with height set to visualise the jump) -->
<h2 id="section-intro">Introduction</h2>
<p style="min-height:600px;background:#f0f0f0;">Introduction content here...</p>

<h2 id="section-details">Details</h2>
<p style="min-height:600px;background:#f0f0f0;">Detailed information here...</p>
▶ Try It Yourself

Anchors also support cross-page jumps — just specify both the page and anchor in the link, e.g. <a href="basics.html#section-head">View Header Structure</a>. The browser will open that page and automatically scroll to the specified position.

Image Links & More Uses

Links aren't limited to text — put an <img> tag inside an <a> tag and the image becomes clickable. The <a> tag can also be used to download files and send emails:

HTML
<!-- Image link -->
<a href="https://www.example.com">
  <img src="banner.jpg" alt="Click for details">
</a>

<!-- Download link -->
<a href="document.pdf" download>Download PDF Document</a>

<!-- Email link -->
<a href="mailto:support@example.com">Send Us an Email</a>
▶ Try It Yourself
📌 Note: The <a> tag also has a commonly-used title attribute that displays tooltip text on hover: <a href="..." title="Click to view full tutorial">HTML Tutorial</a>. This tooltip is also friendly to assistive technologies like screen readers.

📖 Summary

📝 Exercises

  1. Create a link: Write a link to https://www.google.com that opens in a new tab, with the link text "Search on Google".
  2. Anchor navigation: Create a "Back to Top" link at the bottom of a page that points to the top (hint: add an id to <body> or use # with a top-level element).
  3. Self-review: Look at the source code of a page you've written, find all <a> tags, and examine how their href and target attributes are set — can you tell where each link will open?
100%