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 covers the three ways an href (or an image src) can point to a resource, and when to use each.
The three forms
A URL in an attribute can be written in three ways. The browser resolves each one against the address of the current page, called the base URL.
| Form | Example | Resolved against |
|---|---|---|
| Absolute | `https://example.com/` | Nothing. It is complete. |
| Root-relative | `/docs/intro.html` | The site's origin (scheme and host) |
| Relative | `guides/setup.html` | The folder of the current page |
<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>Absolute URLs
An absolute URL includes the scheme (https://), the host, and the path. Use it for links to other websites. It works from any page, but it ties the link to one domain, so you would have to edit every link if the site moved.
Root-relative URLs
A root-relative URL starts with a single slash. The browser keeps the current scheme and host and replaces the whole path. If the page is https://example.com/blog/2024/post.html, then /docs/intro.html becomes https://example.com/docs/intro.html.
The link means the same thing on every page of the site. This makes it a good choice for shared elements such as headers and menus.
Relative URLs
A relative URL has no leading slash. The browser starts from the folder that contains the current page and follows the path you give.
setup.htmlis a file in the same folder.guides/setup.htmlis a file in a subfolder.../index.htmlgoes up one folder, then finds the file.../../index.htmlgoes up two folders.
For the page https://example.com/blog/2024/post.html, the folder is /blog/2024/. So ../index.html resolves to https://example.com/blog/index.html.
flowchart TDThe diagram below shows how one page resolves each form.
Choosing between them
- Use absolute URLs for other sites.
- Use root-relative URLs for links inside your site when the site is served from the domain root.
- Use relative URLs for files that sit close together and move together, such as a page and its images.
Common errors
- Root-relative links break when tested from disk. Opening
file:///home/me/site/index.htmlmakes/docs/intro.htmlpoint to the root of your file system, not your project. Use a local web server, or use relative URLs. - Root-relative links break in a subfolder deployment. If the site lives at
https://example.com/project/, then/docs/intro.htmlskips/project/. Relative URLs survive this move. - Moving a page breaks relative links. The same
../index.htmlpoints somewhere else once the page sits in a different folder. - Missing
https://.href="example.com"is treated as a relative path to a file namedexample.com, not as a website.
Exercise
Your page is at https://example.com/shop/shoes/boots.html. What full URL does href="../hats.html" resolve to, and what does href="/hats.html" resolve to?
Show answer
"../hats.html" goes up from /shop/shoes/ to /shop/, giving https://example.com/shop/hats.html. "/hats.html" replaces the whole path, giving https://example.com/hats.html.