|$ curl https://forge-ai.dev/api/markdown?path=docs/html/links
$cat docs/html-links-&-anchors.md
updated This week·30 min read·published

HTML Links & Anchors

HTMLLinksIntermediateIntermediate
Introduction

Hyperlinks are the foundation of the web. The <a> element creates a clickable link to another resource — a web page, file, email address, phone number, or a specific location within the same document. The href attribute defines the destination, while target, rel, and download control how the link behaves.

Links are also critical for accessibility and SEO. Screen readers announce links and their destinations, search engines follow links to discover and rank content, and users rely on visual cues like link states to navigate confidently. Understanding every aspect of the anchor element is essential for building connected, navigable web experiences.

info

The word "anchor" comes from the element's original purpose: anchoring a location within a document that other links could target. The name attribute was the original way to create a fragment target — today we use id on any element.
Basic Anchor Syntax

The <a> element requires an href attribute to function as a link. Without it, the element renders as plain text (or a placeholder if styled). The target attribute controls where the linked page opens, and rel defines the relationship between the current page and the linked resource.

AttributeRequiredValuesDescription
hrefYesURL, fragment, protocolLink destination
targetNo_self | _blank | _parent | _topBrowsing context for navigation
relNonoopener | noreferrer | nofollow | sponsored | ugcRelationship to linked resource
downloadNofilenamePrompts download instead of navigation
hreflangNoBCP 47 language tagLanguage of the linked resource
typeNoMIME typeHint for the linked resource type
basic-anchor.html
HTML
1<!-- Basic link -->
2<a href="https://example.com">
3 Visit Example
4</a>
5
6<!-- Link opening in new tab with security attributes -->
7<a
8 href="https://example.com"
9 target="_blank"
10 rel="noopener noreferrer"
11>
12 Open in new tab (secure)
13</a>
14
15<!-- Styled as a button -->
16<a href="/signup" class="btn">
17 Sign Up
18</a>
19
20<!-- Link with title tooltip -->
21<a
22 href="https://developer.mozilla.org"
23 title="MDN Web Docs — comprehensive web development documentation"
24>
25 MDN Web Docs
26</a>
27
28<!-- Language hint -->
29<a
30 href="https://es.example.com"
31 hreflang="es"
32 lang="es"
33>
34 Documentación en Español
35</a>

Live preview of basic link examples:

preview
URL Types

The href attribute accepts multiple URL types. Understanding the difference between absolute, relative, and protocol-based URLs is essential for building links that work correctly in all contexts.

TypeExampleResolves To
Absolute URLhttps://example.com/pageA specific resource on the web
Relative (same directory)page.htmlRelative to current page directory
Relative (subdirectory)docs/page.htmlDescendant path
Relative (parent)../page.htmlParent directory traversal
Root-relative/docs/page.htmlRelative to site root
Fragment#section-idSame-page anchor
Mailtomailto:user@example.comOpens email client
Telephonetel:+1234567890Dials phone number (mobile)
SMSsms:+1234567890Opens SMS app (mobile)
JavaScript (avoid)javascript:void(0)Anti-pattern — use event handlers
url-types.html
HTML
1<!-- Absolute URL -->
2<a href="https://www.w3.org/TR/html52/">
3 HTML 5.2 Specification
4</a>
5
6<!-- Relative URL (same directory) -->
7<a href="about.html">About Us</a>
8
9<!-- Relative URL (subdirectory) -->
10<a href="docs/installation.html">Installation Guide</a>
11
12<!-- Relative URL (parent directory) -->
13<a href="../index.html">Back to Home</a>
14
15<!-- Root-relative URL -->
16<a href="/docs/html/links">Links Documentation</a>
17
18<!-- Fragment identifier (same page) -->
19<a href="#introduction">Jump to Introduction</a>
20
21<!-- Fragment identifier (different page) -->
22<a href="docs/installation.html#prerequisites">
23 Installation Prerequisites
24</a>
25
26<!-- Mailto link -->
27<a href="mailto:hello@example.com?subject=Inquiry&body=Hello,%20I%20have%20a%20question.">
28 Send Email
29</a>
30
31<!-- Telephone link -->
32<a href="tel:+1234567890">Call (123) 456-7890</a>
33
34<!-- SMS link -->
35<a href="sms:+1234567890?body=Hello">Send Text</a>

best practice

Always use HTTPS for absolute URLs. Many browsers now mark HTTP links as "Not Secure," which erodes user trust. Use root-relative paths (/page.html) for internal links — they work regardless of the current page depth and simplify site restructuring.
The Target Attribute

The target attribute controls where the linked resource opens. If omitted, _self is the default — the link opens in the same browsing context. The most commonly used alternative is _blank, which opens in a new tab or window.

ValueBehaviorUse Case
_selfOpens in current browsing contextDefault — most internal links
_blankOpens in a new tab or windowExternal links, downloadable files
_parentOpens in the parent frameFrameset / iframe navigation
_topOpens in the full window bodyBreaking out of a frameset
framenameOpens in a named frame or iframeLegacy — avoid in modern sites
target-attribute.html
HTML
1<!-- Default behavior (_self) — same tab -->
2<a href="/about">About Us (same tab)</a>
3
4<!-- New tab/window (_blank) — requires security attributes -->
5<a
6 href="https://external-site.com"
7 target="_blank"
8 rel="noopener noreferrer"
9>
10 External Resource (new tab)
11</a>
12
13<!-- Parent frame navigation -->
14<a href="/full-page" target="_parent">
15 Load in parent frame
16</a>
17
18<!-- Top-level navigation (break out of frames) -->
19<a href="/home" target="_top">
20 Back to full site
21</a>
22
23<!-- Named iframe target -->
24<iframe name="content-frame" src="default.html"></iframe>
25<a href="page.html" target="content-frame">
26 Load in iframe
27</a>

warning

Using target="_blank" without rel="noopener noreferrer" is a security risk. The opened page can access window.opener and redirect the original tab to a phishing site (tabnapping). Always include rel="noopener noreferrer" when using _blank.
Rel Values

The rel attribute defines the relationship between the current page and the linked resource. It serves dual purposes: security (preventing tabnapping) and SEO (communicating link intent to search engines). Multiple values can be space-separated.

ValuePurposeRequired With _blank
noopenerPrevents access to window.openerYes
noreferrerHides the Referer header + implies noopenerYes
nofollowTells search engines not to pass ranking creditNo
sponsoredLinks created as part of advertisements or sponsorshipsNo
ugcUser-generated content links (comments, forum posts)No
externalIndicates the linked page is externalNo
prev / nextPagination — previous and next page in a seriesNo
canonicalDeclares the canonical/preferred URL for a pageNo
rel-values.html
HTML
1<!-- External link with full security attributes -->
2<a
3 href="https://partner-site.com"
4 target="_blank"
5 rel="noopener noreferrer"
6>
7 Partner Website
8</a>
9
10<!-- User-generated content link -->
11<a
12 href="https://spammy-site.com"
13 rel="nofollow ugc noopener"
14 target="_blank"
15>
16 Commenter's Link
17</a>
18
19<!-- Sponsored/advertorial link -->
20<a
21 href="https://sponsor.com"
22 rel="sponsored noopener noreferrer"
23 target="_blank"
24>
25 Sponsored Content
26</a>
27
28<!-- Pagination links -->
29<a href="/page/2" rel="prev">Previous Page</a>
30<a href="/page/4" rel="next">Next Page</a>
31
32<!-- External link icon indicator -->
33<a
34 href="https://external-site.com"
35 target="_blank"
36 rel="noopener noreferrer"
37 class="external-link"
38>
39 Visit External Site
40 <span class="external-icon" aria-hidden="true">↗</span>
41</a>

best practice

Modern SEO best practices: use rel="sponsored" for paid links, rel="ugc" for user-generated content, and rel="nofollow" for untrusted or crawled links. Google treats sponsored and ugc similarly to nofollow but with more specific semantics.
Fragment Identifiers

Fragment identifiers (also called anchor links or hash links) link to a specific section within a page. The fragment is the part of the URL after the # symbol and corresponds to the id attribute of the target element. When clicked, the browser scrolls the target element into view.

fragment-identifiers.html
HTML
1<!-- Same-page fragment links -->
2<nav aria-label="Table of Contents">
3 <ul>
4 <li><a href="#introduction">Introduction</a></li>
5 <li><a href="#features">Features</a></li>
6 <li><a href="#pricing">Pricing</a></li>
7 <li><a href="#contact">Contact</a></li>
8 </ul>
9</nav>
10
11<!-- Target sections with matching IDs -->
12<section id="introduction">
13 <h2>Introduction</h2>
14 <p>Content here...</p>
15</section>
16
17<section id="features">
18 <h2>Features</h2>
19 <p>Content here...</p>
20</section>
21
22<section id="pricing">
23 <h2>Pricing</h2>
24 <p>Content here...</p>
25</section>
26
27<section id="contact">
28 <h2>Contact</h2>
29 <p>Content here...</p>
30</section>
31
32<!-- Fragment link to another page -->
33<a href="/docs/html/links#fragment-identifiers">
34 Learn about fragment identifiers
35</a>
36
37<!-- Fragment with smooth scrolling -->
38<style>
39 html {
40 scroll-behavior: smooth;
41 }
42
43 /* Offset for fixed headers */
44 section[id] {
45 scroll-margin-top: 80px;
46 }
47</style>
48
49<!-- Back to top link -->
50<a href="#">Back to Top</a>
51
52<!-- Breadcrumb with fragment -->
53<nav aria-label="Breadcrumb">
54 <ol>
55 <li><a href="/docs">Docs</a></li>
56 <li><a href="/docs/html">HTML</a></li>
57 <li><a href="/docs/html/links#introduction">Links</a></li>
58 </ol>
59</nav>
🔥

pro tip

Use scroll-margin-top on fragment target elements to account for fixed headers. Without this offset, the browser scrolls the target to the very top of the viewport, where it may be hidden behind a sticky navigation bar. The scroll-behavior: smooth CSS property provides native smooth scrolling.
Accessibility

Accessible links are critical for keyboard and screen reader users. Link text must be descriptive, focus indicators must be visible, and navigation patterns like skip links must be available. The Web Content Accessibility Guidelines (WCAG) specify several success criteria for links.

TechniqueWCAG CriterionImplementation
Descriptive link text2.4.4 Link Purpose (In Context)Never use "click here" — describe the destination
Focus indicator2.4.7 Focus VisibleAlways provide visible focus styles
Skip link2.4.1 Bypass BlocksFirst focusable element on the page
New window warning3.2.5 Change on RequestIndicate when links open in a new window
ARIA labels4.1.2 Name, Role, ValueUse aria-label when link text alone is insufficient
Adjacent links2.4.4 Link PurposeCombine identical adjacent links into one
link-accessibility.html
HTML
1<!-- Skip link: first focusable element -->
2<a href="#main-content" class="skip-link">
3 Skip to main content
4</a>
5
6<style>
7 .skip-link {
8 position: absolute;
9 top: -100%;
10 left: 8px;
11 background: #000;
12 color: #fff;
13 padding: 8px 16px;
14 z-index: 10000;
15 }
16
17 .skip-link:focus {
18 top: 8px;
19 }
20</style>
21
22<!-- Descriptive link text — good -->
23<a href="/docs/installation">
24 Read the installation guide for Linux systems
25</a>
26
27<!-- Descriptive link text — bad -->
28<a href="/docs/installation">Click here</a>
29to read the installation guide.
30
31<!-- Screen reader only text for additional context -->
32<a href="/reports/q1-2026.pdf">
33 Q1 2026 Financial Report
34 <span class="sr-only">(PDF, 2.4 MB)</span>
35</a>
36
37<!-- External link with new window warning -->
38<a
39 href="https://external-site.com"
40 target="_blank"
41 rel="noopener noreferrer"
42>
43 Visit Partner Site
44 <span class="sr-only">(opens in new tab)</span>
45 <span aria-hidden="true">↗</span>
46</a>
47
48<!-- Icon-only link with aria-label -->
49<a
50 href="/profile"
51 aria-label="View your profile"
52>
53 <svg aria-hidden="true" width="24" height="24">...</svg>
54</a>
55
56<!-- Multiple identical links combined -->
57<!-- Instead of: -->
58<div class="card">
59 <a href="/product/123">Product Name</a>
60 <p>Description...</p>
61 <a href="/product/123">View Details</a>
62</div>
63
64<!-- Do this: -->
65<article>
66 <a href="/product/123">
67 <h3>Product Name</h3>
68 <p>Description...</p>
69 <span>View Details →</span>
70 </a>
71</article>

Live preview of accessible link patterns:

preview

best practice

Skip links are the single most impactful accessibility improvement for keyboard users. Place a Skip to main content link as the first focusable element on every page. When focused, it should become visible (typically positioned at the top of the page) and link to the #main-content element.
Best Practices

Security: The _blank Rule

The most important security rule for links is: always pair target="_blank" with rel="noopener noreferrer". Without this, the opened page gains partial access to the original page via window.opener and can redirect it to a phishing site (a technique called tabnapping). Modern browsers default <a> with _blank to rel="noopener", but explicitly including it ensures protection everywhere.

Link Text Guidelines

Never use 'click here', 'read more', or 'link to' as link text — screen readers navigate by links alone and these provide no context
Link text should describe the destination: 'Download Q1 Report (PDF)' instead of 'Download'
Keep link text concise but unique — avoid identical link text pointing to different destinations on the same page
For download links, include the file type and size in the link text or via screen-reader-only text
For image links, the img alt attribute becomes the link text — describe the destination, not the image
Use aria-label on icon-only links to provide accessible names — the icon's visual meaning is not conveyed to screen readers
Avoid opening links in new tabs unless the user would lose their place (e.g., filling out a form)

SEO & Rel Values

Use rel='nofollow' for untrusted links or links you don't want to endorse (e.g., comment sections)
Use rel='sponsored' for paid or sponsored links to comply with Google's link guidelines
Use rel='ugc' for user-generated content like forum posts and blog comments
Use rel='prev' and rel='next' for paginated content to help search engines understand the series
Use rel='canonical' on the link element in <head> to declare the preferred URL for duplicate content
Avoid rel='noreferrer' alone on _blank links — pair it with noopener for maximum security

Live preview of a production-ready link implementation with all best practices:

preview
$Blueprint — Engineering Documentation·Section ID: HTML-18·Revision: 1.0