HTML Links

In HTML, links are essential elements that allow you to connect different web pages, resources, or locations within the same page. Understanding the various aspects of HTML links, such as the target attribute, absolute URLs vs. relative URLs, linking to local files, linking to sections on the same page, linking to email addresses, and linking to phone numbers, is crucial for creating a well-connected and user-friendly website.

The basic syntax for creating a link in HTML uses the <a> (anchor) element.

Here’s a simple example:

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

In this example, the href attribute contains the URL to which the link points. When a user clicks on the link, they will be directed to the specified web page.

Target Attribute

In HTML, the target attribute is used in anchor (<a>) tags to specify how a linked document should be displayed. Here are detailed examples for each target value: _blank, _self, _parent, and _top.

The target attribute determines how the linked content should be displayed. Common values include:

  • _blank: Opens the linked document in a new tab or window.
  • _self: Opens the linked document in the same frame or tab (default).
  • _parent: Opens the linked document in the parent frame.
  • _top: Opens the linked document in the full body of the window.

Example:

<a href="https://www.w3tweaks.com" target="_blank">Visit w3tweaks.com in a new tab</a>

Absolute URLs vs. Relative URLs

Absolute URLs:

Provide the complete web address, including the protocol (http/https).

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

Relative URLs:

Specify the path relative to the current page’s location.

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

Linking to Local Files

To link to local files, use a relative path. For example, linking to an image in the same directory:

<a href="images/logo.png">View Logo</a>

Linking to Sections on the Same Page

You can link to specific sections within the same page using the id attribute.

Here’s an example:

<a href="#section1">Jump to Section 1</a>

<!-- ... -->

<h2 id="section1">Section 1 Content</h2>

Linking to Email Addresses

To create a link that opens the user’s default email client, use the mailto scheme:

<a href="mailto:[email protected]">Contact Us</a>

Linking to Phone Numbers

For linking to phone numbers and initiating a call (on devices that support it), use the tel scheme:

<a href="tel:+1234567890">Call Us</a>

Normal State

The default appearance of a link without any interaction.

HTML

<a href="#">Normal Link</a>

CSS

/* Normal link style */
a {
   color: #3498db; /* Set the default color */
   text-decoration: none; /* Remove underline by default */
}

Hover State

The appearance of a link when the mouse is over it.

HTML

<a href="#">Hover over this Link</a>

CSS

/* Hover state */
a:hover {
 color: #e74c3c; /* Change color on hover */
}

Active State

The appearance of a link when it is clicked.

HTML

<a href="#">Click on this Link</a>

CSS

/* Active state - when the link is clicked */
a:active {
  color: #2ecc71; /* Change color on click */
}

Focus State

The appearance of a link when it is focused, often when tabbed to.

HTML

<a href="#" tabindex="0">Tab to Focus on this Link</a>

CSS

/* Focus state - when the link is focused (e.g., when tabbed to) */
a:focus {
  outline: 2px solid #f39c12; /* Add an outline for focus */
}

Visited State

The appearance of a link that has been visited before.

HTML

<a href="#" target="_blank">Visited Link (opens in new tab)</a>

CSS

/* Visited state - styles for visited links */
a:visited {
 color: #9b59b6; /* Change color for visited links */
}

Remember, effective use of links is crucial for creating a seamless navigation experience on your website. Understanding these link features in HTML will enable you to craft more interactive and user-friendly web pages.

W3TWEAKS
Latest posts by W3TWEAKS (see all)