The base element sets a default URL for resolving relative links and resources in a document. It can simplify special document setups, but it also affects more links than newcomers expect.
What You Need to Know
Only the first base element with an href sets the base URL. It can make #fragment links point at the base URL rather than the current page, so use it carefully.
A base element can change every relative URL in the document, including fragments. Use it only when that behavior is intentional.
Example: See It in Action
Take a look at this small piece of HTML. You can use it in a practice document and change the text to make it your own.
If this is only part of a document, place metadata and style elements in the head of a complete HTML page. Keep ordinary visible content in its body.
<head>
<base href="https://example.test/docs/">
<title>Base URL Example</title>
</head>
<body><a href="intro.html">Introduction</a>
</body>
Let’s Understand the Example
- The base element changes how relative links are resolved across the document. It can surprise you if used casually.
- The head contains useful page information for the browser, such as its title and character encoding.
- The title names the page in the browser tab. It is different from the heading shown on the page.
- Href contains the destination of a link. A value starting with # points to an id on the same page.
The first example link points to https://example.test/docs/. Check whether that destination exists relative to the document. A link changes where readers go; it does not perform an action such as saving a form.
What Should You See?
The browser tab should show “Base URL Example”.
Open the page and compare what appears in the browser tab with the heading inside the document. Both have different jobs.
Try It Yourself
- Compare where intro.html points before and after adding the base element.
- Save your changes, open or refresh the page, and compare the result with the original example.
- Tell a friend what your change did. If you are not sure, undo it, refresh the page, and compare.
Common Beginner Mistakes
- Adding base to fix one image path without checking all other relative links.
- The title in the browser tab and the heading visible in the page are different pieces of information.
Key Takeaways
- The base element sets a default URL for resolving relative links and resources in a document.
- The base element changes how relative links are resolved across the document. It can surprise you if used casually.
- A base element can change every relative URL in the document, including fragments. Use it only when that behavior is intentional.