HTML Base URLs and Relative Links
Set the base URL
The first base element with href establishes the document base URL. Relative resource URLs resolve against it. Put base before URL-dependent metadata. An absolute href is not rewritten.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Metadata practice</title> <base href="https://example.com/guides/"> </head> <body> <a href="chapter.html">Chapter</a> </body></html>The resolved href is https://example.com/guides/chapter.html.
The trailing slash matters
Without a trailing slash, the final path segment is treated like a file for relative URL resolution. Compare guides with guides/. The example deliberately omits the slash.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Metadata practice</title> <base href="https://example.com/guides"> </head> <body> <a href="chapter.html">Chapter</a> </body></html>The resolved URL is https://example.com/chapter.html.
Fragments and absolute destinations
A fragment-only link also resolves against the base URL. With a different base, a link intended as a local jump may navigate away. An absolute URL keeps its own origin.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Metadata practice</title> <base href="https://example.com/docs/"> </head> <body> <a href="#intro">Introduction</a> <a href="https://example.org/help">Help</a> <h1 id="intro">Introduction</h1> </body></html>The first link resolves to /docs/#intro at example.com, the second stays at example.org. No links are followed.
Common mistakes and practice
Use base only when a document-wide change is intentional. Review every relative link, asset and form destination. The target attribute can also set default navigation targets; avoid surprise new tabs.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Base URLs and Relative Links — Knowledge Check
Apply the metadata concepts taught in this lesson.
Which base href is used if several exist?
With base https://example.com/a/ and href b, what is the destination?
What changes if the base ends in /a without slash?
Does base affect fragment-only links?
Does an absolute https://example.org/help link adopt the base origin?