Difference: absolute vs relative URL?
An absolute URL contains the full address, including the scheme and host (https://example.com/docs/guide.html), and points to the same place from anywhere. A relative URL is resolved against the current document's URL (or a <base> element): guide.html, ../images/logo.png or root-relative /docs/guide.html.
Types of relative URLs
- Document-relative:
about.htmlor./about.html(same folder),../index.html(parent folder). - Root-relative:
/images/logo.png, resolved from the site's origin. The most common choice in web apps. - Protocol-relative:
//cdn.example.com/lib.jsuses the current scheme; outdated now that everything is HTTPS, so usehttps://explicitly. - Fragment-only:
#sectionscrolls within the page. - Query-only:
?page=2keeps the path and changes the query.
When to use which
- Absolute: links to other sites, canonical URLs, Open Graph images, sitemaps, emails and RSS feeds (content viewed outside your site).
- Root-relative: internal links and assets in most apps; they survive moving pages between folders and changing domains.
- Document-relative: static sites that may be hosted under different subpaths, documentation folders, local files.
Resolution rules
new URL("../img/a.png", "https://example.com/blog/post/") gives https://example.com/blog/img/a.png. A trailing slash on the base changes the result, which is a common source of broken links.
<base>
<base href="https://example.com/app/"> changes how all relative URLs on the page resolve. It is easy to misuse, so prefer explicit paths.
Code example
1<style>body { font-family: system-ui; } code { background: #f1f5f9; padding: 0 4px; }</style>2<ul>3 <li>Absolute: <a href="https://developer.mozilla.org/en-US/docs/Learn">https://developer.mozilla.org/en-US/docs/Learn</a></li>4 <li>Root-relative: <a href="/docs/getting-started">/docs/getting-started</a></li>5 <li>Document-relative: <a href="../pricing.html">../pricing.html</a></li>6 <li>Fragment: <a href="#out">#out</a></li>7</ul>89<p id="out">Resolved against <code>https://example.com/blog/post/</code>:</p>10<pre id="results"></pre>11<script>12 const base = "https://example.com/blog/post/";13 const inputs = ["comments.html", "./img/cover.png", "../archive/", "/about", "?page=2", "#top", "https://other.site/x"];14 document.getElementById("results").textContent = inputs15 .map((u) => `${u.padEnd(18)} -> ${new URL(u, base).href}`)16 .join("\n");17</script>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.