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.tw" />
</head>

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

</html>

所有連結的預設目標

讓我們來看下面的例子,我們將設定頁面上所有 URL 的預設目標。

<!DOCTYPE html>
<html>

<head>
    <base href="https://tutorialspoint.tw/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.tw/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
廣告