HTML & JS Topic
HTML Links & Navigation Explained
Anchor Tags (<a>)
The foundation of web navigation in HTML:
html
<a href="https://example.com">Visit Example</a>
Key Attributes:
-
href: Specifies the destination URL (required) -
target: Controls where the link opens -
title: Tooltip text on hover -
rel: Relationship between current and linked document -
download: Forces download instead of navigation
Absolute vs Relative URLs
Absolute URLs
Complete web address including protocol, domain, and path:
html
<a href="https://www.example.com/products/item.html">Item</a>
<a href="http://api.example.com/data">API</a>
-
Always points to same location regardless of current page
-
Starts with
http://,https://,ftp://, etc.
Relative URLs
Path relative to current page location:
html
<!-- Current page: https://example.com/products/ -->
<a href="item.html">Item</a> <!-- Same folder -->
<a href="../contact.html">Contact</a> <!-- Parent folder -->
<a href="/about">About</a> <!-- Root-relative -->
<a href="#section-2">Jump to Section</a> <!-- Same page anchor -->
Common relative path syntax:
-
page.html→ Same directory -
./page.html→ Same directory (explicit) -
../page.html→ Parent directory -
/images/photo.jpg→ Root directory -
#contact→ Fragment on current page
Opening Links in New Tabs/Windows
target Attribute Options:
html
<!-- Opens in new tab/window (recommended for external links) -->
<a href="https://external.com" target="_blank">External Site</a>
<!-- Opens in same tab (default behavior) -->
<a href="page.html" target="_self">Same Tab</a>
<!-- Opens in parent frame -->
<a href="page.html" target="_parent">Parent Frame</a>
<!-- Opens in full body of window -->
<a href="page.html" target="_top">Top Window</a>
<!-- Opens in named window -->
<a href="page.html" target="myWindow">Named Window</a>
Best Practices with target="_blank"
Always add rel="noopener noreferrer" for security and performance:
html
<a href="https://external.com" target="_blank" rel="noopener noreferrer"> External Link </a>
Why this matters:
-
noopener: Prevents new page from accessingwindow.opener -
noreferrer: Holds referrer information -
Prevents security vulnerabilities and improves performance
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>Navigation Examples</title>
</head>
<body>
<h1>Link Examples</h1>
<!-- Internal navigation -->
<nav>
<a href="/">Home</a>
<a href="about.html">About (relative)</a>
<a href="/products/item.html">Product (root-relative)</a>
<a href="#contact">Jump to Contact (same page)</a>
</nav>
<!-- External links with best practices -->
<p>
Visit
<a href="https://developer.mozilla.org"
target="_blank"
rel="noopener noreferrer"
title="MDN Web Docs">
MDN Web Docs
</a> for more information.
</p>
<!-- Email and telephone links -->
<p>
<a href="mailto:contact@example.com">Email Us</a> |
<a href="tel:+1234567890">Call Us</a>
</p>
<!-- Download link -->
<a href="/files/document.pdf" download="guide.pdf">
Download Guide
</a>
<!-- Anchor target section -->
<section id="contact" style="margin-top: 500px;">
<h2>Contact Section</h2>
<p>This is the contact section you can jump to.</p>
</section>
</body>
</html>
Key Points to Remember
-
Accessibility: Use descriptive link text (not "click here")
-
External Links: Always use
target="_blank" rel="noopener noreferrer" -
Internal Navigation: Prefer relative URLs for portability
-
Visual Cues: Users expect underlined blue text for links
-
Email/Phone Links: Use
mailto:andtel:protocols appropriately
Common Use Cases
html
<!-- Navigation menu -->
<nav>
<a href="/" aria-current="page">Home</a>
<a href="/about">About</a>
<a href="/services">Services</a>
</nav>
<!-- Breadcrumbs -->
<div class="breadcrumbs">
<a href="/">Home</a> >
<a href="/products">Products</a> >
<span>Current Page</span>
</div>
<!-- Social media links -->
<a href="https://twitter.com/username" target="_blank">Twitter</a>
<a href="https://linkedin.com/in/username" target="_blank">LinkedIn</a>
Comments (0)
No comments yet. Be the first to comment!