HTML Base Tag

The HTML <base> tag is used to set a base URL for all relative links on a webpage. It helps define a default target for links and resources, making URL management easier. It must be placed inside the <head> and only one <base> tag should be used per page.

3 views
5 min read
Try It Yourself

Experiment with the code in an interactive editor

What is Base Tag?

The <base> tag sets a "default address" for all relative links on your page.

How it works:

1<head>
2    <base href="https://www.example.com/images/" target="_blank">
3</head>
4<body>
5    <!-- This link goes to: https://www.example.com/images/logo.png -->
6    <img src="logo.png" alt="Logo">
7    
8    <!-- This link goes to: https://www.example.com/images/banner.jpg -->
9    <img src="banner.jpg" alt="Banner">
10    
11    <!-- This link opens in new tab (because of target="_blank") -->
12    <a href="about.html">About</a>
13</body>

Base tag attributes:

Attribute

What it does

href

Default URL for all relative links

target

Default target for all links (_blank, _self, _parent, _top)

Important warnings:

  1. Only ONE base tag per page (if you put multiple, only first works)

  2. Must be in <head> (before any links)

  3. Affects ALL relative links (including images, CSS, JS)

  4. Absolute links (https://...) are NOT affected

1<head>
2    <base href="https://mysite.com/products/" target="_blank">
3</head>
4<body>
5    <!-- Goes to: https://mysite.com/products/laptops -->
6    <a href="laptops">Laptops</a>
7    
8    <!-- Goes to: https://mysite.com/products/phones -->
9    <a href="phones">Phones</a>
10    
11    <!-- This is absolute, so base tag does NOT affect -->
12    <a href="https://google.com">Google</a>
13    
14    <!-- Still opens in new tab because of base target="_blank" -->
15    <a href="about.html">About</a>
16</body>

When to use Base tag:

  • When moving a site to a new folder

  • When all links should open in new tabs

  • When you have many relative links

When NOT to use Base tag:

  • On most websites (it can be confusing)

  • When you need different targets for different links