HTML <base> Tag โ Base URL for Links
๐ Introduction
The <base> tag defines a base URL for all relative links in a web page.
It tells the browser:
โUse this URL as the starting point for all links and resources.โ
๐ It must be placed inside the <head> section.
๐งฑ Syntax
<head>
<base href="https://example.com/" />
</head>
๐ก Example
<!DOCTYPE html>
<html>
<head>
<title>Base Example</title>
<base href="https://example.com/" />
</head>
<body>
<a href="page.html">Go to Page</a>
</body>
</html>
๐ฅ๏ธ Output Explanation
- The link becomes:
https://example.com/page.html
- Even though you wrote only
page.html
๐ฏ Why <base> is Important
1. ๐ Controls All Relative Links
Applies to:
<a>links<img>sources<script>files<link>CSS files
2. โ๏ธ Simplifies Projects
Instead of repeating full URLs everywhere.
3. ๐ฆ Useful in Large Websites
Helps manage paths easily.
๐งฉ Example with Images and Links
<head>
<base href="https://mysite.com/" />
</head>
<body>
<a href="about.html">About</a>
<img src="images/logo.png" />
</body>
๐ง Important Concept
๐ <base> affects ALL relative paths
| Element | Effect |
|---|---|
<a> |
Link URL changes |
<img> |
Image path changes |
<script> |
JS file path changes |
โ ๏ธ Important Notes
- Only ONE
<base>per page โ (multiple is invalid) - Must be inside
<head> - Affects entire document
๐ซ Common Mistakes
- Putting
<base>in<body>โ - Using multiple
<base>tags โ - Forgetting it changes ALL relative links โ
๐งช Mini Exercise
Create a page with:
- A
<base>URL set to a website - A relative link (
<a href="page.html">) - An image with relative path
- Check how URLs change