Links and URLs

Beginner · 31 min

How the a element and href work, how relative and absolute URLs resolve, how fragments scroll, and what target=_blank does to the opener.

Before thisLists and Description Lists

Dave Gray · 31 min

Links and URLs

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Dave GrayReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A link is `<a href="...">text</a>`. The `href` is what makes it a link. In a test, an `<a>` with no `href` reported an empty `href` and is a placeholder, not a link.
  • A relative URL resolves against the page's own URL. From `/docs/guide/page.html`, `other.html` became `/docs/guide/other.html`, `/root.html` stayed at the site root and `../up.html` became `/docs/up.html`.
  • `#section` links to the element with that `id` on the same page. In a test, clicking it set the URL hash, scrolled the target into view and matched the `:target` pseudo-class.
  • A `<base href>` changes how every relative URL on the page resolves. In a test it rewrote all six relative links. It is rarely worth the surprise.
  • Browsers treat `target="_blank"` as `rel="noopener"`. In a test the new page's `window.opener` was `null`, and only `rel="opener"` restored it.

Before the web, a document sat on its own. The idea behind HTML’s first letter is hypertext: text that can point at other text. One element does that job, the anchor, <a>. The attribute that matters is href, short for hypertext reference.

<a href="https://example.com/pricing">See pricing</a>

Without href it’s an anchor that isn’t a link. In a test, <a>no href</a> reported an empty href property. It is a placeholder, and the spec says it represents a link only when it has an href. Developers sometimes use that on purpose for a placeholder, but it’s the attribute that turns the element into a destination.

URLs: absolute, relative and root-relative

The href value is a URL. There are three shapes, and the difference is what the browser compares it against.

  • Absolute: https://example.com/pricing. Complete, so it points to one place.
  • Relative to the current page: other.html, ../up.html, ?q=2, #sec. The browser resolves them against the URL of the page they’re on.
  • Root-relative: /root.html. Starts at the site root regardless of where the current page lives.

I loaded a page at http://localhost:PORT/docs/guide/page.html?x=1#top in headless Chromium 159 and read the resolved href of each link:

Written Resolved to
other.html /docs/guide/other.html
/root.html /root.html
../up.html /docs/up.html
#sec /docs/guide/page.html?x=1#sec
?q=2 /docs/guide/page.html?q=2
//cdn.example.com/x http://cdn.example.com/x (the page’s own scheme)

Notice that #sec kept the query string and ?q=2 dropped the fragment. Think of relative URLs like giving directions from where you stand: “next door” works from anywhere, but the address it points to depends on where you are. When a page moves, its relative links move with it. Root-relative and absolute links don’t.

A <base href> element in the head changes the starting point for every relative URL on the page. After adding <base href="https://example.org/other/"> in the test, all of the above resolved against example.org, including #sec, which turned into https://example.org/other/#sec. That is almost never what you want for in-page links, so treat <base> with suspicion.

A fragment is the part after #. A link to #deep scrolls to the element with id="deep". In a test, clicking <a href="#deep"> on a long page changed the URL to ...#deep, scrolled the heading into view (scrollY above 2,900) and made document.querySelector(":target") return that heading, which CSS can use through :target. Skip links (“Skip to content”) work this way too.

Opening in a new tab

target="_blank" opens the link in a new browsing context. It used to give the new page a reference back to yours through window.opener, which was a security problem. Current browsers behave as if rel="noopener" is set. In a test, a target=_blank link opened a page whose window.opener was null, and adding rel="opener" made it a real Window again. MDN’s compat data puts this default at Chrome 88, Firefox 79 and Safari 12.1. The security lesson covers noopener, noreferrer and why you still see both written out. Don’t open new tabs by habit: people can do it themselves, and it surprises screen reader users.

A link’s destination isn’t always a page. mailto:a@b.co?subject=Hi%20there opens an email client with a subject, tel: starts a call on a phone, and the download attribute asks the browser to save the file rather than display it. In a test the mailto: URL passed through unchanged, with the encoded space intact.

Write link text that makes sense out of context. Screen reader users often call up a list of every link on a page, and “click here” and “read more” repeated ten times tells them nothing. Compare “Download the 2026 report (PDF)”.

What the video covers

The embed is Dave Gray’s “How to Add Links in HTML code | HTML5 Linking Tutorial” (December 2021, about 31 minutes). The chapters cover the anchor element (1:53), absolute versus relative references (4:37), linking to another page (5:45), a challenge and its solution (6:40 and 7:28), linking to sections in a page (10:44), link to the top (16:14), IDs in the web address (17:03), anchor colours (17:56), link text best practices (19:55), download links (22:45), email links (24:32), phone links (26:10), opening a link in a new tab (27:21) and the default page (28:48). It is long, but nearly every chapter maps to this lesson. I could not read a transcript, so I describe it from the chapters. The resolution table, the base and noopener results and the :target test are this lesson’s own. Kevin Powell’s shorter link lesson is the deeper pick.

Where this fits

Links finish the first group’s core elements, after lists. Next, entities, whitespace and comments covers how special characters in link text and URLs are written. Links also anchor navigation in the landmarks lesson that starts the next group.

Check yourself

  1. 01A page at `https://example.com/blog/post.html` contains `<a href="../about.html">`. Where does it point?

    To `https://example.com/about.html`. `..` goes up one directory from `/blog/`, then `about.html` is resolved from there. A leading slash, as in `/about.html`, would reach the same place from the site root regardless of where the page lives.

  2. 02What is the difference between `href="page.html"` and `href="/page.html"`?

    The first is relative to the current page's directory, so it changes meaning if the page moves. The second is relative to the site root and always points to the same path on the same site. In a test on `/docs/guide/page.html`, they resolved to `/docs/guide/page.html` and `/page.html`.

  3. 03You add `<base href="https://example.org/other/">` to a page. What happens to a link `<a href="#top">`?

    It stops pointing at the current page. A `#top` link is resolved against the base URL, so it points at `https://example.org/other/#top`. In a test, the fragment-only link changed from the page URL to the base URL once `<base>` was added.

  4. 04Why is a link whose text is just 'click here' a problem?

    Assistive technology can list all links on a page out of context, and 'click here' tells the person nothing. Link text should make sense on its own, such as 'Download the 2026 report (PDF)'. Descriptive text also helps search engines and anyone scanning the page.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark