Lesson 5 +10 XP

Links

Links are how we travel around the web. They're the "hyper" in HyperText!

The <a> tag

<a> stands for anchor. You put the destination in the href attribute and the clickable text inside.

Code

html
<a href="https://example.com">Visit Example</a>

href = &quot;hypertext reference&quot; = where the link goes.

Types of addresses

  • Absolute URL: the whole address including https://:
Code

html
  <a href="https://example.com">Example</a>
  • Relative URL: an address relative to your page:
Code

html
  <a href="about.html">About us</a>
  • Same-page anchor: jump to a spot on the same page using # and an id:
Code

html
  <a href="#top">Back to top</a>
  • Email: mailto: opens the email app:
Code

html
  <a href="mailto:me@example.com">Email me</a>
  • Phone: tel: dials a number on phones:
Code

html
  <a href="tel:+15551234567">Call us</a>

Opening in a new tab: target

target=&quot;_blank&quot; opens the link in a new tab.

Code

html
<a href="https://example.com" target="_blank">Open in new tab</a>

When you open in a new tab, it's smart to add rel=&quot;noopener&quot; too. It keeps the new page from messing with your page (a security thing):

Code

html
<a href="https://example.com" target="_blank" rel="noopener">Safe new tab</a>

The rel attribute

rel describes the relationship between your page and the link:

  • rel=&quot;noopener&quot;: security helper for new tabs
  • rel=&quot;noreferrer&quot;: don't tell the next site where you came from
  • rel=&quot;external&quot;: the link goes to another website

Downloads

Add download to make a file download instead of opening:

Code

html
<a href="file.pdf" download>Download the PDF</a>

Always say where you're going!

A great habit: make link text describe the destination.

  • Good: &quot;Read the documentation&quot;
  • Bad: &quot;Click here&quot;

Screen readers and search engines thank you!

TL;DR

  • &lt;a href=&quot;...&quot;&gt;text&lt;/a&gt; makes a link.
  • Addresses can be absolute, relative, page anchors, emails, or phones.
  • target=&quot;_blank&quot; = new tab, use with rel=&quot;noopener&quot;.
  • download makes files download.
  • Say where links go in the text.