< Modern HTML >

< a >

The anchor tag: more versatile than you may know

A native hyperlink. HTML gives you navigation, browser history, referrer policy, middle-click, Cmd/Ctrl-click, right-click context menu, drag-to-bookmark, prefetching hints, link decoration, visited state, and the correct assistive technology role. All of this is built in. The anchor element is one of the oldest elements in HTML and has quietly gained new capabilities over the years that are worth a fresh look.

Built-in with native HTML

All of these come with the element itself, as long as it has a real href value. The pattern for single-page-app routing is to let the anchor be a real anchor with a real URL. A router can still intercept the click and do in-app navigation underneath. Modern SPA router libraries do exactly this: they render a real <a> with a real href and enhance it with client-side routing.

href is the load-bearing attribute

An <a> without href is, by the HTML specification's own language, a "placeholder link." It:

If an anchor has no destination, it's worth considering whether the element you actually want is a <button>. Buttons are purpose-built for actions, and anchors are purpose-built for navigation. When there's no destination, there's no navigation.

The href="#" value is a related pattern worth thinking through. A single # is a valid URL fragment, and navigating to it mutates the current URL, scrolls the page to the top, and pushes a browser history entry. For a link that is meant to do something rather than navigate somewhere, a button is a cleaner fit.

Link relationship attributes that matter

The rel attribute carries real weight. Several values change browser behavior meaningfully:

target="_blank" considerations

The common pattern for an external link that opens in a new tab:

<a href="https://external.example.com" target="_blank" rel="noopener noreferrer">
  External link
</a>

Three things to think about:

  1. Security: noopener, as above.
  2. Privacy: noreferrer if the destination shouldn't know the source page.
  3. Accessibility: when a link opens in a new tab, the user deserves to know. Either a visible icon, visually hidden text like "(opens in new tab)" inside the accessible name, or both. Sighted users lose their back button, and screen reader users don't get a context-switch announcement by default.

Downloads, mail, tel, sms

Four things the anchor can do that are often delegated to JavaScript:

Taken together, a "share this page" component that offers email and SMS as share targets is two anchors with no script at all.

URL fragments and text fragments

<a href="#section-id"> is native in-page navigation. Paired with scroll-behavior: smooth on the root and the :target pseudo-class in CSS, it covers smooth scroll on click and the ability to style the currently-targeted section. No listener, no ref, no scroll library.

Text fragments go further. The URL can link to a specific phrase inside a page, even when the page author never marked it up:

<a href="https://example.com/article#:~:text=the%20exact%20phrase">
  Jump to the phrase in context
</a>

Supported in Chromium and Safari. Useful for documentation, citations, and any "see this specific line" pattern.

Composite patterns that use <a> well

Patterns where the anchor is the right element and a different element would break the pattern:

Already HTML-available

Patterns that HTML handles natively, which JavaScript is often asked to do instead:


Reference:

WHATWG
the-a-element
caniuse.com
mdn-html_elements_a
MDN Docs
Web/HTML/Element/a
Link types (rel values)
WAI-ARIA APG
Link Pattern
web.dev
Text Fragments