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.
Fundamentals
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.
Foundation
📝 Syntax
The basic structure of an anchor element is straightforward:
Attributes modify link behavior, security, and accessibility. Here are the most important ones:
hrefRequired*
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>
targetOptional
Where to open the linked document. Common value: _blank (new tab/window).
<a href="..." target="_blank">Open</a>
relSecurity
Relationship hint. Use noopener noreferrer with target="_blank" to prevent tab-nabbing.
rel="noopener noreferrer"
downloadOptional
Forces download instead of navigation. Optional filename value overrides the saved name.
<a href="report.pdf" download="Q1.pdf">
titleA11y
Advisory tooltip text. Supplement — not a replacement — for accessible link text.
title="Opens in a new tab"
hreflangSEO
Hints the language of the linked resource (e.g. hreflang="fr").
hreflang="en-US"
typeOptional
MIME type advisory for the linked resource (rarely needed in practice).
type="application/pdf"
referrerpolicyPrivacy
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.
Hands-On
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:.
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
<ahref="https://codetofun.com"target="_blank"rel="noopener noreferrer">Visit CodeToFun.com in a new tab</a>
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.
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.
Target Section — you jumped here using href="#section".
📧 Special Link Types & Download
The href attribute supports URL schemes beyond ordinary web addresses. Use mailto:, tel:, sms:, and the download attribute to save files.
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.
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
<ahref="https://codetofun.com"><imgsrc="/images/logo/logo/logo-128.png"alt="CodeToFun logo — visit homepage"width="128"height="128"></a>
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
<ahref="https://example.com"rel="nofollow">Untrusted user link</a><ahref="https://partner.com"rel="sponsored noopener noreferrer"target="_blank">Sponsored ad</a><ahref="https://forum.com/post"rel="ugc nofollow">Forum comment link</a>
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
<ahref="#main-content"class="skip-link">Skip to main content</a><ahref="/html/tags"aria-label="Browse all HTML tags"><spanaria-hidden="true">☰</span></a>
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
<ahref="sms:+18005551234">Text +1 (800) 555-1234</a><ahref="sms:+18005551234?body=Hello%20from%20CodeToFun">Text us with a pre-filled message</a>
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
Newer attributes
These work in all modern browsers but may lack support in very old ones.
📥
downloadChrome 15+ · Firefox 20+ · Safari 10.1+
Partial
🔒
referrerpolicyModern browsers · Not in IE
Partial
<a> tag100% supported
Bottom line: Ship links with confidence. The core <a> element is safe to use in every production environment today.
Wrap Up
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.
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
Summary
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
🔗01
The Web’s Backbone
The <a> tag creates hyperlinks — the invisible threads that turn isolated pages into a connected web.
Foundation
📍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.