👀 Live Preview
Conceptual view of how <base> affects a relative link:
<base href="https://example.com/">href="about.html" https://example.com/about.html
By the end of this tutorial, you’ll know when and how to set a document-wide base URL for relative links and resources.
Set the default URL foundation for every relative path in a document.
Place exactly one <base> inside <head>.
Resolve URLs with href and set default link targets.
Understand how relative, root-relative, and absolute paths behave.
Avoid injected base URLs that redirect users to malicious sites.
Prefer root-relative paths on most modern production sites.
<base> Tag?The base element (<base>) specifies a document-wide base URL. Any relative URL in the page is resolved against this foundation instead of the current page URL.
The base element is a void metadata tag that must live in <head>. Only one is allowed per HTML document, and it should appear before relative URLs that depend on it.
It affects links, images, stylesheets, scripts, forms, and any other attribute that uses a relative URL. Absolute URLs with a full scheme are not changed.
Place <base> inside <head> with an href attribute pointing to the base URL. End with a trailing slash when resolving path segments:
<head>
<base href="https://example.com/">
<!-- Other head elements -->
</head><base> is a void element—no closing tag required in HTML5.href or src that should use the base URL.| Attribute / Rule | Values | Purpose |
|---|---|---|
href | URL | Base URL for resolving relative paths |
target | _blank | _self | … | Default browsing context for links and forms |
| Placement | <head> | Must appear in the document head |
| Limit | One per page | Only a single base element is allowed |
| Absolute URLs | Unchanged | Full URLs with scheme are not affected |
| Alternative | /root-relative | Most sites use paths like /css/style.css instead |
With <base href="https://example.com/docs/">, the browser resolves relative paths like this:
| Input | Type | Resolved URL |
|---|---|---|
page.html | Relative path | https://example.com/docs/page.html |
images/logo.png | Subfolder | https://example.com/docs/images/logo.png |
/about.html | Root-relative | https://example.com/about.html (host only) |
https://other.com/ | Absolute | Unchanged — base does not apply |
The <base> tag supports two meaningful attributes plus global attributes:
href Required*The document base URL used to resolve relative URLs in links, images, CSS, scripts, and forms.
href="https://example.com/"target OptionalDefault browsing context for links and forms: _blank, _self, _parent, or _top.
target="_blank"<base href="https://example.com/" target="_blank">* At least one of href or target must be present for the element to have effect.
Three base URL patterns with copy-ready code, live previews, and hands-on practice.
Conceptual view of how <base> affects a relative link:
<base href="https://example.com/">href="about.html" https://example.com/about.htmlSet the foundation URL inside <head> so all relative paths resolve from one location.
<head>
<base href="https://example.com/">
</head>
<body>
<a href="index.html">Home</a>
</body>Here are the most frequent scenarios where the <base> tag is used.
Point all relative stylesheet, image, and link paths at a CDN or site root without rewriting every URL.
<head>
<base href="https://codetofun.com/">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<img src="images/logo.png" alt="Logo">
<a href="page.html">Visit Page</a>
</body>Use the target attribute to open all links and form submissions in a specific browsing context by default. Individual links can still override with their own target.
<head>
<base href="https://example.com/" target="_blank">
</head>
<body>
<a href="docs.html">Opens in new tab</a>
<a href="help.html" target="_self">Opens in same tab</a>
</body>Because <base> redirects every relative URL on the page, a compromised or injected base element can send users to malicious destinations. Treat it carefully:
<base> tagAdd <base href="..."> before relative URLs that depend on it.
The base URL is stored as the document’s URL resolution context.
Each relative href or src is combined with the base URL to form a full address.
Short relative paths work across mirrored or exported pages without rewriting every URL by hand.
The <base> tag is supported in all browsers, including Internet Explorer.
From legacy IE to the latest browsers — the base element has universal support for href and target resolution.
Bottom line: Ship base URL markup with confidence. The <base> element works in every production browser today.
The <base> tag establishes a document-wide URL foundation for resolving relative links and resources. Use it deliberately in the <head>, limit yourself to one per page, and prefer root-relative paths on most modern sites.
When you do need it, pair href with optional target and keep security in mind.
<base> inside <head> before dependent URLstarget when every link should open the same way/css/style.css) on most sites<base> element per document<base> in the body—it belongs in the head<base> improves SEO—it only affects URL resolution<base>Bookmark these before you ship — they’ll keep your relative URLs resolving correctly and safely.
<base> sets the foundation for resolving every relative path in the document.
Must live in <head>—only one base element per HTML document.
href resolves links, images, CSS, scripts, and form actions.
target sets the default browsing context for all links and forms.
Fully qualified URLs with a scheme are not modified by the base element.
ResolutionSupported in every browser including Internet Explorer—no polyfills needed.
Compatibility<base> element sets a default base URL for resolving relative links and resources in a document—including anchors, images, stylesheets, and scripts.<head>, before any relative URLs that should use it. Only one <base> element is allowed per HTML document.href sets the base URL. target sets the default browsing context for links and forms, such as _blank for new tabs.https://example.com/page.html) are unchanged. Only relative URLs are resolved against the base./css/style.css. Use <base> sparingly for mirrored content, static exports, or legacy templates.Open the Try It editor and see how relative links resolve against a base href.
7 people found this page helpful