On this page
- Key Takeaways
- The Three URL Forms
- How the Document Path Changes a Relative Link
- The <base> element can change the result
- Which Form Should You Use?
- Site-wide navigation: usually root-relative
- Reusable components: match the deployment model
- External and fixed-host destinations: use absolute URLs
- Check a Link in Your Browser
- Common Mistakes
- Best Practices
- Conclusion
Choosing between absolute vs relative URLs in HTML affects where links and assets point when a page moves, a component is reused, or a site is deployed under a different domain. The best choice depends on what should stay fixed: the destination’s domain, its position within the site, or its position relative to the current page.
Key Takeaways
- Use an absolute URL when the destination should include a specific scheme and domain, such as a link to another website.
- Use a root-relative URL for site-wide navigation and assets when the site is served from the domain root.
- Use a path-relative URL for files that should move together, but account for the current document’s directory.
- Relative URLs resolve against the document’s base URL, which an HTML
<base>element can change. - Test links in each deployment environment, especially if the site is hosted under a path prefix such as
/docs/.
The Three URL Forms
Assume a page is loaded at https://example.com/guides/html/page.html and has no <base> element:
| Form | HTML example | Resolved destination |
|---|---|---|
| Absolute | <a href="https://example.com/about.html">About</a> | https://example.com/about.html |
| Root-relative | <a href="/about.html">About</a> | https://example.com/about.html |
| Path-relative | <a href="about.html">About</a> | https://example.com/guides/html/about.html |
| Parent-relative | <a href="../about.html">About</a> | https://example.com/guides/about.html |
An absolute URL includes the scheme and host. A root-relative URL begins with one slash and is relative to the current origin’s root. A path-relative URL is resolved relative to the base URL’s path; .. moves up a path level.
Two leading slashes have a different meaning: //other.example/path specifies a host while inheriting the current scheme. It is not root-relative. Prefer an explicit scheme when linking to an external site.
These examples are based on established URL-resolution behavior, but they have not been independently checked against the current WHATWG URL Standard or executed in browsers for this article. The browser-console workflow below lets you check them in your own environment.
Advertisement
How the Document Path Changes a Relative Link
For a base URL ending in a filename, a path-relative link normally replaces that final path segment. For a base ending in /, it is appended to the directory path.
| Document URL | Link | Resolved URL |
|---|---|---|
https://example.com/guides/html/page.html | images/logo.svg | https://example.com/guides/html/images/logo.svg |
https://example.com/guides/html/ | images/logo.svg | https://example.com/guides/html/images/logo.svg |
https://example.com/guides/html | images/logo.svg | https://example.com/guides/images/logo.svg |
The last two rows differ only by a trailing slash. URL resolution treats html in the last row as the final path segment to replace; it does not know whether your server considers that path a directory. A server redirect that adds a slash can therefore change the document URL and the base used for relative links.
The same principle applies to asset attributes:
<img src="images/logo.svg" alt="Company logo">
On a page at /guides/html/page.html, this requests /guides/html/images/logo.svg. Moving the page to /news/page.html changes the resolved asset path to /news/images/logo.svg.
The <base> element can change the result
A page can explicitly set the base for relative URLs:
<head>
<base href="https://example.com/shared/">
</head>
<body>
<a href="help.html">Help</a>
</body>
Here, the link resolves to https://example.com/shared/help.html, regardless of the page’s own path. A <base> element can also affect root-relative references and fragment links by changing the base origin or path. Check for it before diagnosing a surprising link destination.
Which Form Should You Use?
Site-wide navigation: usually root-relative
For a site deployed at the origin root, root-relative links work consistently from pages at different depths:
<nav>
<a href="/">Home</a>
<a href="/products/">Products</a>
<a href="/contact/">Contact</a>
</nav>
The same markup can be used on both /index.html and /guides/html/page.html without counting ../ segments. It also follows the current origin, so it can work across local, staging, and production domains.
Deployment caveat: /products/ means the origin’s root, not the root of your application. If your application is hosted at https://example.com/my-app/, that link points to https://example.com/products/, not /my-app/products/. Configure your framework or build system for the deployment prefix, or include the prefix in generated links.
Reusable components: match the deployment model
A shared header containing href="contact.html" resolves differently on each page where it appears. If it should always link to one site-wide contact page, use a root-relative or deployment-aware generated URL instead.
Path-relative links can still be useful inside a self-contained group of pages and assets that moves together. For example, ./images/diagram.svg can continue to work when an entire documentation directory is moved, as long as the page and image keep the same relative positions. Moving only the page or only the image breaks that relationship.
An absolute URL in a reusable component fixes the destination to a particular host. That may be intentional for an external service, but a hard-coded production domain can make a staging page link back to production.
External and fixed-host destinations: use absolute URLs
Use an absolute URL when the target should remain on a specific website:
<a href="https://docs.example.net/getting-started/">Read the documentation</a>
Absolute URLs make the destination’s scheme and host explicit, but they may need updating after a domain migration. They can also make preview environments point at the wrong host if used indiscriminately for internal navigation.
Advertisement
Check a Link in Your Browser
Open the page in a browser, open its developer console, and inspect the effective base and a resolved URL:
console.log(document.baseURI);
console.log(new URL('../images/logo.svg', document.baseURI).href);
If the page’s base is https://example.com/guides/html/page.html, the second line should show https://example.com/guides/images/logo.svg. If the first line is unexpected, check the page URL and any <base> element.
To inspect an actual link, select its element in the browser’s Elements panel and examine its href property in the console. For example, in browsers that expose the selected element as $0:
console.log($0.getAttribute('href')); // Markup value
console.log($0.href); // Resolved URL for an anchor
Then follow the link or inspect the network request. A correctly resolved URL can still lead to a missing page, a redirect, or a server configuration error. Repeat the check on nested pages and in each deployment environment.
Common Mistakes
- Assuming a leading slash means the application root. It means the origin root, which may be above your application’s deployment directory.
- Ignoring trailing slashes.
/guides/htmland/guides/html/can resolve the same relative link differently. - Using
../in shared navigation without testing every page depth. The result depends on the base URL where the component is rendered. - Hard-coding the production domain for every internal link. Preview and staging pages may unexpectedly send visitors to production.
- Overlooking
<base>. It can change the resolution of links and assets throughout a document. - Assuming a relative path reflects a server’s filesystem layout. The browser resolves a URL; the server decides how that URL maps to content.
Best Practices
- Decide whether each link should follow the current origin, a page-relative directory, or a fixed external host.
- Use a consistent deployment-aware approach for shared navigation, especially when hosting under a path prefix.
- Keep related pages and assets together when relying on path-relative URLs.
- Check representative links from both top-level and deeply nested pages, including after redirects and domain changes.
- Review generated HTML in staging; a link can look correct in a template but resolve differently in the deployed page.
Conclusion
Use root-relative URLs for consistent site-wide paths when your deployment supports them, path-relative URLs for content that moves together, and absolute URLs for destinations tied to a specific host. When in doubt, inspect the document’s base URL and resolve the link in the browser.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.