Summary -

In this topic, we described about the <base> tag along with detailed example.

The <base> tag is an optional tag in all HTML documents. <base> tag specifies the base location from where the links to made. In other words, <base> tag specifies the base URL. The base URL is the target for all relative URLs in the document.

Relative links/URLs specified will become a relative to the URL specified in the <base> tag. There are no differences between HTML 4.01 and HTML5 for <base> tag. There is no end tag for <base> tag.

The <base> tag specifies with below two attributes.

  1. Href
  2. Target

For <base> tag, at least one of the above attributes are required at any point of time.

Rules -

  1. There must be only one <base> tag for entire documents.
  2. <base> tag should be under the <head> tag.
Note! Do not code the <base> tag in between the <head> as the elements may not consider the URL provided in <base> tag.
The best practice is, always code <base> tag at the beginning of the <head> tag.

Example -

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Base tag example</title>
		<base href="https://www.">
	</head>
	<body>
		<p> <a href="img/header-logo.png">Logo</a> </p>
	</body>
</html>

Output -