<base>
The <base>
tag defines a base URL and/or a default target for all links in an HTML page.
It is declared within the <head>
tag so that it can become a common reference for all elements that redirect the user, such as the <a>
, <img>
, or <link>
tags, for example.
Caution: when used, it should be defined only once in the HTML document. 👀
Structure and syntax of the <base> tag
Basic structure and syntax
The <base>
tag is a self-closing tag, meaning it does not contain any content and does not have a closing tag: it closes itself.
It always follows this structure:
<base href="[base URL]">
At a minimum, the href
attribute is used to define the base URL for all relative links on the current page.
Usage example
Let's take a small example!
<!DOCTYPE html>
<html lang="en">
<head>
<base href="https://www.believemy.com" target="_blank">
</head>
<body>
<a href="/formations">View trainings</a>
</body>
</html>
In this example:
- the original URL is
/formations
; - the final URL will be
https://believemy.com/formations
.
Attributes
The <base>
tag can be used to define both the base URL and the default target for links.
Attribute | Description | Example |
---|---|---|
href | Defines the base URL from which all relative URLs will be resolved. | <base href="https://www.example.com/"> |
target | Defines a default target for all links and forms in the document. | <base target="_blank"> |
Here are the possible values for the
target
attribute:
Value Description _self
Loads the link in the same window or tab (default behavior). _blank
Opens the link in a new tab or window. _parent
Loads the link in the parent window of the document if present. _top
Loads the link in the outermost window (at the root document level).
Browser compatibility
The <base>
instruction is compatible with all browsers.
Element | Google Chrome | Safari | Mozilla Firefox | Edge |
<base> | Yes | Yes | Yes | Yes |