HTML Tutorial

HTML Tag: <base>

Purpose:

  • Specifies the base URL for all relative URLs in the document.

Usage:

  1. Place the <base> tag within the <head> element of the HTML document.
  2. Use the href attribute to specify the base URL.

Attributes:

  • href: The absolute URL that will be the base for all relative URLs.

Examples:

<head>
  <base href="https://example.com/">
  ...
</head>

This will make all relative URLs in the document use https://example.com/ as the base URL. For example, the following link will resolve to https://example.com/about:

<a href="about">...</a>

Exploring the <base> Tag:

Consider the following HTML example:

<!DOCTYPE html>
<html>
<head>
  <base href="https://example.com/">
</head>
<body>
  <img src="image1.jpg">
</body>
</html>

In this example, the <base> tag sets the base URL to https://example.com/. Therefore, the src attribute of the <img> tag will resolve to https://example.com/image1.jpg, even though only the relative path is specified.