HTML <base> 标记用于指定用于 HTML 文档中包含的所有相关链接的基本 URI 或 URL。
一个文档中只能指定一个 <base> 元素,并且它必须放置在 <head> 元素中。我们还可以使用 target 属性指定其他链接应该如何打开(在同一窗口中、在另一个窗口中等)。
<base href= "https://www.rocschool.com">
在 HTML 中 <base> 元素不包含结束标记,但在 XHTML 中结束标记 </base> 是必需的。
以下是关于 <base> 标签的一些规范
| 展示 | 没有任何 |
| 开始标签/结束标签 | 只有开始标签 |
| 用法 | 锚点和链接 |
在同一窗口中打开(默认)
<!DOCTYPE html>
<html>
<head>
<title>Base tag</title>
<style>
a{text-decoration: none;
color:black;}
a:hover{color: green;
font-size: 18px;}
</style>
<base href="https://www.rocschool.com">
</head>
<body>
<h2 style="color: red; font-size: 30px; font-style: italic;">List of Web developement Tutorial</h2>
<a href="/html-tutorial">HTML tutorial</a><br>
<a href="/css-tutorial">CSS tutorial</a><br>
<a href="/bootstrap-tutorial">Bootstrap tutorial</a><br>
<a href="/javascript-tutorial">JavaScript tutorial</a><br>
<a href="/jquery-tutorial">JavaScript tutorial</a>
</body>
</html>在上面的示例中,我们使用了基本 URL =" https://www.javatpoint.com ",所有其他相关链接都将其视为起始 URL。这里 (/) 定义了当前文档的根 URL。
<!DOCTYPE html>
<html>
<head>
<title>Base tag</title>
<style>
a{text-decoration: none;
color:black;}
a:hover{color: green;
font-size: 18px;}
</style>
<base href="https://www.rocschool.com" target="_blank">
</head>
<body>
<h2 style="color: red; font-size: 30px; font-style: italic;">List of Web developement Tutorial</h2>
<a href="/html-tutorial">HTML tutorial</a><br>
<a href="/css-tutorial">CSS tutorial</a><br>
<a href="/bootstrap-tutorial">Bootstrap tutorial</a><br>
<a href="/javascript-tutorial">JavaScript tutorial</a><br>
<a href="/jquery-tutorial">JavaScript tutorial</a>
</body>
</html>在上面的例子中,我们只在 <base> 标签中使用了target="_blank"但它应用于整个文档链接。
标签特定属性:
| 属性 | 属性值 | 定义 | 说明 |
|---|---|---|---|
| href | url | 它指定所有相关链接的基本 URL。 | |
| target | |||
| _blank | 在新窗口中打开相对链接 | ||
| _self | 在当前窗口打开相对链接 | ||
| _parent | 在父框架中打开相对链接 | ||
| _top | 打开页面全宽的链接 |
<base> 标签支持 HTML 中的所有全局属性
<base> 不支持 HTML 中的事件属性
| 元素 | Chrome | IE | Firefox | Safari | Opera |
|---|---|---|---|---|---|
| <base> | Yes | Yes | Yes | Yes | Yes |