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.

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

Example

HTML
<a href="https://web-tutorial.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:

Example

HTML
<a href="https://web-tutorial.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:

Example

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 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.

Example

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.

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:

Example

HTML
<!-- Image link -->
<a href="https://www.example.com">
<img src="/assets/images/tutorials/html-banner-example.webp" 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.

❓ FAQ

Q Why should I add rel="noopener" when using target="_blank"?
A This is a security measure. When a link opens in a new tab with target="_blank", the new page can access the original page's window object via window.opener, potentially allowing malicious pages to tamper with your content or perform phishing attacks. Adding rel="noopener" severs this access, protecting your page. Add rel="noopener" or rel="noreferrer" to all target="_blank" links.
Q What's the difference between absolute and relative paths? When should I use each?
A Absolute paths contain full URLs (like https://example.com/page.html) and point to external resources or when you need explicit domain specification. Relative paths use positions relative to the current file (like ./about.html or ../index.html) for same-site navigation. Root paths (like /html/index.html) start from the site root, ideal for large websites. Rule of thumb: relative paths for internal links, absolute paths for external links.
Q My anchor link isn't working. What should I check?
A Common causes: 1) The target element doesn't have an id attribute; 2) The id value is misspelled (case-sensitive); 3) The id isn't unique on the page (browser jumps to the first match); 4) The target element is hidden with display:none. Debug method: In the browser console, type document.getElementById("yourId") — if it returns null, the id doesn't exist.

📖 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%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏