MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/d6d7ff2e2f9c

我們的志工尚未將本文翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!
您也可以閱讀本文的 English (US) 版本。

The HTML <base> element specifies the base URL to use for all relative URLs contained within a document. There can be only one <base> element in a document. 

The base URL of a document can be queried from a script using document.baseURI.

Content categories Metadata content.
Permitted content None, it is an empty element.
Tag omission There must be no closing tag.
Permitted parents Any <head> that doesn't contain any other <base> element
Permitted ARIA roles None
DOM interface HTMLBaseElement

Attributes

This element's attributes include the global attributes.

href
The base URL to be used throughout the document for relative URL addresses. If this attribute is specified, this element must come before any other elements with attributes whose values are URLs. Absolute and relative URLs are allowed.
target
A name or keyword indicating the default location to display the result when hyperlinks or forms cause navigation, for elements that do not have an explicit target reference. It is a name of, or keyword for, a browsing context (for example: tab, window, or inline frame). The following keywords have special meanings:
  • _self: Load the result into the same browsing context as the current one. This value is the default if the attribute is not specified.
  • _blank: Load the result into a new unnamed browsing context.
  • _parent: Load the result into the parent browsing context of the current one. If there is no parent, this option behaves the same way as _self.
  • _top: Load the result into the top-level browsing context (that is, the browsing context that is an ancestor of the current one, and has no parent). If there is no parent, this option behaves the same way as _self.

Usage notes

If multiple <base> elements are specified, only the first href and first target value are used; all others are ignored.

Examples

<base href="http://www.example.com/page.html">
<base target="_blank" href="http://www.example.com/page.html">  

Specifications

Specification Status Comment
HTML Living Standard
The definition of '<base>' in that specification.
Living Standard No change since last snapshot.
HTML5
The definition of '<base>' in that specification.
Recommendation Specified the behavior of target
HTML 4.01 Specification
The definition of '<base>' in that specification.
Recommendation Added the target attribute

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support (Yes) (Yes)1.0 (Yes)1 (Yes) (Yes)
href (Yes) (Yes)1.0 (Yes) (Yes) (Yes)
target (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support (Yes) (Yes) (Yes)1.0 (Yes) (Yes) (Yes)
href (Yes) (Yes) (Yes)1.0 (Yes) (Yes) (Yes)
target (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

1. Before Internet Explorer 7, <base> can be positioned anywhere in the document and the nearest value of <>base> is used.

Hint

  • The usage of an anchor tag within the page, e.g. <a href="#anchor">anchor</a> is resolved by using the base url as reference and triggers an http request to the base url.

    Example:

    The base url:
    <base href="http://www.example.com/">

    The anchor:
    <a href="#anchor">Anker</a>

    Refers to:
    http://www.example.com/#anchor

OpenGraph

OpenGraph meta-tags, do not acknowledge base-url and should always have full URLs, for example:

<meta property='og:image' content='http://example.com/thumbnail.jpg'>