Links are one of the most important parts of HTML.
The web itself is essentially a huge network of documents connected through hyperlinks.
In this chapter, you'll learn how to create links, navigate between pages, open links in new tabs, link to sections, send emails, make phone links, download files, and write links securely and accessibly.
A link allows a user to navigate from one resource to another.
Example:
<a href="https://www.google.com">Google</a>The browser displays:
GoogleWhen the user clicks it, the browser navigates to the URL.
The HTML element is:
<a>The most important attribute is:
hrefThe general syntax is:
<a href="URL">Link Text</a>Example:
<a href="https://example.com">
Visit Example
</a>Break it down:
<a> → Anchor element
href → Destination
https://example.com → URL
Visit Example → Visible text
</a> → Closing tag<a> Element#<a> is called the anchor element.
It can point to:
Example:
<a href="/about.html">About Us</a>An absolute URL contains the complete address.
Example:
<a href="https://www.google.com">
Google
</a>Another:
<a href="https://www.nextgen-blog.com">
NextGen Blog
</a>An absolute URL usually includes:
https://
domain
path
query
fragmentFor example:
https://example.com/products?id=10#reviewsA relative URL points to a resource relative to the current document.
Suppose your project is:
website/
│
├── index.html
├── about.html
├── contact.html
└── pages/
└── services.htmlFrom index.html:
<a href="about.html">About</a>From index.html:
<a href="contact.html">Contact</a>To access services.html:
<a href="pages/services.html">
Services
</a>| Type | Example | Typical use |
|---|---|---|
| Absolute | https://example.com/about |
External sites |
| Relative | about.html |
Same website |
| Root-relative | /about |
Same website from root |
| Fragment | #contact |
Same-page section |
Consider:
<a href="/about">About</a>The / means the URL starts from the website's root.
If your website is:
https://example.comthen:
/aboutmeans:
https://example.com/aboutThis is common in modern web applications.
..#Suppose:
website/
│
├── index.html
│
└── pages/
└── about.htmlInside:
pages/about.htmlto go back to index.html:
<a href="../index.html">Home</a>.. means:
Go to the parent directory.
.#. represents the current directory.
For example:
<a href="./about.html">About</a>This means:
Look for
about.htmlin the current directory.
These are often equivalent:
<a href="about.html">About</a>and:
<a href="./about.html">About</a>A simple website might have:
website/
│
├── index.html
├── about.html
├── services.html
├── projects.html
└── contact.htmlNavigation:
<nav>
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="services.html">Services</a>
<a href="projects.html">Projects</a>
<a href="contact.html">Contact</a>
</nav>Now the pages are connected.
You can use:
<a href="/">Home</a>This points to the root of the website.
For a simple local project, you might instead use:
<a href="index.html">Home</a>Use:
target="_blank"Example:
<a
href="https://example.com"
target="_blank"
>
Open Example
</a>The browser will normally open the destination in a new tab or browsing context.
target="_blank"#When using _blank for an external page, a common best practice is:
<a
href="https://example.com"
target="_blank"
rel="noopener"
>
Visit Example
</a>noopener prevents the newly opened page from getting a reference to the opener through window.opener.
Modern browsers have also implemented protections around _blank, but explicitly using rel="noopener" remains clear and defensive.
rel="noreferrer"#You may also see:
<a
href="https://example.com"
target="_blank"
rel="noopener noreferrer"
>
Visit Example
</a>noreferrer can prevent the browser from sending the referring URL in the Referer header and also implies noopener behavior.
Use it when that privacy behavior is desired.
This is extremely useful.
Suppose:
<h2 id="about">About Me</h2>You can link to it:
<a href="#about">About Me</a>Clicking the link moves the browser to the element with:
id="about"Consider:
<a href="#projects">Projects</a>
...
<h2 id="projects">My Projects</h2>The URL may become:
https://example.com/#projectsThe part after # is called a fragment.
The browser looks for an element matching that fragment identifier.
This is a very common real-world use.
<nav aria-label="Table of contents">
<a href="#introduction">
Introduction
</a>
<a href="#html">
HTML
</a>
<a href="#css">
CSS
</a>
<a href="#javascript">
JavaScript
</a>
</nav>
<h2 id="introduction">Introduction</h2>
<p>...</p>
<h2 id="html">HTML</h2>
<p>...</p>
<h2 id="css">CSS</h2>
<p>...</p>
<h2 id="javascript">JavaScript</h2>
<p>...</p>This pattern is widely used in:
Use the mailto: scheme.
<a href="mailto:hello@example.com">
Email Me
</a>Clicking it usually opens the user's configured email application.
You can pre-fill a subject:
<a href="mailto:hello@example.com?subject=Hello">
Send Email
</a>You can also include a body:
<a
href="mailto:hello@example.com?subject=Hello&body=I%20have%20a%20question"
>
Contact Me
</a>Notice that spaces in URLs may need URL encoding.
Use:
<a href="tel:+919876543210">
Call Us
</a>On a mobile device, this can open the phone application.
This is particularly useful for:
Some devices support:
<a href="sms:+919876543210">
Send SMS
</a>Support can vary by device and browser.
The download attribute can suggest that the browser download a resource rather than navigate to it.
Example:
<a href="resume.pdf" download>
Download Resume
</a>You can optionally provide a filename:
<a
href="resume.pdf"
download="Anubhav-Resume.pdf"
>
Download Resume
</a>However, the browser/server and origin rules can affect whether the download behavior is honored.
You can simply link to a PDF:
<a href="documents/course.pdf">
HTML Course PDF
</a>The browser may open the PDF in its built-in viewer.
Or:
<a href="documents/course.pdf" download>
Download PDF
</a>You can link directly to an image:
<a href="images/photo-large.jpg">
Open Full Image
</a>You can also make an image clickable:
<a href="product.html">
<img
src="product.jpg"
alt="Product"
>
</a>This is common in:
Example:
<a href="/">
<img
src="logo.png"
alt="Company homepage"
>
</a>The entire image becomes the clickable link.
The alt text should describe the image's purpose in context.
For a linked logo, something like:
alt="Company homepage"can be more useful than:
alt="Logo"because it communicates the link destination/purpose.
Avoid vague link text like:
<a href="/article">
Click here
</a>Prefer:
<a href="/article">
Read the HTML accessibility guide
</a>Why?
Screen-reader users can navigate through links without necessarily hearing all surrounding text.
Compare:
Click here
Click here
Click herewith:
Read the HTML guide
View pricing
Contact supportThe second provides much more context.
If clicking something navigates somewhere, use:
<a href="/products">
View Products
</a>If clicking something performs an action, use:
<button type="button">
Add to Cart
</button>This distinction is extremely important.
Think:
Navigation → <a>
Action → <button>We'll study buttons extensively when we reach forms.
<a href="/profile">
View Profile
</a>Purpose:
Go somewhere.
<button type="button">
Delete Account
</button>Purpose:
Perform an action.
This distinction improves:
CSS commonly styles different link states:
:link
:visited
:hover
:focus
:activeHTML creates the link.
CSS controls how those states look.
Example:
a:hover {
text-decoration: underline;
}We'll learn CSS separately.
id and Links#Remember:
<h2 id="contact">Contact</h2>The id must be unique within the document.
Then:
<a href="#contact">
Contact
</a>connects to it.
Don't create:
<h2 id="contact">Contact</h2>
<h2 id="contact">Another Contact</h2>Duplicate IDs cause ambiguity and can break JavaScript, CSS selectors, accessibility relationships, and fragment navigation.
Consider:
https://example.com/products?id=123#reviewsBreak it down:
https://
↓
Protocol
example.com
↓
Host
/products
↓
Path
?id=123
↓
Query string
#reviews
↓
FragmentSo:
URL
│
├── Scheme
├── Host
├── Path
├── Query
└── FragmentThis isn't strictly HTML, but understanding URLs is essential for working with links.
Example:
<a href="/products?category=mobile">
Mobile Phones
</a>The URL contains:
category=mobileMultiple parameters:
<a href="/products?category=mobile&brand=samsung">
Samsung Phones
</a>The query string is commonly used to send information to a server or application.
You can have both:
<a href="/products?category=mobile#reviews">
Reviews
</a>The URL:
/products?category=mobile#reviewsmeans:
/products
↓
Path
?category=mobile
↓
Query
#reviews
↓
FragmentA professional website may contain both.
<nav>
<!-- Internal -->
<a href="/about">About</a>
<a href="/projects">Projects</a>
<!-- External -->
<a
href="https://github.com"
target="_blank"
rel="noopener"
>
GitHub
</a>
</nav>A typical website might have:
<header>
<a href="/" aria-label="Home">
<img src="logo.svg" alt="">
</a>
<nav aria-label="Main navigation">
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/projects">Projects</a>
<a href="/blog">Blog</a>
<a href="/contact">Contact</a>
</nav>
</header>Notice something important:
<nav aria-label="Main navigation">This gives the navigation landmark a useful accessible name.
We'll study accessibility in much more detail later.
Suppose you're currently on the About page.
You might have:
<nav aria-label="Main navigation">
<a href="/">Home</a>
<a
href="/about"
aria-current="page"
>
About
</a>
<a href="/projects">Projects</a>
</nav>aria-current="page" communicates that the current link represents the current page.
This is useful for assistive technologies.
You may see:
<a
href="/about"
title="Learn more about me"
>
About
</a>The title attribute can provide advisory information.
But don't rely on it for essential information.
Don't do:
<a href="/products" title="View our products">
Click here
</a>Instead:
<a href="/products">
View our products
</a>Visible link text is much more useful.
rel Attribute#rel describes the relationship between the current document and the linked resource.
Examples include:
rel="noopener"rel="noreferrer"rel="nofollow"rel="external"rel="sponsored"rel="ugc"Their purposes differ.
For example, links representing sponsored content can use:
<a
href="https://example.com"
rel="sponsored"
>
Sponsored Product
</a>For user-generated links:
<a
href="https://example.com"
rel="ugc"
>
User Recommendation
</a>Search engines use links to discover and understand pages.
Good:
<a href="/html-course">
Complete HTML Course
</a>Less informative:
<a href="/html-course">
Click here
</a>Descriptive link text provides useful context.
Internal linking can also help users discover related content.
For a blog:
HTML
│
├── CSS
│
├── JavaScript
│
└── ReactYou might connect related articles with descriptive links.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
<title>My Portfolio</title>
</head>
<body>
<header>
<a href="/" aria-label="Go to homepage">
<img
src="logo.svg"
alt=""
>
</a>
<nav aria-label="Main navigation">
<a href="/" aria-current="page">
Home
</a>
<a href="/about">
About
</a>
<a href="/projects">
Projects
</a>
<a href="/blog">
Blog
</a>
<a href="/contact">
Contact
</a>
</nav>
</header>
<main>
<h1>Welcome to My Portfolio</h1>
<p>
Learn more about
<a href="/about">my experience</a>.
</p>
<p>
You can also
<a
href="https://github.com"
target="_blank"
rel="noopener"
>
view my GitHub
</a>.
</p>
</main>
</body>
</html>This contains many professional practices:
| Requirement | HTML |
|---|---|
| Basic link | <a href="/about">About</a> |
| External link | <a href="https://example.com">Example</a> |
| New tab | target="_blank" |
| New tab security | rel="noopener" |
| Same-page section | href="#about" |
href="mailto:name@example.com" |
|
| Phone | href="tel:+919876543210" |
| Download | download |
| Parent directory | ../ |
| Current directory | ./ |
| Root | / |
| Current page | aria-current="page" |
Create a small 5-page portfolio website:
portfolio/
│
├── index.html
├── about.html
├── skills.html
├── projects.html
└── contact.htmlEvery page should have the same navigation:
Home | About | Skills | Projects | ContactInclude:
Welcome to My Portfolio
About Me
My Skills
My Projects
ContactCreate links to the appropriate pages.
Create:
Include:
Create three projects.
Each project should link to either:
Create:
Email Me
Call MeUse:
mailto:and:
tel:<a> element?href?../ mean?/ mean in a root-relative URL?target="_blank" do?rel="noopener" useful?rel="noreferrer"?<a> and <button>?aria-current="page" mean?download attribute?Remember this rule:
User clicks
│
┌──────────┴──────────┐
↓ ↓
Navigate Perform action
│ │
↓ ↓
<a> <button>And remember:
href="/about"
↓
Where should I go?
href="#contact"
↓
Which section should I show?
href="mailto:..."
↓
Start an email
href="tel:..."
↓
Start a phone callWe'll learn <img> in depth, including src, alt, image paths, dimensions, responsive images, srcset, sizes, <picture>, <figure>, <figcaption>, lazy loading, image accessibility, performance, and production image practices.
Mark it as complete to track your mastery.