Menu

HTML Anchor Tag: How to Make Links with a href

How the <a> element makes links: the href attribute, absolute and relative URLs, jumping to a section of the page with #id, download links, card links, and the mistakes that break them.

This page includes runnable editors - edit, run, and see output instantly.

A link is an anchor element, <a>, with an href attribute. href is where the link goes; the content between the tags is what the reader clicks.

Browsers show links blue and underlined and change the cursor to a hand. The last line shows why href matters: without it, <a> is plain text. It is not focusable, not clickable, and not styled as a link.

Absolute and relative URLs

The value of href decides where the link goes. The main forms:

hrefGoes to
https://example.com/pageAn absolute URL: exactly that address
/aboutabout at the root of the current site
about.htmlabout.html in the same folder as the current page
../index.htmlindex.html one folder up
#pricingThe element with id="pricing" on this page
/help#faqThe faq section of another page on this site
mailto:hi@example.comA new email in the reader's mail app
tel:+15551234567A phone call, on devices that can make one

Use absolute URLs for other sites and root-relative paths (/about) inside your own: they keep working when the page that contains them moves to another folder.

<a href="https://www.wikipedia.org/">Wikipedia</a>  <!-- another site -->
<a href="/pricing">Pricing</a>                     <!-- same site, from the root -->
<a href="www.wikipedia.org">Broken</a>              <!-- relative path, not a site! -->

The last line is the most common link bug. With no https://, the browser reads www.wikipedia.org as a file name in the current folder.

Email and phone links have their own page: HTML email links.

Linking to a section of the page

Any element with an id is a target. Link to it with # followed by the id, and the browser scrolls to it. Click the links in the preview:

Some details:

  • The id must match exactly, including case. href="#FAQ" does not find id="faq".
  • href="#top" and href="#" scroll to the top of the page even when no element has the id top.
  • The :target selector styles the section the URL currently points to. It highlights here on a real page; the preview scrolls without changing its address, so the highlight does not show in it.
  • To link to a section of another page, add the hash to that page's URL: <a href="/docs/html/table#caption">.

A sticky or fixed header covers the top of the section you jump to. scroll-margin-top on the targets leaves room for it, and scroll-behavior: smooth animates the jump:

Delete the scroll-margin-top line and click Two again: the heading ends up hidden behind the dark header.

The download attribute asks the browser to save the file instead of opening it. Its value, if you give one, is the suggested file name:

<a href="/files/report-2026.pdf" download>Download the report</a>
<a href="/files/r-8f2a.pdf" download="report.pdf">Download (saved as report.pdf)</a>

download only works for files on the same origin as the page (same scheme, host and port) and for data: and blob: URLs. For a file on another origin, even a subdomain, browsers ignore it and navigate to the file as a normal link. The server can force a download for any file with the Content-Disposition: attachment header.

Screen reader users often move through a page link by link, hearing only the link text. "Click here" and "Read more" tell them nothing.

<!-- Vague: -->
To see our prices, <a href="/pricing">click here</a>.

<!-- Clear: -->
See the <a href="/pricing">pricing plans</a>.

The title attribute shows a tooltip on hover but is not shown on touch screens or to most keyboard users, so do not put anything important in it.

An <a> can wrap block content: a heading, an image and a paragraph can all be one link. This is how clickable cards are built.

display: block makes the whole box clickable, not only the text. Keep the card's content short, because a screen reader reads all of it as the link's name. And never put another link or a button inside: interactive elements cannot be nested in <a>.

Other attributes of the a tag

AttributeWhat it does
hrefThe destination. Without it, <a> is not a link
targetWhere to open it; target="_blank" opens a new tab, see open link in new tab
relThe relationship to the target: noopener, nofollow, sponsored, ugc
downloadSave the file instead of opening it
hreflangThe language of the linked page, as a hint
referrerpolicyHow much of the current URL to send as the referrer

Common mistakes

  • No scheme on an external URL. href="www.site.com" is a relative path. Write https://.
  • <a href="#"> for a JavaScript action. It scrolls to the top and adds # to the URL. If clicking does not go anywhere, use a <button>.
  • A link inside a link, or a button inside a link. Both are invalid HTML: a second <a> makes the parser close the first one early, and a click on a button inside a link has no clear target.
  • Spaces in file names. href="my file.html" usually works because the browser encodes the space, but my-file.html avoids the question entirely.
  • "Click here" as link text. Describe the destination instead.
  • Mismatched id case. #Pricing and id="pricing" are different.

Frequently Asked Questions

How do you make a link in HTML?

Use the anchor element: <a href="https://example.com">Example</a>. href is the destination and the text between the tags is what the reader clicks.

What does href stand for?

Hypertext reference. It holds the URL the link points to: a full address, a path on the same site such as /about, a section of the page such as #pricing, or a scheme like mailto:.

How do I link to a specific part of a page?

Give the target element an id, then link to it with a hash: <h2 id="faq">FAQ</h2> and <a href="#faq">FAQ</a>. From another page, add the hash to the URL: <a href="/help#faq">.

Why does my link go to the wrong page when I write www?

Without https:// the browser treats www.example.com as a relative path on your own site, so the link goes to something like yoursite.com/www.example.com. Always write the scheme for external links: href="https://www.example.com".

Can an a tag contain a div?

Yes. In HTML5 an <a> can wrap block content such as a <div>, a heading and a paragraph, which is how whole-card links are built. It cannot contain another link, a button, or any other interactive element.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED