C Programs Tutorials | IT Developer
IT Developer

Hyperlinks in HTML



Share with a Friend

Introduction

Hyperlinks are an essential part of the web, enabling users to navigate between different pages, sections, and resources. HTML provides the <a> tag (anchor tag) to create links. Links can be classified into absolute and relative links, and there are various attributes that can modify the behavior of a hyperlink.

In this article, we will cover the following aspects of hyperlinks:

  1. Absolute vs. Relative Links
  2. Anchor Tags (<a>)
  3. Link Attributes
  4. Anchor Links (Jumping within the page)
  5. Navigation Menus

1. Absolute vs. Relative Links

Absolute Links and Relative Links are two types of links used in HTML, each with its use case.

Absolute Links

  1. An absolute link is a complete URL, including the domain name and the protocol (e.g., https://).
  2. This type of link points to a specific location on the web, regardless of the current location of the page.

Example

<a href="https://www.example.com">Visit Example</a>

In this case, the link will always point to "https://www.example.com", no matter where the HTML page is located.

Relative Links

  1. A relative link points to a resource within the same website or directory.
  2. It is used when linking pages within the same website, making the link shorter and easier to maintain.

Example

<a href="about.html">About Us</a>

In this example, the link points to the about.html page, which should be in the same directory as the current page.


2. Anchor Tags (<a>)

The anchor tag (<a>) is used to create hyperlinks in HTML. The link can be used to navigate to a new page, a different part of the same page, or an external website.

Syntax

<a href="URL">Link Text</a>
  1. href: The attribute that specifies the URL or the destination of the link.
  2. Link Text: The clickable text that the user sees.

Example

<a href="https://www.google.com">Go to Google</a>

This example creates a link to Google. When users click on the text "Go to Google," they will be redirected to https://www.google.com.


3. Link Attributes

The <a> tag supports several attributes that can be used to modify the behavior and appearance of links.

Common Link Attributes

  1. href: Specifies the destination URL.
    1. Example: <a href="https://www.example.com">Link</a>
  2. target: Specifies where to open the linked document.
    1. Values:
    2. _blank: Opens the link in a new tab.
    3. _self: Opens the link in the same tab (default).
    4. _parent: Opens the link in the parent frame.
    5. _top: Opens the link in the full body of the window.

Example

<a href="https://www.example.com" target="_blank">Open in a new tab</a>
  1. title: Adds a tooltip that appears when the user hovers over the link.

Example

<a href="https://www.example.com" title="Visit Example">Visit Example</a>
  1. rel: Specifies the relationship between the current page and the linked page. Common values include noopener, noreferrer, and nofollow.

Example

<a href="https://www.example.com" rel="noopener noreferrer">Visit Example</a>

4. Anchor Links (Jumping within the Page)

Anchor links allow you to link to specific sections within the same page. This is useful for long pages with different sections, such as FAQs, tables of contents, or navigation within a page.

Creating Anchor Links

  1. First, create an id attribute for the target element.
  2. Then, create a link that refers to the id.

Syntax

<a href="#section1">Go to Section 1</a> ... <h2 id="section1">Section 1</h2>

Explanation

  1. The link <a href="#section1">Go to Section 1</a> will jump to the section with id="section1" when clicked.

Example

<a href="#contact">Contact Us</a> ... <h2 id="contact">Contact Us</h2>

In this example, clicking on the "Contact Us" link will take the user directly to the section of the page with the id="contact".


5. Navigation Menus

Navigation menus are a set of hyperlinks that allow users to navigate to different pages or sections of a website. They are typically organized in a list format.

Creating a Basic Navigation Menu

A navigation menu can be created using <ul> (unordered list) and <li> (list items), with each item being an anchor link.

Syntax

<ul> <li><a href="home.html">Home</a></li> <li><a href="about.html">About Us</a></li> <li><a href="services.html">Services</a></li> <li><a href="contact.html">Contact</a></li> </ul>

Example

<nav> <ul> <li><a href="home.html">Home</a></li> <li><a href="about.html">About Us</a></li> <li><a href="services.html">Services</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav>

This example creates a basic navigation menu with links to four pages: Home, About Us, Services, and Contact.


Summary

Feature Description
Absolute Link A complete URL pointing to a resource.
Relative Link A shorter URL pointing to a resource within the same site.
Anchor Tag (<a>) The HTML tag used to create hyperlinks.
Link Attributes Additional properties like target, title, and rel that modify link behavior.
Anchor Links Links that navigate to a specific section within the same page.
Navigation Menus A collection of links to different sections or pages, typically organized in a list.

Hyperlinks are an integral part of web navigation, and understanding how to use them effectively allows you to enhance the user experience and make your website more interactive.