在项目开发中, 发现很多脚手架提供的 index.html 模板会含有 <base>
标签, 比如 Angular CLI.
<base>
的 href
属性指定了文档中所有相对 URL 的基本 URL, 默认值为 location.href
.
下例中, <img>
的资源路径为 https://example.com/img.jpg
<base href="https://example.com">
<img src="img.jpg" />
多数情况下, 我们设置 <base>
的 href
属性指向静态资源的根目录.
需要注意的是, 锚点同样受 <base>
的 href
属性影响, 即 <a href="#anchor">Anker</a>
不会相对当前页面的路径解析.