Lesson 3 of 29 · HTML Basics
HTML Links: The a Tag and href
Links are what turn single pages into a web. They are the "HyperText" in HTML: click one, and the browser takes you to another page, another website or another part of the same page. In this lesson you'll write all three kinds.
The <a> element
A link is an <a> element, short for anchor. Its href attribute (hypertext reference) says where the link goes, and the content between the tags is the clickable link text:
<a href="https://developer.mozilla.org">MDN Web Docs</a>
An attribute is extra information inside an opening tag, written as name="value". You'll meet many of them — and href is the most important of all.
Link text that makes sense on its own
Screen reader users often pull up a list of all the links on a page, and search engines use link text to learn what the target page is about. "Click here" tells neither of them anything. Describe the destination instead: "Read the MDN guide to HTML links".
Absolute and relative URLs
An absolute URL is a complete address, including the protocol: https://www.wikipedia.org. Use it for pages on other websites.
A relative URL is a path from the current page. Use it for pages on your own site — it keeps working even if the whole site moves to a new domain:
<a href="https://www.wikipedia.org">Wikipedia</a> <!-- another website -->
<a href="contact.html">Contact</a> <!-- a page in the same folder -->
<a href="/courses">All courses</a> <!-- from the root of this site -->
Opening a link in a new tab
target="_blank" opens the link in a new tab. Always pair it with rel="noopener": without it, the new page gets a handle on your page (window.opener) and could quietly redirect it somewhere nasty.
<a href="https://developer.mozilla.org" target="_blank" rel="noopener">MDN Web Docs</a>
Use new tabs sparingly: visitors can always open one themselves with a middle click or Ctrl-click when they want to.
Jumping to a section of the page
Any element with an id can be a link target. Put # plus the id in the href, and clicking the link scrolls straight to that element:
<a href="#rules">Read the rules</a>
...
<h2 id="rules">Rules</h2>
An id must be unique on the page and can't contain spaces. You can even combine both ideas: href="guide.html#rules" opens another page and jumps to its rules section.
Common mistakes
- Leaving out
https://.href="www.wikipedia.org"is treated as a relative path, so the browser looks for a page calledwww.wikipedia.orgon your own site. - Putting the
#in the id. It'sid="faq"on the target andhref="#faq"on the link. target="_blank"withoutrel="noopener". Always add both together.- Vague link text like "here" or "more".
Assignment
Replace the comment in the first paragraph with three links:
MDN Web Docs— links tohttps://developer.mozilla.organd opens in a new tab, safely:target="_blank"andrel="noopener".Contact— links to the relative URLcontact.html.Jump to the FAQ— links to the section with the idfaq.
Then give the FAQ heading at the bottom of the page the id faq. In the Preview, the last link should now scroll down to the FAQ.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .