HTML Link Code: How to Make a Link in HTML
The HTML code for a link is <a href="https://example.com">Link text</a>. The href attribute holds the address the link goes to, and the text between the opening and closing tags is what readers see and click. The same element links to other sites, to pages on your own site, to a section of a page or to a file.
The <a> element, short for anchor, is the oldest idea on the web: pages pointing to other pages. This guide shows the variations you will actually use, each with a live example you can open in HTMLpad and change.
The basic link
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Links</title></head>
<body>
<p>Read the <a href="https://developer.mozilla.org/">MDN web docs</a> for more.</p>
<p>Or go back to the <a href="/">home page</a>.</p>
</body>
</html>
Everything about a link comes down to two choices: where it goes (href) and what it says (the text). The MDN reference for the anchor element lists every attribute, but most links never need more than these two.
Absolute and relative addresses
An absolute address includes the whole thing, https:// and the domain. Use it for links to other websites.
A relative address is read from the page the link is on. Use it for links within your own site, so they keep working if the domain changes.
| You write | The link goes to |
|---|---|
href="https://example.com/about/" |
That exact address |
href="about.html" |
about.html in the same folder as the current page |
href="/about/" |
/about/ at the root of the current site |
href="../index.html" |
index.html one folder up |
The same rules apply to images and stylesheets. If a link works online but not when you open the file on your computer, a path starting with a slash is the likely cause, as I explain in how to link CSS to HTML.
Open a link in a new tab
Add target="_blank". When you do, also add rel="noopener", which stops the new page from getting control over the page that opened it. Modern browsers apply this by default, but writing it costs nothing and covers older ones.
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>New tab</title></head>
<body>
<p><a href="https://example.com/" target="_blank" rel="noopener">Opens in a new tab</a></p>
<p><a href="https://example.com/">Opens in the same tab</a></p>
</body>
</html>
Use new tabs sparingly. Readers can always choose to open a link in a new tab themselves, but they cannot easily stop a page from doing it to them. New tabs make sense for links that would interrupt a task, such as help pages linked from a form.
Link to a section of the same page
Give the target an id, then link to it with a hash. This is how tables of contents work.
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Jump links</title>
<style>section { min-height: 90vh; border-top: 1px solid #ccc; }</style></head>
<body>
<nav><a href="#prices">Prices</a> · <a href="#contact">Contact</a></nav>
<section id="prices"><h2>Prices</h2></section>
<section id="contact"><h2>Contact</h2><a href="#">Back to top</a></section>
</body>
</html>
You can combine both: href="/guide/#prices" opens another page and scrolls to its section with the id prices.
Link to a file, an email address or a phone number
- A file to download:
<a href="report.pdf" download>Download the report</a>. Thedownloadattribute asks the browser to save the file rather than open it. It only works for files on the same site. - An email address:
<a href="mailto:hello@example.com">Email us</a>. I cover subjects and pre-filled messages in the HTML email link guide. - A phone number:
<a href="tel:+15551234567">Call us</a>. On a phone, tapping it starts a call.
Links that look like buttons, and images that are links
A link can wrap an image, so clicking the picture follows the link; see how to make an image a link. It can also be styled to look like a button; see the HTML button link guide, which also explains when you should use a real button instead.
Write link text people can understand
Search engines and screen readers both rely on link text. "Click here" tells neither of them anything, and a list of five "click here" links read aloud is useless. Describe where the link goes: "download the 2026 price list" rather than "click here for the list". The W3C's guidance on link purpose explains why this matters for accessibility.
Common mistakes
- Forgetting
https://.href="example.com"is read as a file called example.com on your own site. External addresses need the full prefix. - Spaces in file names. Rename
my page.htmltomy-page.html, or the link may break on some servers. - Nesting links. A link inside another link is invalid HTML and browsers handle it unpredictably.
- Missing closing tag. Without
</a>, everything after the link becomes part of it.
To try changes quickly without setting anything up, paste your page into the HTML sandbox and edit it there. For more on working with existing files, see how to edit an HTML file and the main guide to opening HTML files.
Frequently asked questions
What does href mean?
It stands for hypertext reference. It is the attribute that holds the address a link points to.
How do I make a link open in a new tab?
Add target="_blank" rel="noopener" to the <a> tag. Use it for links that would interrupt what the reader is doing, not for every external link.
Why does my link go to the wrong place?
Usually the address is missing https://, or a relative path is being read from a different folder than you expect. Hover over the link and check the address your browser shows at the bottom of the window.
Can a link go to a specific part of another page?
Yes, if that part has an id. Add a hash and the id to the address, as in /guide/#prices.
