← Back to HTML & CSS, page 2

html-css-0027 · Easy fresher

How do relative and absolute URLs work in links, and when should you use target="_blank" with rel?

An absolute URL has scheme and host, a root-relative URL starts with a slash, and a relative URL is resolved from the current page's folder. The browser converts all of them to full URLs using the page as the base. target="_blank" opens a new tab, and rel="noopener noreferrer" is a safe habit for external links.

Full answer

A link's href can be written three ways.

  • Absolute URL: the full address with scheme and host, such as https://example.com/courses/css.html. It works from anywhere.
  • Root-relative: begins with /, such as /courses/css.html. It starts from the site's root.
  • Relative: no leading slash, such as css.html, ./css.html or ../index.html. It is resolved from the folder of the current page. .. means one folder up.

A fragment such as #fees jumps inside the page. Other schemes, such as mailto: and tel:, open the mail or phone app.

The browser turns every URL into a full one with the current page as the base. This script shows how:

The output is:

next.html -> https://school.example/courses/css/next.html ./next.html -> https://school.example/courses/css/next.html ../index.html -> https://school.example/courses/index.html /fees -> https://school.example/fees #top -> https://school.example/courses/css/lesson.html#top https://other.example/x -> https://other.example/x

target="_blank" opens a new tab. Without precautions the new page could get access to window.opener. Current browsers treat target="_blank" links as noopener by default, but writing rel="noopener" is still a harmless habit that also covers older browsers. noreferrer also hides the page address from the destination. Tell users when a link opens in a new tab, for example with visible text or an icon with a text label.

html
<pre id="out"></pre>
<script>
  const base = "https://school.example/courses/css/lesson.html";
  const links = ["next.html", "./next.html", "../index.html", "/fees", "#top", "https://other.example/x"];
  document.getElementById("out").textContent =
    links.map(l => l + " -> " + new URL(l, base).href).join("\n");
</script>
<a href="https://other.example" target="_blank" rel="noopener noreferrer">External site</a>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Absolute, root-relative and relative links resolve differently.
  • The .. segment moves one folder up.
  • A fragment like #fees links within the page.
  • target="_blank" with rel="noopener" protects against window.opener access.
  • The base element changes how every relative link resolves.

Common mistakes

  • using ../ paths in templates that are reused at different folder depths, which then break on some pages.
  • using target="_blank" for every link. It takes control away from users, and for screen reader users a new tab can be disorienting.

Follow-ups

  • what does the base element do?
  • What does rel="noreferrer" change?
  • Why do relative links break when a template moves to another folder depth?

Tips

  • Use root-relative or relative links for pages on your own site so the same code works on staging and production.
  • Show the new URL(link, base) trick to prove your answer; it is quick and exact.
  • Mention that new tabs should be announced to users.