HTML <a> Tag

Beginner
⏱️ 9 min read
📚 Updated: Jun 2026
🎯 10 Examples
Hyperlinks

What You’ll Learn

By the end of this tutorial, you’ll confidently use the anchor element in real-world projects.

01

Core Syntax

Write valid <a> elements with correct opening, closing, and nesting rules.

02

Essential Attributes

Master href, target, rel, download, and more.

03

Link Types

Create external links, internal routes, email links, phone links, and file downloads.

04

CSS Styling

Style default, hover, focus, and visited states for accessible, beautiful links.

05

Anchor Navigation

Build smooth in-page jumps with fragment identifiers and scroll behavior.

06

Production Tips

Apply security, SEO, and accessibility best practices used by top websites.

What Is the <a> Tag?

The anchor element (<a>) is one of HTML’s most important tags. It creates a hyperlink — a clickable connection between the current document and another resource: a webpage, file, email address, phone number, or a specific section on the same page.

💡
Why “anchor”?

Historically, links “anchored” one document to another. Today, the term still applies when jumping to a fixed point within a page using #section-id.

Without the <a> tag, the web wouldn’t be a web. Search engines crawl links, users navigate through them, and SPAs still rely on anchor semantics under the hood.

📝 Syntax

The basic structure of an anchor element is straightforward:

syntax.html
<!-- Basic anchor syntax -->
<a href="URL">Link Text</a>

Syntax Rules

  • href is the destination — required for the link to function (except placeholder links).
  • Link text goes between the opening and closing tags and should be descriptive.
  • Self-closing syntax (<a />) is not valid in HTML.
  • Block-level elements cannot be nested inside inline anchors in HTML4; in HTML5, anchors can wrap most content if there’s a single interactive target.
complete-example.html
<a
  href="https://developer.mozilla.org"
  target="_blank"
  rel="noopener noreferrer"
>
  MDN Web Docs
</a>

⚡ Quick Reference

Use CaseCode SnippetResult
External link<a href="https://example.com">Visit</a>Visit
Same-page link<a href="#top">Back to top</a>Back to top
Email link<a href="mailto:hi@example.com">Email</a>Email
Phone link<a href="tel:+15551234567">Call</a>Call
Download file<a href="file.pdf" download>PDF</a>Triggers download
New tab<a href="..." target="_blank" rel="noopener">Open</a>Opens new tab safely

🧰 Attributes

Attributes modify link behavior, security, and accessibility. Here are the most important ones:

href Required*

The URL or fragment the link points to. Use #id for in-page anchors, mailto:, tel:, or absolute/relative paths.

<a href="/about">About</a>
target Optional

Where to open the linked document. Common value: _blank (new tab/window).

<a href="..." target="_blank">Open</a>
rel Security

Relationship hint. Use noopener noreferrer with target="_blank" to prevent tab-nabbing.

rel="noopener noreferrer"
download Optional

Forces download instead of navigation. Optional filename value overrides the saved name.

<a href="report.pdf" download="Q1.pdf">
title A11y

Advisory tooltip text. Supplement — not a replacement — for accessible link text.

title="Opens in a new tab"
hreflang SEO

Hints the language of the linked resource (e.g. hreflang="fr").

hreflang="en-US"
type Optional

MIME type advisory for the linked resource (rarely needed in practice).

type="application/pdf"
referrerpolicy Privacy

Controls how much referrer info is sent. E.g. no-referrer, strict-origin.

referrerpolicy="no-referrer"

* href can be omitted for placeholder links (href="#" or href=""), but always provide a meaningful destination in production.

Examples Gallery

Real-world anchor patterns with copy-ready code, live previews, and hands-on practice.

href

The href attribute specifies the destination URL or location the hyperlink points to. It can be an absolute address (https://…), a relative path (/about), a fragment (#section), or a special scheme like mailto:.

href.html
<a href="https://codetofun.com">Visit CodeToFun.com</a>
Try It Yourself

target

The target attribute controls where the linked page opens. Common values are _self (same tab, default) and _blank (new tab or window). When using _blank for external sites, add rel="noopener noreferrer" for security.

target.html
<a href="https://codetofun.com" target="_blank" rel="noopener noreferrer">Visit CodeToFun.com in a new tab</a>
Try It Yourself

📚 Common Use Cases

Here are the most frequent ways developers use the <a> tag in real projects.

Basic Hyperlinks

The primary role of the <a> tag is to create basic hyperlinks that take users to another page. Use clear anchor text so visitors know what they will find when they click.

basic-hyperlinks.html
<a href="https://codetofun.com/css">Visit CSS Tutorial</a>
Try It Yourself

Linking to Sections within a Page

Set href to a fragment identifier (an id prefixed with #) to link to a specific section on the same page. Combine a path with a fragment—href="/about#team"—to jump to a section on another page.

linking-to-sections.html
<a href="#section">Jump to Section</a>
Try It Yourself

Email Links (mailto:)

A mailto: link opens the visitor’s default email client with the address pre-filled. You can also add a subject or body using query parameters: mailto:user@example.com?subject=Hello.

email-link.html
<a href="mailto:hello@codetofun.com">Email CodeToFun</a>
Try It Yourself

Telephone Links (tel:)

A tel: link lets mobile users tap to call a phone number. Use the international format for best results: tel:+18005551234.

telephone-link.html
<a href="tel:+18005551234">Call +1 (800) 555-1234</a>
Try It Yourself

Download Attribute

The download attribute tells the browser to save the linked file instead of navigating to it. Optionally set a filename: download="report.pdf".

download-link.html
<a href="/files/honey-bee.pdf" download="honey-bee.pdf">Download PDF</a>
Try It Yourself

🚀 Beyond the Basics

Once you know href and target, these patterns separate a good anchor tutorial from a great one: clickable images, SEO-aware rel values, and accessible link design.

Image Links

Wrap an <img> inside <a> to make a logo, thumbnail, or banner clickable. Always provide meaningful alt text so screen readers describe the destination.

image-link.html
<a href="https://codetofun.com">
  <img src="/images/logo/logo/logo-128.png" alt="CodeToFun logo — visit homepage" width="128" height="128">
</a>
Try It Yourself

rel Attribute — SEO & Security

The rel attribute describes the relationship between the current page and the linked resource. Use noopener noreferrer with target="_blank" for security. For SEO, Google recognizes nofollow, sponsored, and ugc.

rel-seo.html
<a href="https://example.com" rel="nofollow">Untrusted user link</a>
<a href="https://partner.com" rel="sponsored noopener noreferrer" target="_blank">Sponsored ad</a>
<a href="https://forum.com/post" rel="ugc nofollow">Forum comment link</a>
Try It Yourself

Accessible Links

Links must work for keyboard and screen-reader users. Use descriptive text, aria-label for icon-only links, visible :focus-visible outlines, and skip links that let users jump past navigation.

accessible-links.html
<a href="#main-content" class="skip-link">Skip to main content</a>
<a href="/html/tags" aria-label="Browse all HTML tags">
  <span aria-hidden="true">&#9776;</span>
</a>
Try It Yourself

SMS Links (sms:)

Like tel:, the sms: scheme opens the device’s messaging app. Pre-fill the body with a query: sms:+18005551234?body=Hello. Support varies by device and browser.

sms-link.html
<a href="sms:+18005551234">Text +1 (800) 555-1234</a>
<a href="sms:+18005551234?body=Hello%20from%20CodeToFun">Text us with a pre-filled message</a>
Try It Yourself

🧠 How Anchor Navigation Works

1

User clicks the link

The browser reads the href value on the <a> element to determine the destination.

User action
2

Browser resolves the URL

Absolute URLs load a new document. Relative paths resolve against the current page. Fragments (#id) scroll within the same page.

Navigation
3

target decides the frame

_self (default) replaces the current tab. _blank opens a new browsing context. Use rel with _blank for security.

Presentation
=

Seamless web navigation

The <a> tag connects pages into the hyperlinked web. It is an inline element—wrap text, images, or block content (in HTML5) to make any of it clickable.

Universal Browser Support

The <a> tag has been supported since the earliest browsers and remains a baseline HTML feature.

Baseline · Since 1993

Works everywhere your users are

From legacy Internet Explorer to the latest Chromium builds — the anchor tag is one of the most universally supported HTML elements ever created.

100% Core tag support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support

Newer attributes

These work in all modern browsers but may lack support in very old ones.

📥
download Chrome 15+ · Firefox 20+ · Safari 10.1+
Partial
🔒
referrerpolicy Modern browsers · Not in IE
Partial
<a> tag 100% supported

Bottom line: Ship links with confidence. The core <a> element is safe to use in every production environment today.

Conclusion

The HTML <a> tag is deceptively simple yet incredibly powerful. Whether you’re building a personal blog, a SaaS dashboard, or an e-commerce store, links are how users and search engines discover and move through your content.

Master the syntax, respect accessibility, apply thoughtful CSS, and your links will feel invisible in the best way — fast, clear, and trustworthy. Use the quick reference above and experiment until creating links becomes second nature.

💡 Best Practices

✅ Do

  • Use descriptive link text (“Read the pricing guide” not “Click here”)
  • Add rel="noopener noreferrer" with target="_blank"
  • Ensure visible focus styles for keyboard users
  • Use mailto: and tel: for contact actions
  • Indicate external links when it helps users (icon + aria-label)

❌ Don’t

  • Use empty or javascript-only href values in production
  • Remove focus outlines without an alternative
  • Nest interactive elements inside an <a>
  • Rely on color alone to distinguish links
  • Open new tabs without user expectation

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about <a>

Bookmark these before you ship — they’ll make every link you write faster, safer, and more accessible.

5
Core concepts
📍 02

Meaningful Destinations

href defines where a link goes. Always point to a real, purposeful URL — never leave users guessing.

Essential
03

Jump In-Page

Use #id fragments for same-page navigation. Pair with smooth scrolling for a polished UX.

Navigation
🎨 04

Style Every State

Design :link, :visited, :hover, and :focus-visible — keyboard users depend on visible focus.

Accessibility
🛡 05

Lock Down Externals

Always add rel="noopener noreferrer" when using target="_blank" to prevent tab-nabbing attacks.

Security

❓ Frequently Asked Questions

The <a> (anchor) tag creates hyperlinks. Users click it to navigate to other pages, download files, send email (mailto:), or jump to sections on the same page.
href is the destination. target controls where it opens—_self (same tab, default), _blank (new tab), _parent, or _top.
Add target="_blank" and rel="noopener noreferrer". The noopener value prevents the new page from accessing window.opener.
Give the target element an id, then set href="#that-id" on your anchor. The browser scrolls to the matching element.
Use href="mailto:address@example.com". The browser opens the user’s email client. Add ?subject= or &body= for pre-filled content.
Use href="tel:+18005551234". On mobile devices, tapping the link starts a call. Include the country code for international numbers.
Add download to an <a> tag to suggest saving the linked resource. Use download="filename.pdf" to set the saved filename. Most reliable for same-origin URLs.
Yes. Wrap an <img> inside <a> to make the image clickable. Add meaningful alt text on the image for accessibility.
rel="nofollow" tells search engines not to pass ranking credit to the linked page. Use it for untrusted user content. For paid ads use rel="sponsored"; for forum posts and comments use rel="ugc".
Write descriptive link text, add aria-label for icon-only links, style :focus-visible for keyboard users, and include skip links (href="#main-content") on navigation-heavy pages.
An sms: link opens the device messaging app, similar to tel: for phone calls. Example: href="sms:+18005551234?body=Hello". Works best on mobile devices.

Practice in the Live Editor

Open the HTML editor, write your own anchor links, and see the result instantly.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful