What is the <a> tag?

The <a> (anchor) element creates a hyperlink to another page, a file, a location within the page (#id), an email address (mailto:) or a phone number (tel:). It needs an href to be a real, keyboard-focusable link. Links navigate; buttons perform actions.

Key attributes

  • href: the destination (absolute or relative URL, #fragment, mailto:, tel:).
  • target="_blank": open in a new tab. Modern browsers imply rel="noopener", but adding rel="noopener noreferrer" is still common and harmless.
  • rel: relationship: noopener, noreferrer, nofollow, sponsored, ugc, external, me.
  • download: download the file instead of navigating (same-origin or blob:/data: URLs).
  • hreflang, type, referrerpolicy, ping.

Accessibility

  • Link text must make sense out of context: "Read the pricing guide", not "click here".
  • Warn users when a link opens a new tab or downloads a file.
  • Do not use <a> without href as a button, or a <div> as a link.
  • Visible focus styles and enough color contrast; do not rely on color alone to identify links in text.

Links vs buttons

  • Navigation to a URL: <a href>.
  • Actions (submit, open a modal, toggle): <button>.

Fragments and scrolling

href="#pricing" scrolls to id="pricing". CSS scroll-behavior: smooth and scroll-margin-top (to offset sticky headers) improve this. Text fragments (#:~:text=...) can link to specific text in Chromium and Safari.

Code example

html
1<style>
2 body { font-family: system-ui; }
3 a { color: #4f46e5; text-underline-offset: 3px; }
4 a:focus-visible { outline: 3px solid #f59e0b; outline-offset: 2px; }
5 html { scroll-behavior: smooth; }
6 #faq { scroll-margin-top: 16px; margin-top: 60vh; }
7</style>
8
9<nav>
10 <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a">MDN: the anchor element</a> |
11 <a href="https://html.spec.whatwg.org/" target="_blank" rel="noopener noreferrer">HTML spec (opens in a new tab)</a> |
12 <a href="#faq">Jump to FAQ</a>
13</nav>
14
15<p>
16 Contact <a href="mailto:support@example.com?subject=Help">support@example.com</a> or call
17 <a href="tel:+911234567890">+91 12345 67890</a>.
18</p>
19<p><a href="data:text/plain,Hello%20from%20DevQnA" download="hello.txt">Download hello.txt</a></p>
20
21<h2 id="faq">FAQ</h2>
22<p>You scrolled here with a fragment link.</p>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app