Web development

Choose relative links carefully on nested pages

A relative link resolves against the current document URL. On a nested static site, use root-relative paths for shared site navigation and intentional local paths for neighboring resources.

Worked example

Node.js example · save as example.mjs and run node example.mjs

const base = "https://example.com/articles/files/";
console.log(new URL("index.html", base).pathname);
console.log(new URL("/articles/", base).pathname);
console.log(new URL("../", base).pathname);

How it works

Index.html points inside the current folder, while the leading slash starts from the origin root. Parent navigation removes one path segment. The trailing slash on the base affects whether its final segment is interpreted as a folder or filename.

Limits and mistakes

Root-relative URLs need adjustment if a project is hosted beneath a repository subpath. A base element can also alter relative resolution across the document. Test the actual deployed URL structure rather than only opening local files.

Check the result

Expect /articles/files/index.html, /articles/, and /articles/. Open a deeply nested article and verify its logo, stylesheet, and category links resolve correctly.

Sources

Reference documentation for the behavior used in this guide: