Skip to main content
Mediaweb Logo

Mediaweb

Html Guide

Links and Anchors in HTML: Creating Navigation and User-Friendly Connections

Master HTML links and anchors with this comprehensive guide. Learn about hyperlinks, absolute vs relative URLs, anchor links, and styling for usability.

September 3, 2025
11 min read
By Mediaweb Team
HTML
links
anchors
navigation
hyperlinks
URLs
usability

Absolute vs. Relative URLs

Understanding the difference between absolute and relative URLs is crucial for creating maintainable websites.

Absolute URLs

Absolute URLs include the complete web address:

<!-- Absolute URLs - Full web addresses -->
<a href="https://www.example.com">External site</a>
<a href="https://www.yoursite.com/about.html">Your about page</a>
<a href="http://www.oldsite.com">Non-secure site</a>

When to use absolute URLs:

  • Linking to external websites
  • Linking to resources on different domains
  • When you need to specify the protocol (http/https)
  • In email templates or RSS feeds

Advantages:

  • Work from any location
  • Clear and unambiguous
  • Work in any context

Disadvantages:

  • Longer and more verbose
  • Harder to move between domains
  • Can break if domain changes

Relative URLs

Relative URLs are paths relative to the current page's location:

<!-- Relative URLs - Paths from current location -->
<a href="about.html">About page (same directory)</a>
<a href="pages/contact.html">Contact page (subdirectory)</a>
<a href="../index.html">Home page (parent directory)</a>
<a href="../../images/logo.png">Logo (two levels up)</a>

Understanding Relative Path Notation

Current page: /blog/posts/article.html

about.html              → /blog/posts/about.html
../about.html           → /blog/about.html
../../about.html        → /about.html
pages/contact.html      → /blog/posts/pages/contact.html
/about.html             → /about.html (root-relative)

Path Symbols:

  • ./ - Current directory (optional)
  • ../ - Parent directory (go up one level)
  • / - Root directory (from site root)

Root-Relative URLs

Root-relative URLs start from the website's root directory:

<!-- Root-relative URLs - Start from site root -->
<a href="/about.html">About page</a>
<a href="/blog/posts/article.html">Blog article</a>
<a href="/images/logo.png">Site logo</a>

Benefits:

  • Work from any page on your site
  • Shorter than absolute URLs
  • Easy to move between development and production

Choosing the Right URL Type

Use Absolute URLs for:

  • External websites
  • Social media links
  • API endpoints
  • CDN resources

Use Relative URLs for:

  • Pages in the same directory
  • Closely related content
  • When file structure might change

Use Root-Relative URLs for:

  • Site-wide navigation
  • Assets like images and stylesheets
  • When you want consistency across all pages

Linking to Sections with Anchors

Anchor links allow users to jump to specific sections within the same page or other pages.

Step 1: Create the target with an ID

<h2 id="getting-started">Getting Started</h2>
<p>This section explains how to get started...</p>

<section id="advanced-topics">
    <h2>Advanced Topics</h2>
    <p>This section covers advanced concepts...</p>
</section>

Step 2: Link to the target

<!-- Link to section on same page -->
<a href="#getting-started">Jump to Getting Started</a>
<a href="#advanced-topics">Go to Advanced Topics</a>

<!-- Link to section on different page -->
<a href="tutorial.html#getting-started">Getting Started Tutorial</a>
<a href="/docs/api.html#authentication">API Authentication</a>

Table of Contents Example

<nav class="table-of-contents">
    <h2>Table of Contents</h2>
    <ul>
        <li><a href="#introduction">Introduction</a></li>
        <li><a href="#basic-concepts">Basic Concepts</a>
            <ul>
                <li><a href="#html-elements">HTML Elements</a></li>
                <li><a href="#attributes">Attributes</a></li>
            </ul>
        </li>
        <li><a href="#advanced-topics">Advanced Topics</a></li>
        <li><a href="#conclusion">Conclusion</a></li>
    </ul>
</nav>

<main>
    <section id="introduction">
        <h2>Introduction</h2>
        <p>Welcome to this comprehensive guide...</p>
    </section>
    
    <section id="basic-concepts">
        <h2>Basic Concepts</h2>
        <p>Let's start with the fundamentals...</p>
        
        <h3 id="html-elements">HTML Elements</h3>
        <p>HTML elements are the building blocks...</p>
        
        <h3 id="attributes">Attributes</h3>
        <p>Attributes provide additional information...</p>
    </section>
    
    <section id="advanced-topics">
        <h2>Advanced Topics</h2>
        <p>Now let's explore more complex concepts...</p>
    </section>
    
    <section id="conclusion">
        <h2>Conclusion</h2>
        <p>In this guide, we've covered...</p>
    </section>
</main>

Skip links help users with screen readers navigate more efficiently:

<body>
    <!-- Skip link - usually hidden visually -->
    <a href="#main-content" class="skip-link">Skip to main content</a>
    
    <header>
        <nav>
            <!-- Navigation menu -->
        </nav>
    </header>
    
    <main id="main-content">
        <!-- Main content starts here -->
        <h1>Page Title</h1>
        <p>Main content...</p>
    </main>
</body>

Smooth Scrolling with CSS

Enhance anchor links with smooth scrolling:

html {
    scroll-behavior: smooth;
}

/* Or target specific elements */
.smooth-scroll {
    scroll-behavior: smooth;
}

Practical Examples

<nav class="main-navigation" aria-label="Main navigation">
    <ul>
        <li><a href="/" aria-current="page">Home</a></li>
        <li><a href="/about">About</a></li>
        <li><a href="/services">Services</a>
            <ul class="submenu">
                <li><a href="/services/web-design">Web Design</a></li>
                <li><a href="/services/development">Development</a></li>
                <li><a href="/services/seo">SEO</a></li>
            </ul>
        </li>
        <li><a href="/blog">Blog</a></li>
        <li><a href="/contact">Contact</a></li>
    </ul>
</nav>
<nav aria-label="Breadcrumb">
    <ol class="breadcrumb">
        <li><a href="/">Home</a></li>
        <li><a href="/blog">Blog</a></li>
        <li><a href="/blog/html">HTML Tutorials</a></li>
        <li aria-current="page">Links and Anchors</li>
    </ol>
</nav>
<div class="social-links">
    <a href="https://twitter.com/yourhandle" 
       target="_blank" 
       rel="noopener noreferrer"
       aria-label="Follow us on Twitter">
        <svg><!-- Twitter icon --></svg>
        Twitter
    </a>
    
    <a href="https://facebook.com/yourpage" 
       target="_blank" 
       rel="noopener noreferrer"
       aria-label="Like us on Facebook">
        <svg><!-- Facebook icon --></svg>
        Facebook
    </a>
    
    <a href="https://linkedin.com/company/yourcompany" 
       target="_blank" 
       rel="noopener noreferrer"
       aria-label="Connect with us on LinkedIn">
        <svg><!-- LinkedIn icon --></svg>
        LinkedIn
    </a>
</div>
<section class="cta-section">
    <h2>Ready to Get Started?</h2>
    <p>Join thousands of satisfied customers who trust our services.</p>
    
    <div class="cta-buttons">
        <a href="/signup" class="button primary">Start Free Trial</a>
        <a href="/demo" class="button secondary">Watch Demo</a>
        <a href="/pricing" class="button outline">View Pricing</a>
    </div>
    
    <p class="small">
        Questions? <a href="/contact">Contact our sales team</a> or 
        <a href="tel:+1234567890">call (123) 456-7890</a>
    </p>
</section>

Key Takeaways

  • Use descriptive link text - Avoid "click here" and "read more"
  • Choose appropriate URL types - Absolute for external, relative for internal
  • Be selective with new tabs - Only use target="_blank" when necessary
  • Include security attributes - Always use rel="noopener noreferrer" with target="_blank"
  • Create accessible links - Provide context and proper focus indicators
  • Style for usability - Make links clearly identifiable and easy to interact with
  • Test your links - Ensure all links work and lead to the right destinations

Conclusion

Links are the connective tissue of the web, and creating effective links is essential for good user experience. Well-crafted links help users navigate your site, find information quickly, and take desired actions.

Remember that links serve both functional and communicative purposes. They should clearly indicate where they lead, be easy to identify and interact with, and provide a smooth, predictable experience for all users.

Whether you're creating simple navigation, complex anchor systems, or call-to-action buttons, always consider your users' needs and expectations. Good links are invisible when they work well - users can focus on their goals rather than figuring out how to navigate your site.

Practice creating different types of links, experiment with styling approaches, and always test your links with real users. The time you invest in creating thoughtful, accessible links will pay dividends in user satisfaction and site effectiveness.

Last updated: September 5, 2025

Related Articles

Continue reading with these related articles