HTML File Paths

HTML Living Standard

Imagine catalog.html is inside shop/pages, product.svg is inside shop/images and logo.svg is at the site root under assets. Each example shows the URL the browser resolves for that folder layout.

On this page

Resolve ./ and ../ from the current file

./ starts in the current folder and ../ moves up one folder. HTML uses these paths in attributes such as href and src. When you move the HTML file, check any paths that start from its folder.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Resolve ./ and ../ from the current file</title>
</head>
<body>
<p id="examples"><a href="./hours.html">Hours</a> · <a href="../images/product.svg">Product image</a></p>
<p id="paths" style="white-space: pre-wrap; overflow-wrap: anywhere"></p>
  <script>
const pageURL = "https://example.com/shop/pages/catalog.html";
const examples = [...document.querySelectorAll("#examples a")];
document.querySelector("#paths").textContent = examples.map(link => {
  const path = link.getAttribute("href");
  return path + " → " + new URL(path, pageURL).href;
}).join("\n");
  </script>
</body>
</html>

What to expect ./hours.html resolves inside /shop/pages/. ../images/product.svg resolves inside /shop/images/.

Read root-relative and full URLs

A slash at the beginning starts from the root of the current origin. A full URL includes the protocol and host and can point at another website.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Read root-relative and full URLs</title>
</head>
<body>
<p id="examples"><a href="/assets/logo.svg">Site logo</a> · <a href="https://static.example.org/logo.svg">External logo</a></p>
<p id="paths" style="white-space: pre-wrap; overflow-wrap: anywhere"></p>
  <script>
const pageURL = "https://example.com/shop/pages/catalog.html";
const examples = [...document.querySelectorAll("#examples a")];
document.querySelector("#paths").textContent = examples.map(link => {
  const path = link.getAttribute("href");
  return path + " → " + new URL(path, pageURL).href;
}).join("\n");
  </script>
</body>
</html>

What to expect /assets/logo.svg keeps example.com. The full URL uses static.example.org.

Connect three pages

Create index.html, projects.html and contact.html in one folder. Place the same list of descriptive links on each page. Move projects.html into a work folder, then fix both the link to it and its return link.

Official references