The Anchor Element: Turning Text Into a Link
One element built the web. Learn the href attribute, what makes link text good or useless, the target and rel attributes, and why a link is not a button.
-
HTML Basics
- What is HTML: The Structure Layer of Every Web Page
- HTML Document Structure: DOCTYPE, html, head and body
- Elements, Tags and Attributes: The Vocabulary of HTML
- HTML Comments: Notes That Ship With Your Code
- Block Level and Inline Elements
- Writing and Running Your First HTML Page
- How a Browser Turns Markup Into a Page
- Text and Formatting
- Links and Navigation
- Images and Media
- Lists
- Tables
-
Forms
- Form Structure: form, action and method
- Input Types: Text, Email, Number, Date and the Rest
- Labels: The Most Important Element in a Form
- Checkboxes, Radio Buttons and Grouping
- select, option, optgroup and datalist
- textarea, File Uploads and Hidden Fields
- Buttons: submit, reset and button
- Built In Form Validation
- GET or POST: What Happens When a Form Is Submitted
- Semantic HTML
- HTML5 Features
- Head and Metadata
- HTML with CSS
- HTML with JavaScript
- Accessibility
-
HTML SEO
- How Google Works: Crawling, Indexing and Ranking
- SEO Friendly HTML Structure
- Titles and Descriptions That Earn Clicks
- Headings and Content Structure for Search
- Internal Linking and Anchor Text
- robots.txt and XML Sitemaps
- Canonical URLs and Duplicate Content
- Structured Data and JSON-LD
- Image SEO
- Core Web Vitals and Mobile Friendliness
- DevTools and Debugging
- Editor Productivity
- HTML Best Practices
- HTML Projects
- Advanced Projects
- Practice and Exams
Concept
The anchor element, a, is what makes hypertext hyper. It takes a piece of content and attaches a destination to it, so that activating the content takes the reader somewhere else.
It is the most important element in HTML. Search engines discover pages by following anchors. Screen reader users list every anchor on a page and pick one. Browser history, the back button, bookmarking, opening in a new tab, copying a URL - all of it exists because of this one element and stops working when it is faked with a script.
Syntax
<a href="destination">the text a reader sees and activates</a>href - hypertext reference - is the destination. An anchor without an href is not a link at all: it is not focusable, not announced as a link, and not followed by a crawler. It is simply a span with a strange name.
Example
<p>
Full instructions are in the
<a href="/guides/installation">installation guide</a>.
</p>
<p>
The figures come from the
<a href="https://example.org/transport/2026"
target="_blank"
rel="noopener noreferrer">District Transport Review (opens in a new tab)</a>.
</p>
<a href="/products/kettle" class="card">
<img src="/images/kettle.jpg" alt="" width="400" height="300">
<h3>Two litre electric kettle</h3>
<p>Boils in ninety seconds. Two year warranty.</p>
</a>Explanation
The first link sits inside a sentence, and its text describes the destination on its own. The second points off site, opens in a new tab, and says so in the visible text, so no one is surprised when the back button stops working.
The third is a whole card wrapped in one anchor. Modern HTML permits an anchor to contain block content, which is exactly what a clickable card needs: one link, one tab stop, one announcement. The image carries empty alt text because the heading beside it already names the destination - repeating it would make the screen reader say the same thing twice.
Link text is the interface
Screen reader users routinely pull up a list of every link on the page and read it out of context. So does a crawler, roughly. Which means link text has to make sense alone.
| Poor | Better | Why |
|---|---|---|
| Click here | Download the fee structure | Says nothing when read alone |
| Read more | Read more about hostel admission | Twelve identical entries in the link list |
| https://example.com/a/b/c | The full report | A screen reader spells out every character |
| Link | Contact the admissions office | Describes the element, not the destination |
The test is simple: read your link text with your eyes closed to the sentence around it. If you cannot tell where it goes, rewrite it.
target and rel
target="_blank" opens the destination in a new browsing context. Always pair it with rel:
<a href="https://example.org" target="_blank" rel="noopener noreferrer">External site</a>noopenerstops the new page from getting a reference back to yours throughwindow.opener, which was a real security problem and is now the default in modern browsers - but stating it protects older ones.noreferreralso withholds the referring URL.
Other rel values worth knowing:
| Value | Meaning |
|---|---|
nofollow | Do not pass ranking credit through this link |
sponsored | A paid or advertising link |
ugc | User generated content, such as a comment link |
noreferrer | Do not send the referrer header |
me | Another profile belonging to the same person |
Link or button?
This is a decision, not a style choice.
- A link navigates. It changes what document you are looking at. It is activated with
Enter, can be middle clicked, and its URL can be copied. - A button acts. It submits, opens, toggles, deletes. It is activated with
EnterorSpace.
Using the wrong one breaks keyboard behaviour and announces the wrong role. A <a href="#"> that runs a script is a link that goes nowhere: it is announced as a link, adds a history entry, and jumps the page to the top when activated.
Important rules
- Do not nest an anchor inside another anchor, or put a button inside one. Interactive elements must not contain interactive elements.
- An anchor with no
hrefis not focusable and not a link. - Never remove the focus outline without replacing it with something equally visible. Keyboard users navigate by it.
- Underlining is the one visual convention every reader already understands. Removing it from body copy links makes them hard to find, especially for readers who do not perceive colour differences.
- Tab order follows document order, so the markup order of your navigation is the order a keyboard reaches it.
Common mistakes
- Click here and read more, repeated down a page.
<a href="#">or<a href="javascript:void(0)">used as a button.- Opening every link in a new tab. It takes control away from the reader, who can already do it themselves.
- Not warning readers when a link opens a new tab, downloads a file, or starts a phone call.
- Making a whole paragraph one link, so the screen reader announces the entire paragraph as link text.
- Styling a
spanor adivto look like a link and attaching a click handler.
Best practices
- Write link text that names the destination in three to six words.
- Keep links visually distinct from ordinary text by more than colour alone.
- Reserve
target="_blank"for genuinely disruptive cases - a PDF, a payment gateway, a form the reader is halfway through - and say so in the text. - Use a real
buttonfor actions and a reala hreffor navigation, every time. - Give links a large enough tap target on touch screens; roughly forty four pixels square is the usual guidance.
Practice
- Take a page you have written and list every link out of context. How many still make sense?
- Rewrite three read more links so each names its destination.
- Build a card where the entire card is one link, with a heading, an image and a description, and check that a keyboard reaches it in exactly one tab stop.
- Explain why
<a href="#" onclick="save()">Save</a>is wrong in three separate ways.