DEV Community

Cover image for Adding Links in HTML: Internal and External Anchors
Sharique Siddiqui
Sharique Siddiqui

Posted on

Adding Links in HTML: Internal and External Anchors

Hyperlinks are at the heart of web navigation—letting users jump from one place to another, whether it’s another point on your page, a different page on your website, or an entirely different site. In HTML, anchors (<a>) are used for all these link types. Let's dive into how you can add both internal and external links (anchors) to your web pages.

The <a> (Anchor) Tag: The Basics

The core syntax:

xml
<a href="URL">Link Text</a>
Enter fullscreen mode Exit fullscreen mode
  • href: Where the link goes.
  • The text between and is what users see and click.

Internal Links (Anchors)

Internal links help users navigate within your own website. They can point to:

  • Other pages on your site (e.g., from the homepage to the contact page)
  • Different sections within the same page (“in-page” or “jump” links)
1. Navigating to a Different Page on Your Site

Simply use a relative path in the href:

xml
<a href="/about.html">About Us</a>
Enter fullscreen mode Exit fullscreen mode

This link takes users to the about.html page within your domain.

2. Jumping to a Section Within the Same Page (Fragment Identifier)

This approach is great for long pages like documentation or FAQs.

How it works:
  • Add an id attribute to the target element (where you want the user to jump).
  • Use href="#id" in your anchor link.

Example: Table of Contents and Section Headers

xml
<!-- Table of Contents at the top -->
<ul>
  <li><a href="#features">Features</a></li>
  <li><a href="#pricing">Pricing</a></li>
  <li><a href="#faq">FAQ</a></li>
</ul>

<!-- Later in the page -->
<h2 id="features">Features</h2>
<p>Details about features...</p>

<h2 id="pricing">Pricing</h2>
<p>Details about pricing...</p>

<h2 id="faq">FAQ</h2>
<p>Answers to common questions...</p>
Enter fullscreen mode Exit fullscreen mode

Clicking “Features” takes you straight to that section by scrolling down.

3. Linking to Sections on Another Page

You can even link directly to a section on a different page (as long as the element on that page has the corresponding id).

xml
<a href="/about.html#team">Meet the Team</a>
Enter fullscreen mode Exit fullscreen mode

This will jump to the element with id="team" in about.html.

External Links

External links point to resources outside your domain—other websites, articles, or files.

Example:

xml
<a href="https://www.codencloud.com">Codencloud</a>
Enter fullscreen mode Exit fullscreen mode

Users clicking this link will be taken to codencloud.com.

Opening Links in a New Tab

For external links, it’s common to open them in a new tab, so users don’t lose their place on your site:

xml
<a href="https://www.wikipedia.org" target="_blank" rel="noopener noreferrer">Wikipedia</a>
Enter fullscreen mode Exit fullscreen mode
  • target="_blank": Opens in a new tab.
  • rel="noopener noreferrer": Improves security and performance when opening external sites.

Pro Tips for Effective Linking

  • Be descriptive: Use meaningful anchor text (“Read our pricing guide” instead of “Click here”).
  • Accessibility: Avoid ambiguous link text and ensure visited links are visually distinguishable.
  • Keep IDs unique: Each id on a page must be unique for in-page linking to work smoothly.
  • Relative vs. absolute URLs: Use relative paths for your own pages, absolute URLs for other domains.

Conclusion

  • Internal anchors boost navigation within or between your website’s pages.
  • External anchors connect your visitors to outside resources or references—use them thoughtfully!
  • Mastering the <a> tag unlocks your ability to create a truly connected and user-friendly site.

With just a few lines of HTML, you can turn static content into an interactive, interconnected experience!

Check out the YouTube Playlist for great HTML content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud

Top comments (0)