HTML Links (Anchors)

Learn how to create and use various types of links in HTML.

What are HTML Links?

HTML links allow users to navigate to different pages, jump to sections within the same page, download files, send emails, or even make phone calls. All links are created using the <a> (anchor) tag with the href attribute.

<a>Anchor Tag

The <a> tag is used to create clickable links. The destination is defined by the href attribute.

  • Basic structure: <a href='URL'>Link Text</a>
  • Links can point to external pages, internal pages, sections, files, emails, or phone numbers
  • Essential for website navigation

Basic Link Example

A simple external link.

Absolute URLs

An absolute URL specifies the full web address including protocol and domain. Useful for external sites.

  • Always includes the protocol (http:// or https://)
  • Best used with target='_blank' for external links
  • Example: https://developer.mozilla.org

Absolute URL Example

External website link.

Relative URLs

In real projects, relative URLs link to other files. In our compiler, we'll demonstrate using a section ID.

  • Starts with / or ./ or ../
  • Points to files within the same structure
  • Click the link below and observe the scroll jump!

Relative Link Demo

Demonstrates internal site navigation.

Anchor Links

Anchor links jump to a specific 'id' within the same document.

  • Uses #followed by the target id
  • Critical for 'Table of Contents' and long pages
  • Instantly scrolls the preview to the target element

Anchor Link Example

Standard in-page navigation.

targetAttribute

Defines where the link opens.

  • _self: Same tab (default)
  • _blank: New tab

Target Attribute Example

Opens the link in a new tab.

download

Forces the file to download.

  • Uses a Data URL for this demo
  • Verify in preview by clicking

Download Attribute Example

Forces a download when clicked.

mailto:Email Links

Launch email client.

  • mailto:email@address.com
  • Can include ?subject=...

Email Link Example

Opens default email client.

tel:Phone Links

Call numbers on mobile.

  • tel:+123456789
  • Highly useful for contact pages

Phone Link Example

Triggers call on mobile devices.

Link States

Style links based on user interaction.

  • :link, :visited, :hover, :active
  • Try hovering over links in the preview!

CSS State Example

Visual feedback for users.

Bookmark Links

Navigate large documents easily.

  • Combines # with element IDs
  • Ideal for Tables of Contents

Bookmark Link Example

Navigate using bookmarks.

Knowledge Check

1. Which attribute opens a link in a new browser tab?

2. Which prefix is used for email links?

3. What does the download attribute do?

4. Which link type jumps to a section within the same page?