HTML <base> Element
The HTML <base> tag specifies the base URL for all relative URLs within a document. This means that all relative URLs will be calculated based on the provided base URL.
Usage of the <base> Tag
When using the <base> tag, the URL specified in the href, src, or any other attribute that references a URL in the document will have the base URL prepended to it.
Main Attributes of the <base> Tag
| Attribute | Description |
|---|---|
| href | Specifies the base URL for all relative URLs in the document. |
Example
Consider the following example where the base URL is set to https://www.example.com/. Any relative URLs in the document will be calculated based on this base URL.
<!DOCTYPE html>
<html>
<head>
<base href="https://www.example.com/">
</head>
<body>
<a href="#">Link to Page</a>
<img src="../../images/Logo_BluecodeAcademy_horizontal.png" alt="Image">
</body>
</html>
In the example above, the <a> tag and <img> tag reference relative URLs. Since the base URL is set to https://www.example.com/, the URLs will be resolved as follows:
https://www.example.com/page.htmlhttps://www.example.com/image.jpg