The HTML a tag (links)

By

Published · Updated

Tagged: HTML

Learn how to create links with the HTML a tag: absolute and relative URLs, opening a new tab safely, jumping to a section, mailto and tel links, downloads, and good link text.

Links are defined using the a tag. The link destination is set via its href attribute.

Example:

<a href="https://flaviocopes.com">click here</a>

Between the starting and closing tag we have the link text.

The above example is an absolute URL. Links also work with relative URLs:

<a href="/test">click here</a>

In this case, when clicking the link the user is moved to the /test URL on the current origin.

Be careful with the / character. If omitted, instead of starting from the origin, the browser resolves the link starting from the current folder.

Example, I’m on the page https://flaviocopes.com/axios/ and I have these links:

The trailing slash matters too. On https://flaviocopes.com/axios (no slash at the end), axios is treated as a file, not a folder, so test brings me to https://flaviocopes.com/test.

Link tags can include other things inside them, not just text. For example, images:

<a href="https://flaviocopes.com">
	<img src="test.jpg" alt="Flavio Copes home page">
</a>

When the image is the only content, its alt text is the link text, so describe where the link goes.

You can also wrap whole blocks, like a card with a heading and a paragraph.

What you can’t put inside a link is interactive content: other <a> tags, buttons, form fields, or elements with a tabindex attribute.

If you want to open the link in a new tab, you can use the target attribute:

<a href="https://flaviocopes.com" target="_blank">open in new tab</a>

You might have seen rel="noopener" added to these links. Without it, the page you open could use window.opener to change the URL of your tab. Modern browsers now apply noopener automatically when you use target="_blank", so it’s only needed for older browsers.

If you also don’t want the other site to know which page the visitor came from, add noreferrer:

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

Opening a new tab can surprise people, especially screen reader users. If you do it, say so in the link:

<a href="https://example.com" target="_blank">example (opens in new tab)</a>

Jumping to a section of the page

Give the target element an id, then link to it with # and the id:

<a href="#install">Go to the install section</a>

<h2 id="install">Install</h2>

You can do the same across pages with /docs/#install.

href="#top" or just href="#" brings you back to the top of the current page. See using the hashtag in links for more on this.

Links are not just for web pages. These open the email app, the phone dialer or the messages app:

<a href="mailto:hello@example.com">Send me an email</a>
<a href="tel:+15555550123">Call us</a>
<a href="sms:+15555550123">Send a text</a>

Write phone numbers in international format, with the country code, so they work from any country.

The download attribute tells the browser to download the file instead of opening it:

<a href="/files/report.pdf" download>Download the report</a>

Give it a value to suggest a file name:

<a href="/files/report-2026-final-v3.pdf" download="report.pdf">Download the report</a>

The catch: it only works for files on the same origin as your page (plus blob: and data: URLs). On a link to another domain, the browser ignores download and opens the file normally. For other ways to make that work, see how to force download links.

Don’t use a link as a button, with href="#" or href="javascript:void(0)" and a click handler. Use a link when you go to a URL, and a <button> when you do something on the page, like opening a menu or submitting a form. Buttons and links behave differently with the keyboard and with screen readers.

Screen reader users often pull up a list of all the links on a page, without the text around them. A list of ten “click here” links tells them nothing.

So instead of this:

To see our products, <a href="/products">click here</a>.

write this:

See <a href="/products">our products</a>.

It’s also better for search engines, which use the link text to understand the page you link to.

Other attributes

hreflang and type don’t change what the browser does, they’re just information.

Want me to talk about your product? You can sponsor this site.

Related posts about html: