# The Anchor Element & href

> The `<a>` element becomes a hyperlink the moment you add `href`. What you write for the link text matters as much as where the link goes.

Subject: HTML | Lesson 14 of 42 | 5 min read
Interactive lesson: https://codewithanimation.in/html/anchor-and-href
Previous: [abbr, q, time & span](https://codewithanimation.in/html/abbr-q-time-and-span.md) | Next: [Paths & Fragment Links](https://codewithanimation.in/html/link-paths-and-fragments.md)

## What it is

`<a>` is the anchor element. By itself it does nothing: `<a>Some text</a>` renders as plain text with no link behavior. Adding `href` activates it as a hyperlink. The attribute value is the destination, and it can take many forms: an absolute URL, a relative path, a fragment identifier that jumps within the same page, a `mailto:` address, or a `tel:` number.

In HTML5 an `<a>` can wrap block-level content, so linking an entire card or section is valid markup. This was not allowed in HTML 4, where `<a>` could only appear inside inline content.

## Why it matters

The link text is the first thing a screen reader user hears when navigating a page's links list. Many assistive tools let users pull up every link on a page as a standalone list. A page full of "click here" and "read more" links becomes completely useless in that view: each item is identical and tells the user nothing. Descriptive link text is not a nicety; it is what makes navigation possible.

The `href` value type also has consequences beyond just where the browser goes. Relative URLs keep the site portable when the domain changes. Fragment links let users skip to specific sections. Correct scheme usage (`mailto:`, `tel:`) lets browsers hand off to the right app rather than trying to navigate to a URL.

## How it works

Add `href` with the destination and write the link text between the tags: `<a href="https://developer.mozilla.org">MDN Web Docs</a>`. The browser applies its default link styles: blue and underlined for unvisited, purple for visited. You override these with CSS.

For an absolute URL, include the full scheme and domain. For a relative URL, the path is resolved from the current page's location. A fragment identifier starts with `#` and jumps to the element whose `id` matches. The `download` attribute hints that the browser should save the file rather than navigate to it, and you can give the saved file a name: `<a href="/report.pdf" download="annual-report.pdf">`.

Write link text that makes sense read alone, out of context. "Read the MDN anchor docs" beats "click here". Avoid wrapping a bare URL as the link text when a real description fits, because URLs read poorly with screen readers and give no meaningful destination cue.

## Common mistake: Generic link text: "click here" and "read more"

These phrases tell the user nothing about where the link goes. They also force the user to read the surrounding sentence to get context, which is extra work that descriptive text would eliminate. Write the destination into the text: "Download the 2026 style guide" rather than "click here to download". The surrounding context is bonus information; the link text should stand alone.

## Tip: An <a> without href is intentionally inert

Sometimes you want a placeholder link, a navigation item that represents the current page, or a link that JavaScript will activate later. Writing `<a>Current page</a>` without an `href` is valid and produces an element that looks like text and is not keyboard-focusable by default. This is the correct way to mark a link that is not yet active, rather than using `href="#"` which scrolls to the top and can disrupt focus position.

## Try it: Try it

See the underline appear as a link enters view, and compare descriptive link text against the uninformative alternatives.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/anchor-and-href

## Check yourself

### Question 1

What does <a>About us</a> (no href) render as?

- A. Plain text with no link behavior
- B. A link to the current page
- C. An error; href is required for the element to be valid

Correct answer: A

`href` is what makes `<a>` a hyperlink. Without it the element is an inert anchor: valid markup, styled as text, and not keyboard-focusable by default.

- Why not B: Without href the anchor element is inert. It renders as text, not a hyperlink, and has no default navigation target.
- Why not C: An <a> without href is valid HTML. It represents a placeholder link or a link that JavaScript will activate later.

### Question 2

Which link text is most accessible?

- A. Read the 2026 accessibility guidelines
- B. Click here
- C. https://www.w3.org/TR/WCAG22/

Correct answer: A

Link text should describe the destination and make sense read alone, out of context. A link list user who hears "Read the 2026 accessibility guidelines" knows exactly where that link goes.

- Why not B: "Click here" gives no destination information when read alone. Screen reader users navigating a links list hear nothing useful.
- Why not C: A raw URL is read character by character by some screen readers and gives no natural-language description of the destination.

### Question 3

Is it valid in HTML5 to wrap an entire <article> card in a single <a>?

- A. Yes. HTML5 allows <a> to wrap block-level content
- B. No. <a> can only wrap inline content
- C. Only if the card contains no interactive elements like buttons or inputs

Correct answer: A

HTML5 changed the content model of `<a>` to allow block-level content as descendants. Wrapping an entire card is valid. The practical caution is avoiding nested interactive elements, which create confusing click targets.

- Why not B: That was the HTML 4 rule. HTML5 changed the content model of <a> to allow block-level descendants, so wrapping a card is valid.
- Why not C: Interactive content inside a linked block is a usability issue (nested clickable areas), but the markup itself is not invalid simply because of block content.

## Remember this

- `<a>` without `href` is inert plain text; `href` is what makes it a hyperlink.
- href accepts absolute URLs, relative paths, `#fragment` identifiers, `mailto:` addresses, and `tel:` numbers.
- Write link text that describes the destination and makes sense read out of context.
- HTML5 allows `<a>` to wrap block-level content; avoid nesting interactive elements inside a linked block.
