Words like "See the menu", "Contact us" or "Read more" take you to another page when you click them. Words you can click to go to another page are called links. In HTML, you write a link with the <a> tag (from anchor). The address the link goes to is in the href attribute. This is the href from the images lesson, which did not work on <img>. Its right place is on <a>.
Writing a link
Here is one link:
<a href="https://www.google.com">Visit Google</a>
<a ...>is the opening tag.href="https://www.google.com"is where the link goes. It's an attribute, just likesrcon<img>: extra information inside the opening tag, written asname="value".Visit Googleis the text visitors see and click.</a>is the closing tag.
The result: the words Visit Google show in blue, with an underline. When you click them, the browser goes to Google.
What makes an <a> clickable is its href. Without href, an <a> is plain text: you can see it, but it doesn't go anywhere.
Other sites need the full address
To go to another site, write its full address, starting with https://:
<a href="https://www.google.com">Google</a>
<a href="www.google.com">Google</a>
On the page, these two links look the same, but only the link on the first line opens Google. The link on the second line has no https://. So the browser reads www.google.com as the name of a file on your own site. It looks for yoursite.com/www.google.com, a page that doesn't exist.
For pages on your own site, the file name is enough, for example href="contact.html". The file name alone works as long as the file is in the same folder as the page that is open.
When your site's files are in several folders, fill in href in one of these ways:
| Write | Meaning | When to use it |
|---|---|---|
blog/recipe.html |
go into the blog folder |
the blog folder is in the same folder as this page |
../index.html |
go up one folder | this page is inside a folder, such as blog/recipe.html, and the file you link to is outside that folder |
/contact.html |
start from the top folder of the site | the address is the same from any page. Works only when the site is opened through a server (a computer that serves the site), not in a file opened with a double-click |
Opening in a new tab
The target attribute sets where the link opens. The value _blank (with an underscore in front) means a new tab:
<a href="https://www.google.com" target="_blank" rel="noopener">Google</a>
When you click it, Google opens in a new tab, and your page stays open in the old tab.
rel="noopener" is a protection that people usually write together with target="_blank". With this attribute, the page in the new tab can't use window.opener to replace the page in your old tab with another site, such as a fake site. window.opener is part of JavaScript. You don't need to know how it works. Modern browsers already add this protection to every target="_blank" link. Writing rel="noopener" still helps older browsers, so make it a habit to write these two attributes as a pair.
Jumping to a part of the same page
A link can also jump to one part of the page that's already open:
<a href="#prices">See prices</a>
<h2 id="prices">Prices</h2>
href="#prices"is a hash sign#followed by a name. It means: jump to the element namedpriceson this page.id="prices"gives that name to the heading you're jumping to.idis an attribute that names one element. You can use each name only once per page.
When you click "See prices", the page scrolls directly to the Prices heading.
Email links use mailto:
Put mailto: in href, followed by the email address, with no spaces:
<a href="mailto:[email protected]">Contact Us</a>
When a visitor clicks it, their email app opens with [email protected] already filled in as the recipient.
The same idea works for phone calls: <a href="tel:+15550123456">Call us</a> starts a call on a phone. For WhatsApp, use https://wa.me/ and then the number in international format (starting with the country code, here 1), with no +, spaces or leading zero: <a href="https://wa.me/15550123456">Chat on WhatsApp</a>.
⚠️ Links that go to the wrong place, vanish, or hide where they lead
- A missing
>in the opening tag. In<a href="https://www.google.com"Visit Google</a>, the link text disappears, because the browser reads that text as part of the tag. - A missing
https://. The link goes to a file on your own site instead. href="#"used as a temporary address. A link withhref="#"only jumps to the very top of the page. Givehrefa real destination.- "Click here" as the link text. Users of screen readers (programs that read the page aloud) often hear only the list of links, with none of the text around them. "Click here" doesn't tell them where the link goes. So say where the link goes: "See plan prices", not "click here".
TL;DR: A link is <a href="address">text</a>, and the text between the tags is what people click. Without href, an <a> can't be clicked. Other sites need the full address from https://. For a new tab, use target="_blank" with rel="noopener". To jump to a part of the page: href="#id", which is # and then the name set with id on the target element. An email link: href="mailto:address".
⚡ Text you can click
Write:
<a href="https://www.google.com">Visit Google</a>
Guess first: if you delete the https://, where does the link go?
Not to Google. The browser reads www.google.com as a file name on your own site, so it looks for yoursite.com/www.google.com, a page that doesn't exist.
Meaning: <a> makes a link. href holds the address the link goes to, and the text between the tags is what people click. Without href, an <a> is plain text. Other sites need the full address from https://. Pages on your own site only need the file name.
⚡ Open in a new tab
Write:
<a href="https://www.google.com" target="_blank" rel="noopener">Google</a>
Meaning: target="_blank" opens the link in a new tab. rel="noopener" is its protection: the page in the new tab can't change your old tab. Modern browsers already add this protection, but still write the two as a pair for older browsers.
⚡ Jump within the same page
Write:
<a href="#prices">See prices</a>
<h2 id="prices">Prices</h2>
Meaning: Put a hash sign # in href, then the name of the place to go. You give that name to the target element with id, and you can use each name only once per page.
⚡ An email link
Write:
<a href="mailto:[email protected]">Contact Us</a>
Meaning: Put mailto: in href, followed by the email address, with no spaces. When a visitor clicks it, their email app opens with the recipient already filled in.