<base>
Definition
The HTML base
element is a way to specify a default URL for all the links on a page. This means that you can set the base
URL once in the base
element, and all the other links on the page will use this URL as the starting point, unless they are explicitly given another URL to use. This can be useful if you have a lot of links on your page, or if you want to make it easy to change the base URL for all the links on your page at once. For example, you might use the base
element if you have a page with links to different pages on your website, and you want to make sure that all the links use the correct URL for your website.
Examples
Here is a simple example of how you might use the base
element in an HTML page:
<!DOCTYPE html>
<html>
<head>
<base href="https://www.example.com/">
<title>My Page</title>
</head>
<body>
<p>Here is a <a href="page1">link</a> to another page on my website.</p>
<p>And here is another <a href="page2">link</a> to another page on my website.</p>
</body>
</html>
In this example, the base
element is used to specify the base URL for all the links on the page. The base
element is placed in the head
section of the HTML page, and it has an href
attribute that specifies the base URL. In this case, the base
URL is set to https://www.example.com/
, which means that all the links on the page will use this URL as the starting point.
For example, the first link on the page has an href
attribute that specifies the relative URL page1
. Because the base
URL is set to https://www.example.com/
, this relative URL will be combined with the base
URL to form the full URL for the link, which will be https://www.example.com/page1
. Similarly, the second link on the page has an href
attribute that specifies the relative URL page2
, which will be combined with the base
URL to form the full URL https://www.example.com/page2
.
In this way, the base
element allows you to specify a default URL for all the links on a page, so that you don't have to include the full URL for each link individually. This can make it easier to manage your links, and it can also make it easier to update the base URL if you need to change it in the future.
Attributes
Attribute | Description | Deprecated |
---|---|---|
href | The URL of the document to which the base element applies | No |
target | The default target for all hyperlinks and forms in the document | No |
Best Practices
- The
<base>
element should be used in the<head>
section of an HTML document. - Only one
<base>
element should be used per document. - The
<base>
element should be used to specify the base URL for all relative URLs in the document. - The
target
attribute of the<base>
element should be used to specify a default target for all hyperlinks and forms in the document. - The
href
attribute of the<base>
element should be used to specify the default URL for all hyperlinks and forms in the document.
Accessibility Considerations
The target
attribute of the <base>
element can be used to specify a default target for all hyperlinks and forms in the document. This can cause issues for users who rely on assistive technologies, such as screen readers, as they may not be able to determine the target of a link or form. In such cases, it is recommended to specify the target for each individual hyperlink and form, rather than using the target
attribute of the <base>
element.
Additionally, the href
attribute of the <base>
element should be used with caution, as it can affect the behavior of relative URLs in the document. This can cause confusion for users and make it difficult for them to navigate the document.
Browser Compatibility
Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | Yes |