HTML - <base> Tag



In recent times, HTML5 has added the <base> tag. For every relative URL in the HTML document, it defines an absolute (base) URL. Additionally, this element specifies how links in the current document must be opened (in a new window, in the current window, etc.).

The <head> element include the only one <base> tag that can be used on the page. It must be inserted as soon as possible because its action extends at the place where it is given.

Only the first href and target attributes will be followed if you use multiple <base> element. The rest won't be considered.

Syntax

Following is the syntax of <base> tag −

<base href = "SAMPLE_URL">

Specific Attributes

The HTML <base> tag also supports the following additional attributes −

S.No. Attribute & Description
1

href

Specifies the URL of a page or the name of the anchor that the link goes to.
2

target

Where to open the target URL.

Example

Following is a basic example of the usage of the <base> tag.

<!DOCTYPE html>
<html>
<head>
   <title>HTML base Tag</title>
   <base href="https://www.tutorialspoint.com" />
</head>
<body> HTML: <img src="https://www.tutorialspoint.com/cg/images/logo.png" />
</body>
</html>

On running the above code, it will generate an output consisting of the image that gets uploaded to the webpage along with text.

Example

Let’s look at the following example, where we are going to set the default target for all the URLs on the page.

<!DOCTYPE html>
<html>
<head>
   <base href="https://www.tutorialspoint.com/index.htm" target="_blank">
</head>
   <body style="background-color:#D5F5E3">
   <p>
      <img src="https://www.tutorialspoint.com/cg/images/logo.png" height="39" alt="logo"> - Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
   </p>
   <p>
      <a href="https://www.tutorialspoint.com/html/index.htm">HTML Tutorial</a>
   </p>
</body>
</html>

When we execute the above code, the output window will pop up, displaying the image along with text and a link to the webpage. When the user clicks on the link, it opens in a new window even though the target is not set. It opens in a new window because the target of the base element is set to target=_blank.

Example

Consider another scenario where we are going to take the base URL, which all the relative links treat as the starting URL and opens in the same window.

<!DOCTYPE html>
<html>
<head>
   <title>Base tag</title>
   <style>
      a {
         text-decoration: none;
         color: black;
      }

      a:hover {
         color: green;
         font-size: 20px;
      }
   </style>
   <base href="https://www.tutorialspoint.com/index.htm">
</head>
<body style="background-color:#D6EAF8 ">
   <h2 style="color: #DE3163; font-size: 30px; font-style: verdana;">List of Courses</h2>
   <a href="https://www.tutorialspoint.com/html/index.htm">HTML tutorial</a>
   <br>
   <a href="https://www.tutorialspoint.com/artificial_intelligence/index.html">AI tutorial</a>
   <br>
   <a href="https://www.tutorialspoint.com/data_structures_algorithms/index.htm">Data Structure tutorial</a>
   <br>
   <a href="https://www.tutorialspoint.com/cloud_computing/index.htm">Cloud Computing tutorial</a>
   <br>
</body>
</html>

On running the above code, it will generate an output consisting of links along with applied CSS displayed on the webpage. When the user clicks on the link, it will open the page within the same window.

html_tags_reference.htm
Advertisements