HTML <base> Tag

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

Definition and Usage

The <base> element sets the document base URL used to resolve relative URLs in the page — links, images, stylesheets, scripts, and more. Scripts can read the effective base with document.baseURI.

Put it in <head>. A document may have only one <base>. It must include href, target, or both.

Baseline · Widely available.

Not deprecated. Core support has been stable across browsers since about July 2015.

Use with care.

One wrong base can rewrite every relative URL on the page (including in-page # links). Prefer root-relative paths like /css/style.css unless you truly need a shared base.

Security note.

If target contains a newline, tab, or <, browsers reset it to _blank to reduce dangling-markup injection risks.

HTML Syntax

syntax.html
<base href=" url">

<base> is a void metadata element: start tag only, no end tag, no children. Place it in <head> before other URL-bearing elements that should use this base.

Attributes

Provide at least one of these attributes (both are allowed together):

AttributeDescriptionTry it
hrefBase URL for relative links and resources. Absolute or relative allowed; not data: or javascript:Try it
targetDefault browsing context for <a>, <area>, and <form> without their own target: _self, _blank, _parent, _topTry it

Global Attributes

The <base> tag also supports the Global Attributes in HTML.

Live Preview

Pick a base href and a relative path to see how the browser would resolve the final URL (safe demo — does not inject <base> into this tutorial page).

Resolved with the URL constructor the same way relative references resolve against a base.

Resolved URL

https://example.com/index.html

base="https://example.com/" + "index.html"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Set a Base URL

Resolve relative links against one root URL from <head>.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Base href</title>
  <base href="https://example.com/">
</head>
<body>
  <p>
    <a href="index.html">Home</a>
    resolves to https://example.com/index.html
  </p>
</body>
</html>
Try It Yourself

How It Works

  1. Open <head> and add one <base>.
  2. Set href to the root you want relative URLs to use.
  3. Write relative links such as index.html in the body.
  4. Browser resolves them against that base before navigation.

Example 2 — Relative Images and Pages

The same base applies to more than anchors — images and other relative resources too.

HTML
<head>
  <base href="https://www.example.com/">
</head>
<body>
  <img src="images/logo.png" alt="Site logo" width="120">
  <p><a href="html/tags/a">HTML a tag</a></p>
</body>
Try It Yourself

How It Works

  1. Declare the base once in <head>.
  2. Use relative src / href in the document.
  3. Each relative URL is joined to the base the same way.
  4. Absolute URLs stay absolute and ignore the base for their final address.

Example 3 — Default Link Target

Set a default browsing context for links and forms that omit their own target.

HTML
<head>
  <base target="_blank">
</head>
<body>
  <p>
    <a href="https://www.example.com">
      Example (opens in a new tab by default)
    </a>
  </p>
</body>
Try It Yourself

How It Works

  1. Add target on <base> (here _blank).
  2. Omit target on the link so the default applies.
  3. Navigation uses that context for <a>, <area>, and <form>.
  4. Override when needed by setting target on the individual element.

Example 4 — Combine href and target

One <base> can set both the URL root and the default window.

HTML
<head>
  <base href="https://example.com/" target="_blank">
</head>
<body>
  <a href="docs/guide.html">Guide</a>
</body>
Try It Yourself

How It Works

  1. Put both attributes on the single allowed <base>.
  2. href resolves docs/guide.html against https://example.com/.
  3. target="_blank" opens that result in a new browsing context by default.
  4. If multiple <base> tags appear, only the first href and first target are used.

Edge Cases & Pitfalls

Most base bugs come from surprising URL resolution or placing the tag too late.

anchors

Fragment links use the base too

href="#section" becomes baseURL#section, which can leave the current page. Prefer a full same-document strategy when you use <base>.

order

Place <base> early in <head>

It must come before other elements with URL attributes that should resolve against it.

count

Only one effective base

Extra <base> elements are ignored for later href/target values — use a single tag.

og

Open Graph ignores <base>

Social meta image URLs should be absolute. Relative Open Graph values will not pick up your base.

href

No data: or javascript: bases

Those schemes are not allowed in base href.

security

Odd target characters reset to _blank

Newline, tab, or < in target is sanitized to _blank.

Browser Support

The <base> element is core HTML metadata in every modern browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<base>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera resolve relative URLs against base. Always test fragment links and third-party meta tags in your own setup.

97% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<base> Widely supported

Bottom line: Safe to use when you need a shared document base. Validate relative links, fragments, and social meta URLs carefully.

Key Takeaways

  • Role: <base> sets the document base URL and/or default link target.
  • Placement: one tag in <head>, early, with href and/or target.
  • Scope: relative URLs only — absolute URLs keep their own address.
  • Caution: fragment links and Open Graph tags are common surprise areas.

One line: use one early <base> only when many relative URLs must share the same root.

Default CSS Settings

<base> lives in <head> and is not shown in the page body. Metadata elements are typically not rendered:

default.css
base {
  display: none;
}

There is nothing visual to style. Effects appear only through how relative URLs and default targets behave.

Frequently Asked Questions

The base element sets the document base URL used to resolve relative URLs (links, images, scripts, stylesheets, and more). It can also set a default target for links and forms that omit target.
Inside head, and only once per document. It must appear before other elements whose attributes use URLs that should resolve against that base.
You need at least one of them. href sets the base URL; target sets the default browsing context (_self, _blank, _parent, _top, or a frame name).
No. Absolute URLs with a scheme and host keep their own address. Only relative URLs are resolved against the base href.
Fragment-only links like href="#section" are resolved against the base URL, so the browser may request the base page with that fragment instead of jumping in the current document.
Usually no. Prefer root-relative paths like /css/style.css when you can. Use base when many relative paths must share one root (mirrors, static exports, some legacy layouts).

Next: basefont

See why the obsolete basefont element should be replaced with CSS.

basefont tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful