Definition
The <base>
HTML element is used to specify the base URL or default target for all relative URLs within a document. It is typically placed in the <head>
section of an HTML document and provides a reference point for resolving relative URLs.
The <base>
element has a single required attribute, href
, which specifies the base URL for all relative URLs in the document. Here’s an example:
<head>
<base href="https://www.example.com/" />
</head>
In this example, the <base>
element sets the base URL to “https://www.example.com/”. Any relative URLs, such as <a>
or <img>
tags without an explicit protocol or domain, will be resolved relative to this base URL.
The <base>
element is particularly useful when you have multiple relative URLs within a document and want to ensure that they are resolved correctly. It helps to avoid repeating the base URL in every relative URL, making the document more maintainable and reducing the chance of broken links.
It’s important to note that the <base>
element should have only one occurrence within an HTML document, and it should be placed in the <head>
section before any other elements that reference relative URLs.
Keep in mind that using the <base>
element affects all relative URLs within the document, so be cautious when using it in cases where you have different sections or components with their own relative URLs. In such cases, you may need to consider alternative strategies, such as using relative URLs with respect to the current page or component, or specifying explicit absolute URLs.
Overall, the <base>
element provides a way to establish a consistent base URL for relative URLs within an HTML document, simplifying URL resolution and improving the maintainability of the document.