The “HT” in HTML stands for HyperText — text that contains links to other text. Links are what turn millions of separate documents into one connected web. Without them, every page would be an island.
In this chapter, we'll learn the anchor element <a>, how to write correct paths to other pages, how to link to a section of a page, open email and phone apps, offer downloads, and how to write links that are safe and accessible.
The Anchor Element
A hyperlink is created with the
<a>(anchor) element. Itshrefattribute holds the destination URL, and its content is the clickable link text.
<a href="https://www.cslearningportal.com">Visit CS Learning Portal</a>Let's break this down:
<a href="https://www.cslearningportal.com">Visit CS Learning Portal</a>
│ │ └─ the destination URL └─ link text (what users see and click)
│ └─ href = "hypertext reference"
└─ a = anchorBy default, browsers show unvisited links as blue and underlined, and visited links in purple. When you hover, the cursor changes to a pointing hand. All of this can be changed with CSS later.
An <a> without an href is not a link at all — it is just a placeholder. It isn't focusable with the keyboard and doesn't look like a link. So always include href when you mean to link somewhere.
Absolute vs Relative URLs
The value of href can be written in two broad ways.
Absolute URLs
An absolute URL is the complete address, including the scheme (https://):
<a href="https://developer.mozilla.org/en-US/docs/Web/HTML">MDN HTML docs</a>Use absolute URLs to link to other websites.
Relative URLs
A relative URL is written relative to the location of the current page. Use relative URLs to link to pages within your own site. They are shorter, and they keep working if you move the whole site to a new domain.
Suppose your project folder looks like this:
my-site/
├── index.html
├── about.html
├── notes/
│ ├── dsa.html
│ └── os.html
└── images/
└── logo.pngHere's how to write each link, depending on which file you are writing it in:
| Link written in | Link to | href | Why |
|---|---|---|---|
| index.html | about.html | about.html | Same folder |
| index.html | notes/dsa.html | notes/dsa.html | Go into the notes folder |
| notes/dsa.html | notes/os.html | os.html | Same folder (notes) |
| notes/dsa.html | index.html | ../index.html | .. means “go up one folder” |
| Any page | index.html | /index.html | Leading / means “from the site root” |
Worked Example: Resolving a Relative URL
Say the current page is https://example.com/notes/dsa.html and it contains <a href="../images/logo.png">. The browser works it out step by step:
- Take the current page's folder:
https://example.com/notes/ - Apply
..→ go up one level:https://example.com/ - Append the rest:
https://example.com/images/logo.png
A root-relative URL like /images/logo.png skips step 1 and always starts from https://example.com/, no matter where the current page is.
Root-relative URLs (
/about.html) work on a web server but behave differently when you open files directly from your disk (file:///), because the “root” then becomes the root of your drive. For local practice, plain relative paths (about.html,../index.html) are the safest.
Linking to a Section of a Page
Long pages (like this tutorial!) often link to a specific section. This works in two steps:
- Give the target element an
id. - Link to it with
#followed by the id. This part of a URL is called the fragment.
<p><a href="#stack">Jump to Stack</a></p>
<h2 id="queue">Queue</h2>
<p>A queue is FIFO.</p>
<h2 id="stack">Stack</h2>
<p>A stack is LIFO.</p>Clicking “Jump to Stack” scrolls the page so the “Stack” heading is at the top. The URL in the address bar also changes to end with #stack, so you can share a link directly to that section.
You can combine a page and a fragment:
<a href="notes/dsa.html#stack">Stacks in the DSA notes</a>Two useful details:
- An
idmust be unique within the page, and it is case-sensitive:#Stackwill not findid="stack". href="#top"scrolls to the top of the page even if no element hasid="top"— the HTML standard treats “top” as a special case.
In older HTML you may see <a name="stack"> used as the target. The name attribute on <a> is obsolete; use id on any element instead.
Opening Links in a New Tab: target and rel
The target attribute says where to open the link. The value you'll use most is _blank, which opens a new tab (or window).
<a href="https://github.com" target="_blank" rel="noopener noreferrer">GitHub (opens in a new tab)</a>Why rel="noopener"?
A page opened in a new tab used to get a JavaScript reference back to your page, called window.opener. A malicious page could use it to silently redirect your original tab to a fake login page while the user was looking elsewhere. This attack is called reverse tabnabbing.
rel="noopener"cuts that reference.rel="noreferrer"additionally stops the browser sending theRefererheader (so the other site doesn't learn which page the visitor came from). It also impliesnoopener.
Modern browsers now apply noopener automatically to target="_blank" links, so the attack is blocked by default. Writing rel="noopener" explicitly is still a good habit — it documents your intent and protects users on older browsers.
Should Links Open in New Tabs?
Usually, no. Users can choose to open any link in a new tab themselves (middle-click or long-press). Forcing a new tab breaks the Back button and can confuse screen-reader users. Reasonable exceptions are links that would interrupt a task — for example a “Terms and conditions” link inside a half-filled form. When you do use _blank, tell the user, as in “(opens in a new tab)” above.
target value | Opens the link in… |
|---|---|
_self | The same tab (default) |
_blank | A new tab or window |
_parent | The parent frame (used with iframes) |
_top | The topmost window, breaking out of all frames |
Email, Phone and Download Links
Email Links: mailto:
<a href="mailto:help@example.com">Email us</a>
<a href="mailto:help@example.com?subject=Doubt%20in%20Chapter%206">Ask a doubt</a>Clicking opens the user's email app with the address filled in. The ?subject= part pre-fills the subject. Spaces in a URL must be encoded as %20.
Be aware that email addresses written in a page can be collected by spam bots, and mailto: does nothing useful if the user has no email app configured. Many sites show the address as text too.
Phone Links: tel:
<p>Helpline: <a href="tel:+919876543210">+919876543210</a></p>On a phone, tapping this opens the dialler. Write the number in international format in href: a +, the country code (91 for India) and the digits, with no spaces. Showing the number itself as the link text is helpful: on a desktop without a calling app the link may do nothing, but the user can still read the number. (If you format the visible number with spaces, use non-breaking spaces so it never wraps across two lines — see Character Entities & Symbols.)
Download Links: the download attribute
<a href="files/syllabus-2026.pdf" download>Download syllabus</a>
<a href="files/syllabus-2026.pdf" download="CS-Syllabus.pdf">Download syllabus</a>Normally, a PDF link opens the PDF in the browser. The download attribute asks the browser to save the file instead. If you give it a value, that value is suggested as the file name.
Important limitation: download only works for same-origin URLs (files on your own site), plus blob: and data: URLs. For a file on another website, the attribute is ignored and the link behaves normally.
Writing Good Link Text
Screen-reader users often pull up a list of all links on a page to navigate quickly. Search engines also use link text to understand what the target page is about. In both cases, the link text is read out of context.
<!-- Bad: a list of links reads "click here, click here, here" -->
<p>For DSA notes, <a href="notes/dsa.html">click here</a>.</p>
<!-- Good: the link text describes the destination -->
<p>Read our <a href="notes/dsa.html">data structures notes</a>.</p>Guidelines:
- Describe the destination: “Download the 2026 syllabus (PDF, 2 MB)” is better than “Download”.
- Don't put the full URL as link text unless the URL itself is the point.
- The
titleattribute is not a substitute for good link text: it only appears on mouse hover and is ignored by many screen readers.
Wrapping Bigger Content in a Link
An <a> can contain more than text. In modern HTML it may wrap whole blocks — for example, a “card” made of a heading and a paragraph — as long as it is placed where that content would be allowed anyway.
<a href="notes/os.html">
<h3>Operating Systems</h3>
<p>Processes, threads, scheduling, deadlocks and memory management.</p>
</a>What a link cannot contain is other interactive content: no link inside a link, and no <button> or form control inside a link. The browser wouldn't know which one you clicked.
Links vs Buttons
A very common interview topic. They look similar once styled, but they mean different things:
<a href> | <button> | |
|---|---|---|
| Purpose | Navigates to a URL | Performs an action (submit, open menu, play) |
| Keyboard | Activated with Enter | Activated with Enter or Space |
| Right-click → “Open in new tab” | Works | Not available |
| Example | “Read the notes” | “Add to cart” |
If clicking it changes the URL, use a link. If it does something on the current page, use a button. We'll meet buttons properly in Forms.
Worked Example: A Mini Notes Site
Let's create a two-page site. Make a folder with this structure:
mini-site/
├── index.html
└── notes/
└── dsa.htmlindex.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home – Mini Notes</title>
</head>
<body>
<h1>Mini Notes</h1>
<p>Start with our <a href="notes/dsa.html">data structures notes</a>,
or jump straight to the <a href="notes/dsa.html#stack">section on stacks</a>.</p>
<p>Official reference: <a href="https://developer.mozilla.org/" target="_blank" rel="noopener">MDN Web Docs (opens in a new tab)</a></p>
<p>Questions? <a href="mailto:help@example.com">Email us</a>.</p>
</body>
</html>notes/dsa.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>DSA – Mini Notes</title>
</head>
<body>
<p><a href="../index.html">← Back to home</a></p>
<h1>Data Structures</h1>
<p>Contents: <a href="#queue">Queue</a> · <a href="#stack">Stack</a></p>
<h2 id="queue">Queue</h2>
<p>First In, First Out.</p>
<h2 id="stack">Stack</h2>
<p>Last In, First Out.</p>
</body>
</html>Open index.html in your browser. You'll see three lines of text, each with a blue underlined link. Clicking “data structures notes” opens the DSA page; clicking “section on stacks” opens the same page scrolled to the Stack heading (if the page is long enough to scroll). On the DSA page, “← Back to home” uses ../ to climb out of the notes folder.
Common Mistakes
- Forgetting the scheme in external links.
<a href="www.google.com">is a relative URL! Onexample.com/index.htmlit points toexample.com/www.google.comand gives a 404. Always writehttps://www.google.com. - Using backslashes from Windows paths:
notes\dsa.html. URLs always use forward slashes/. - Spaces or capital letters in file names.
My Notes.htmlbecomesMy%20Notes.html, and many web servers treatNotes.htmlandnotes.htmlas different files. Use lowercase names with hyphens:my-notes.html. - Duplicate ids, so fragment links jump to the wrong place.
- “Click here” link text.
- Nesting a link inside another link.
- Using
<a>with JavaScript for actions (likehref="#"plus a click handler) when a<button>is the correct element.
Interview and Exam Questions
1. What is the difference between an absolute and a relative URL?
An absolute URL contains the full address including the scheme, e.g. https://example.com/about.html. A relative URL is resolved against the current page's address, e.g. about.html or ../index.html. Use absolute URLs for other sites and relative URLs within your own site.
2. A page at https://site.com/a/b/page.html contains <a href="../c/x.html">. Where does it point?
Current folder is /a/b/. .. goes up to /a/. Appending gives https://site.com/a/c/x.html.
3. Why should you add rel="noopener" to links with target="_blank"?
Without it, older browsers give the new page access to window.opener, which lets it redirect the original tab (reverse tabnabbing). noopener removes that access. Modern browsers apply it by default for _blank, but adding it explicitly is still recommended.
4. How do you create a link to a specific section of a page?
Give the section an id, e.g. <h2 id="faq">, then link with href="#faq" (same page) or href="page.html#faq" (another page).
5. What does the download attribute do, and when does it not work?
It tells the browser to save the linked file instead of navigating to it, optionally suggesting a file name. It is ignored for cross-origin URLs; it works only for same-origin, blob: and data: URLs.
6. When should you use a link and when a button?
Use <a href> for navigation to another URL; use <button> for actions on the current page such as submitting a form or opening a menu.
7. What is wrong with <a href="www.wikipedia.org">Wikipedia</a>?
It has no scheme, so the browser treats it as a relative path inside the current site. It should be https://www.wikipedia.org.
Summary
- Links are made with
<a href="...">link text</a>. - Use absolute URLs for other sites and relative URLs (
page.html,folder/page.html,../page.html) within your site. - Link to a section with
#id; ids must be unique. target="_blank"opens a new tab — use sparingly, addrel="noopener", and tell the user.mailto:andtel:open the email app and dialler;downloadsaves same-origin files.- Write descriptive link text; never “click here”.
- Links navigate, buttons act.
Next, in Images, we'll use the same path rules to put pictures on our pages.