Cover Image for HTML base Tag
62 views

HTML base Tag

The <base> tag in HTML is used to specify the base URL or target for all relative URLs within a document. It sets the base URL for all relative links, including those used for images, scripts, stylesheets, and anchor links.

Here is an example of how the <base> tag can be used:

HTML<span role="button" tabindex="0" data-code="<head>       <base href="https://www.example.com/" target="_blank"> </head> <body>      <a href="page.html">Link</a>      <img src="image.jpg" alt="Image">
<head>
      <base href="https://www.example.com/" target="_blank">
</head>
<body>
     <a href="page.html">Link</a>
     <img src="image.jpg" alt="Image">
</body>

In this example, the <base> tag is placed within the <head> section of the HTML document. The href attribute specifies the base URL, which is “https://www.example.com/“. This means that all relative URLs within the document will be resolved relative to this base URL.

The <a> tag and the <img> tag within the <body> section of the document use relative URLs. In this case, the URL “page.html” in the <a> tag will be resolved to “https://www.example.com/page.html” due to the base URL specified by the <base> tag. Similarly, the image URL “image.jpg” in the <img> tag will be resolved to “https://www.example.com/image.jpg“.

The target attribute in the <base> tag specifies the default target for all anchor links within the document. In this example, the _blank value opens the links in a new browser tab or window.

Using the <base> tag can be helpful when you want to set a common base URL for all relative links within a document, reducing the need to repeat the base URL in every link or resource reference. However, it’s important to use the <base> tag carefully and ensure that it is set correctly to avoid potential issues with relative URL resolution.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS