HTML - <base> 标签



HTML <base> 标签用于指定基准 URL。<base> 标签中应该包含 hreftarget 属性。

此外,此元素指定当前文档中的链接必须如何打开。HTML <head> 元素只包含一个可在页面上使用的 <base> 标签。它必须尽快插入,因为它的作用范围延伸到它出现的位置。

如果您使用多个 <base> 元素,则只会遵循第一个 href 和 target 属性。其余的将不会被考虑。

语法

<base href="" target="">

属性

HTML base 标签支持 全局 属性,并接受一些列出的特定属性。

属性 描述
href URL 指定页面 URL 或链接指向的锚点名称。
target _blank
_parent
_self
_top
在何处打开目标 URL。

HTML base 标签示例

下面的例子将说明 base 标签的用法,包括在哪里、何时以及如何使用 base 标签来指定基准 URL。

指定默认 URL

以下是 <base> 标签用法的基本示例。

<!DOCTYPE html>
<html>

<head>
    <title>HTML base Tag</title>
    <base href="https://tutorialspoint.com" />
</head>

<body>
    <img src="/cg/images/logo.png" />
</body>

</html>

所有链接的默认目标

让我们来看下面的例子,我们将设置页面上所有 URL 的默认目标。

<!DOCTYPE html>
<html>

<head>
    <base href="https://tutorialspoint.com/index.htm">
</head>

<body>
    <p>
        <a href="/html/index.htm">
        <img src="/cg/images/logo.png" 
             height="39"
             alt="logo"> 
        </a>
        <br> 
        Tutorials Point originated from the idea that
        there exists a class of readers who respond 
        better to online content and prefer to learn new
        skills at their own pace from the comforts of their
        drawing rooms.
    </p>
</body>

</html>

多个相对链接的基准 URL

考虑另一种情况,我们将采用基准 URL,所有相对链接都将其视为起始 URL,并在同一窗口中打开。

<!DOCTYPE html>
<html>
<head>
   <title>HTML base Tag</title>
   <base href="https://tutorialspoint.com/index.htm">
</head>
<body >
   <h2>List of Tutorials</h2>
<ul>
   <li><a href="/html/index.htm">HTML Tutorial</a></li>
   <li><a href="/css/index.htm">CSS Tutorial</a></li>
   </ul>
</body>
</html>

支持的浏览器

标签 Chrome Edge Firefox Safari Opera
base
html_tags_reference.htm
广告
© . All rights reserved.