Sub topic 3
Absolute, root-relative, and relative URLs
This chapter assumes you can write a basic HTML document, open it in a browser, and read file and folder paths. It also assumes you know the <a> element and its href attribute from the previous chapter. Everything here applies equally to src on images, which later chapters use.
The three forms of a URL
An href value can point to the same place in three different ways. The browser turns each one into a full address before it requests anything.
- An absolute URL includes the scheme and host:
https://example.com/docs/intro.html. - A root-relative URL starts with a single slash and is resolved from the root of the current site:
/docs/intro.html. - A relative URL has no leading slash and is resolved from the folder of the current page:
guides/setup.htmlor../index.html.
<a href="https://example.com/">Example site</a>
<a href="/docs/intro.html">Introduction (root-relative)</a>
<a href="guides/setup.html">Setup guide (relative)</a>
<a href="../index.html">Back to the parent folder</a>The first link works from any page on any site. The second works from any page on the same site. The last two depend on where the current page lives.
How the browser resolves a URL
The browser takes the address of the current page, called the base URL, and combines it with the href value. Suppose the page is https://example.com/docs/guides/setup.html. The browser resolves links like this:
- An absolute URL is used as written.
- A root-relative URL keeps the scheme and host of the base and replaces the whole path.
- A relative URL drops the file name from the base path, then appends the
hrefvalue.
The file name is dropped before appending. From /docs/guides/setup.html, the link other.html becomes /docs/guides/other.html, because the folder is /docs/guides/.
A sample site
The examples below use this structure.
The table shows how to link from /docs/guides/setup.html to other files.
| Target | Relative | Root-relative |
|---|---|---|
| `/docs/guides/other.html` (same folder) | `other.html` or `./other.html` | `/docs/guides/other.html` |
| `/docs/intro.html` (one level up) | `../intro.html` | `/docs/intro.html` |
| `/index.html` (two levels up) | `../../index.html` | `/index.html` |
| `/images/logo.svg` | `../../images/logo.svg` | `/images/logo.svg` |
Relative path syntax
Relative paths use three building blocks.
name.htmlor./name.htmlmeans a file in the current folder. The./is optional.folder/name.htmlmeans a file in a subfolder.../means go up one folder. You can chain it:../../goes up two.
<!-- Page: /docs/guides/setup.html -->
<a href="other.html">Same folder</a>
<a href="advanced/tuning.html">Subfolder</a>
<a href="../intro.html">One level up</a>
<a href="../../index.html">Two levels up</a>You cannot go above the site root. A path such as /../x.html is resolved as /x.html.
When to use each form
Choose by what should stay stable when something moves.
| Form | Best for | Breaks when |
|---|---|---|
| Absolute | Other websites, canonical links, email and social content | You change your domain or switch between `http` and `https` |
| Root-relative | Navigation, headers, footers and assets shared by many pages | The site is deployed in a subfolder instead of at the domain root |
| Relative | Links between files that move together, such as a chapter and its images | You move one file without the other |
A common rule is this. Use absolute URLs for other sites, root-relative URLs for shared site-wide parts, and relative URLs for files that live together. A shared navigation menu written with relative URLs would need different paths on every page depth. With root-relative URLs, the same markup works everywhere.
Common errors
Root-relative links on a subfolder site
If your site is served from https://user.github.io/my-project/, then /docs/intro.html points to https://user.github.io/docs/intro.html. That is outside your project, so the link fails. Either use relative URLs, or include the project folder in every path: /my-project/docs/intro.html.
Root-relative links opened from disk
If you double-click an HTML file, the browser uses a file: URL. The root / then means the root of your disk, not of your project folder. Links such as /images/logo.svg fail. Run a local server from the project folder to test root-relative paths.
python3 -m http.server 8000Then open http://localhost:8000/ in the browser.
The trailing slash changes the base
A folder URL with a trailing slash and one without are different bases. Take the link setup.html:
- On
https://example.com/docs/guides/, it resolves to/docs/guides/setup.html. - On
https://example.com/docs/guides(no slash), the last segmentguidesis treated as a file name and dropped. It resolves to/docs/setup.html.
Most servers redirect folder URLs to the slash version, but not all do. Links that behave oddly only on the live site are often caused by this.
Letter case and spaces
Most web servers treat file paths as case-sensitive. A link to Images/Logo.svg fails if the file is images/logo.svg, even if it worked on your Windows or macOS machine. Use lowercase names with hyphens. A space in a path must be written as %20, so avoid spaces altogether.
Backslashes and drive letters
URLs use forward slashes. ..\index.html and C:\site\index.html are not valid paths for the web, even if a local test happens to work.
Mixing http and https
An absolute http:// link on an https:// page can trigger mixed-content blocking when it loads a resource such as an image. Use https:// for any site that supports it.
Query strings and fragments
Any of the three forms can end with a query string, a fragment, or both.
<a href="/search.html?q=lists&page=2">Search results</a>
<a href="guides/setup.html#install">Installation section</a>
<a href="https://example.com/docs/intro.html?lang=en#top">Intro, English, top</a>The query string starts with ? and is sent to the server. The fragment starts with # and is used only by the browser, to scroll to the element with that id. The next chapter covers fragments in detail.
Changing the base with the base element
An optional <base> element in the <head> sets the base URL for all relative and root-relative links on the page.
<head>
<base href="https://example.com/docs/">
</head>
<body>
<a href="intro.html">Resolves to https://example.com/docs/intro.html</a>
</body>It affects every link and every image on the page, including fragment-only links such as #install, which then point at the base page instead of the current one. Because of that surprise, most projects avoid it unless there is a clear need.
Checking a link
When a link fails, resolve it by hand. Write down the current page's full URL, remove the file name, apply the href value step by step, and compare the result with the real file location. Then open the browser's developer tools, look at the Network tab, and check the exact address that returned a 404 status.
Exercise
The page is https://example.com/docs/guides/setup.html. Write one relative URL and one root-relative URL that link to https://example.com/images/logo.svg.
Show answer
Relative: ../../images/logo.svg (up out of guides, up out of docs, then into images). Root-relative: /images/logo.svg.
Exercise
A site is published at https://user.github.io/my-project/. A page contains <a href="/about.html">About</a>, and the file is my-project/about.html. The link returns a 404 error. Why, and how can you fix it?
Show answer
The leading slash resolves from the domain root, so the browser requests https://user.github.io/about.html, which is outside the project. Fix it with a relative URL (about.html, or ../about.html from a subfolder) or by including the project folder: /my-project/about.html.
Exercise
A page at https://example.com/docs/guides (no trailing slash) contains <a href="setup.html">. Where does the link go?
Show answer
To https://example.com/docs/setup.html. Without a trailing slash, guides is treated as a file name and dropped, so the base folder is /docs/.