Anchor Element
The \<a\> (anchor) element creates hyperlinks to other pages, resources, or locations.
Basic Link
<!-- External link -->
<a href="https://www.example.com">Visit Example</a>
<!-- Internal link -->
<a href="/about">About Us</a>
<!-- Email link -->
<a href="mailto:user@example.com">Send Email</a>
<!-- Phone link -->
<a href="tel:+15551234567">Call Us</a>
Link Attributes
| Attribute | Purpose | Example |
|---|---|---|
href |
Link destination | https://example.com |
target |
Where to open | _blank, _self, _parent, _top |
rel |
Relationship to linked resource | noopener, nofollow |
download |
Download instead of navigate | download="file.pdf" |
title |
Tooltip text | title="Visit site" |
Target Values
<!-- Same window (default) -->
<a href="/page" target="_self">Same Window</a>
<!-- New window/tab -->
<a href="https://example.com" target="_blank" rel="noopener noreferrer">
New Tab
</a>
<!-- Parent frame -->
<a href="/page" target="_parent">Parent Frame</a>
<!-- Full body -->
<a href="/page" target="_top">Full Body</a>
Security: rel="noopener noreferrer"
<!-- ❌ Dangerous: window.opener can redirect your page -->
<a href="https://evil.com" target="_blank">Evil Site</a>
<!-- ✅ Safe: prevents window.opener access -->
<a href="https://evil.com" target="_blank" rel="noopener noreferrer">
Safe External Link
</a>
Download Links
<!-- Force download -->
<a href="/files/report.pdf" download>Download Report</a>
<!-- Custom filename -->
<a href="/files/report.pdf" download="annual-report.pdf">
Download Report
</a>
<!-- Conditional download -->
<a href="/files/data.csv" download id="csv-link">Download CSV</a>
<script>
const link = document.getElementById('csv-link');
if (navigator.onLine) {
link.href = '/files/data.csv';
} else {
link.href = '/files/data-cached.csv';
}
</script>
Link Types
HTML supports different link relationships using the rel attribute.
External Resources
<!-- Stylesheet -->
<link rel="stylesheet" href="styles.css">
<!-- Favicon -->
<link rel="icon" href="/favicon.ico">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<!-- Manifest -->
<link rel="manifest" href="/manifest.json">
<!-- Preload (high priority) -->
<link rel="preload" href="/fonts/main.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/hero.jpg" as="image">
<!-- Prefetch (low priority) -->
<link rel="prefetch" href="/next-page.html">
<!-- DNS prefetch -->
<link rel="dns-prefetch" href="https://api.example.com">
SEO Links
<!-- Canonical URL -->
<link rel="canonical" href="https://www.example.com/page">
<!-- Alternate versions -->
<link rel="alternate" hreflang="es" href="https://www.example.com/es/page">
<link rel="alternate" type="application/rss+xml" href="/feed.xml">
<!-- Author -->
<link rel="author" href="https://example.com/author">
Navigation Patterns
<!-- Main navigation -->
<nav aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
<!-- Breadcrumbs -->
<nav aria-label="Breadcrumb">
<ol>
<li><a href="/">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/products/shoes" aria-current="page">Shoes</a></li>
</ol>
</nav>
<!-- Skip link (accessibility) -->
<a href="#main" class="skip-link">Skip to main content</a>
Anchor Links (In-Page)
<!-- Link to section -->
<a href="#section-id">Jump to Section</a>
<!-- Section with id -->
<section id="section-id">
<h2>Section Title</h2>
<p>Content...</p>
</section>
<!-- Back to top -->
<a href="#top">Back to Top</a>
<!-- Smooth scrolling -->
<style>
html {
scroll-behavior: smooth;
}
</style>
Quiz
1. What does rel="noopener noreferrer" do?
2. Which target value opens a link in a new tab?
3. What is a skip link?
4. What does the download attribute do?
Flashcards
Question
What does the href attribute do?
Click to reveal answer
Answer
Specifies the URL or path the link points to. Can be absolute (https://...) or relative (/page).
Question
Why use rel="noopener noreferrer" with target="_blank"?
Click to reveal answer
Answer
Prevents the new page from accessing window.opener, protecting against phishing attacks.
Question
What is a skip link?
Click to reveal answer
Answer
A hidden link at the top of the page that becomes visible on focus, allowing keyboard users to skip to main content.
Revision Notes
Key Takeaways
- 1. Always use rel="noopener noreferrer" with target="_blank"
- 2. Use descriptive link text for accessibility
- 3. Skip links help keyboard users navigate
- 4. Use mailto: for email links and tel: for phone links
- 5. Anchor links (#) navigate within the same page
Interview Tips
- • Know the security implications of target="_blank"
- • Understand when to use different link types
- • Be familiar with link accessibility best practices
- • Know how to create in-page anchor links
Cheat Sheet
Links and Navigation Cheat Sheet
Anchor Element:
Text
Target Values:
- _self: Same window (default)
- _blank: New tab/window
- _parent: Parent frame
- _top: Full body
Security:
- Always add rel="noopener noreferrer" with target="_blank"
Link Types:
- External: https://...
- Internal: /page
- Email: mailto:
- Phone: tel:
- Anchor: #section
Accessibility:
- Use descriptive link text
- Add skip links
- Use aria-current for active page