Skip to content
beginnerPhase 30 · HTML

Links and Navigation

Master anchor elements, relative vs absolute URLs, and navigation patterns.

30m
0 problems
Topic Progress0%

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>

Practice Problems

0/3solved
Build Links and Navigation Component

Create a reusable React component implementing Links and Navigation. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Links and Navigation Testing

Write unit and integration tests for Links and Navigation using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Links and Navigation Performance

Optimize Links and Navigation for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What does rel="noopener noreferrer" do?

Question 1 options

2. Which target value opens a link in a new tab?

Question 2 options

3. What is a skip link?

Question 3 options

4. What does the download attribute do?

Question 4 options

Flashcards

Question

What does the href attribute do?

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"?

Answer

Prevents the new page from accessing window.opener, protecting against phishing attacks.

Question

What is a skip link?

Answer

A hidden link at the top of the page that becomes visible on focus, allowing keyboard users to skip to main content.

Question

What is Links and Navigation?

Answer

Links and Navigation is a key concept in frontend development.

Question

When to use Links and Navigation?

Answer

Use Links and Navigation when building production systems that require reliability, scalability, and maintainability.

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