Links and Anchors: Connecting Web Pages

Learn how to create hyperlinks using the `` tag and understand the concept of anchors for internal navigation within a page.


Links and Anchors: Connecting Web Pages

Absolute vs. Relative URLs

The href attribute can contain either an absolute or a relative URL.

  • Absolute URL: An absolute URL contains the full address of the resource, including the protocol (e.g., https://), domain name, and path. For example: https://www.example.com/about.html. Use absolute URLs when linking to resources on different websites.
  • Relative URL: A relative URL specifies the path to a resource relative to the current page's location. It's used for linking to pages within the same website. For example, if you have a file named contact.html in the same directory as your current page, you can link to it using: <a href="contact.html">Contact Us</a>. If `contact.html` is in a subdirectory called `pages`, you would use: <a href="pages/contact.html">Contact Us</a>. Use relative URLs when linking to resources within the same website, as they are more maintainable and less likely to break if the website's domain name changes.

What are Anchors?

Anchors allow you to create links that jump to specific sections within the same page. This is particularly useful for long pages with a table of contents or multiple sections. Instead of linking to another page, the link will scroll the user to a specific point on the current page.

Creating Anchors for Internal Navigation

To create an anchor, you need two things:

  1. The Anchor Point: This is the location on the page you want to link to. You define this using the id attribute on an HTML element. The id attribute must be unique within the page.
    <h2 id="section2">Section 2</h2>
  2. The Link to the Anchor: This is the hyperlink that points to the anchor point. You create this using the <a> tag, with the href attribute set to # followed by the id of the anchor point.
    <a href="#section2">Go to Section 2</a>

When the user clicks the "Go to Section 2" link, the browser will scroll to the element with the id="section2".

Practical Example

Here's a complete example demonstrating both external and internal links:

 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Links Example</title>
</head>
<body>

  <h1>My Webpage</h1>

  <nav>
    <ul>
      <li><a href="#section1">Go to Section 1</a></li>
      <li><a href="#section2">Go to Section 2</a></li>
      <li><a href="https://www.google.com" target="_blank">Visit Google</a></li>
    </ul>
  </nav>

  <section id="section1">
    <h2>Section 1</h2>
    <p>This is the content of Section 1.  Lorem ipsum dolor sit amet, consectetur adipiscing elit.  ... (more content here) ...</p>
  </section>

  <section id="section2">
    <h2>Section 2</h2>
    <p>This is the content of Section 2.  Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ... (more content here) ...</p>
  </section>

</body>
</html> 

This code creates a basic webpage with two sections and a navigation menu. The menu contains links to both sections within the page (using anchors) and to an external website (Google).