3.5 Referencing an External Document via <link>通过<link>引用外部文档
The link
element is a standalone tag for the head of the HTML document between <head>
and </head>
, which you can use to create a relationship between the current document and an external document. link
元素是<head>
和</head>
之间HTML文档头部的独立标记,您可以使用它在当前文档和外部文档之间创建关系。In practice, the 在实践中,link
element is often used to include an external CSS file in the current document. link
元素通常用于在当前文档中包含外部CSS文件。The <link>
tag can be used more than once in the head
element to include multiple resources in the current document. <link>
标签可以在head
元素中多次使用,以在当前文档中包含多个资源。The type or purpose of the relationship depends on the value of the 关系的类型或目的取决于所使用的rel
attribute used (see Table 3.4 for the attributes of the link
element).rel
属性的值(链接元素的属性见表3.4)。
Even though the tag here is 尽管这里的标签是<link>
, this standalone element has nothing in common with the familiar hypertext links, which are underlined and allow the user to navigate to other websites by clicking on this underlined text.<link>
,但这个独立元素与熟悉的超文本链接没有任何共同之处,这些超文本链接带有下划线,允许用户通过点击这个带下划线的文本导航到其他网站。
The following example shows you how you can include an external CSS file in the HTML document by using the 以下示例向您展示了如何使用link
element.link
元素在HTML文档中包含外部CSS文件。
<!doctype html>
<html lang="en">
<head>
<title>Logical linking via link</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
</head>
<body>
<p>A simple paragraph text!</p>
</body>
</html>
Listing 3.3 /examples/chapter003/3_5/index.html
You can specify the relationship between the document and the external document via the HTML attribute 您可以通过HTML属性rel
. rel
指定文档和外部文档之间的关系。Possible values for this attribute are listed in Table 3.4. In the example, the document is linked to an external stylesheet file. 表3.4列出了该属性的可能值。在该示例中,文档链接到外部样式表文件。When using this tag, you should also make sure to specify the MIME type, which I’ve done here with the 使用此标记时,您还应该确保指定MIME类型,我在这里已经用type
attribute and the text/css
value. type
属性和text/css
值指定了MIME类型。However, the most important attribute that you must always use along with the 但是,您必须始终与link
element is the href
attribute, which specifies the URL of the linked resource. link
元素一起使用的最重要的属性是href
属性,它指定了链接资源的URL。By specifying style.css, I assumed the stylesheet file is in the same directory as the HTML document.通过指定style.css,我假设样式表文件与HTML文档位于同一目录中。
Figure 3.5
Thanks to the Logical Link to the External CSS File, the <p> Element Was Formatted Here in This Example由于外部CSS文件的逻辑链接,本例中<p>
元素的格式如下
Specifying a Base URL via the <base> Element通过<Base>
元素指定基本URL
If you want to set the resources from another URL using the 如果你想使用base
element, you still need to put <base>
before <link>
in the head area between <head>
and </head>
. Example:base
元素从另一个URL设置资源,你仍然需要在<head>
和</head>
之间的head
区域将<base>
放在<link>
之前。示例:
...
<head>
<title>Logical linking via link</title>
<base href="https://css.sap-press.com/">
<link rel="stylesheet" type="text/css" href="rheinwerk.css">
<meta charset="UTF-8">
</head>
...
Here in 在<base>
, https://css.sap-press.com/ was defined as the base URL for the references, so the reference to the CSS file https://css.sap-press.com/rheinwerk.css is added and used in <link>
. <base>
中,https://css.sap-press.com/被定义为引用的基本URL,因此对CSS文件的引用https://css.sap-press.com/rheinwerk.css在<link>
中添加和使用。If the 如果base
element were located behind the link
element, the rheinwerk.css file would again be expected in the same directory as the current document, which may well be intentional. base
元素位于link
元素后面,那么rheinwerk.css文件将再次与当前文档位于同一目录中,这很可能是有意的。For this reason, you should pay attention to where you define the 因此,您应该注意定义base
element because only those references that are located after this element are extended with the base URL.base
元素的位置,因为只有位于此元素之后的引用才会用基本URL扩展。
Regarding the 关于rel
attribute and its possible values, note that some of these values are only of limited use in practice because how (and whether) a logical link is displayed by the web browser isn’t specified. rel
属性及其可能的值,请注意,其中一些值在实践中的用途有限,因为没有指定web浏览器如何(以及是否)显示逻辑链接。Especially for links with the link types 特别是对于具有next
, prev
, first
, last
, author
, help
, search
, sidebar
, and license
, it’s therefore more advisable (for the time being) to insert a corresponding button or text for this yourself to create a hyperlink (see Chapter 5, Section 5.2). next
、prev
、first
、last
、author
、help
、search
、sidebar
和license
等链接类型的链接,因此(目前)最好自己插入相应的按钮或文本来创建超链接(见 第5章第5.2节)。The global 全局title
attribute enables you to define a caption here that will be displayed if a web browser should support one of these links, that is, the just-mentioned rel
values such as next
, prev
, first
, and so on.title
属性允许您在此处定义一个标题,如果web浏览器支持其中一个链接,即刚才提到的rel
值,如next
、prev
、first
等,则将显示该标题。
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Table 3.4
HTML Attributes for the <link> HTML Element<link>
HTML元素的HTML属性
If you take a closer look at the 如果你仔细看看表3.4中的rel
values in Table 3.4, you might notice that there are two different types of values here: (1) the attribute values for pure hypertext links, and (2) values for links to external resources. rel
值,你可能会注意到这里有两种不同类型的值:(1)纯超文本链接的属性值,以及(2)指向外部资源的链接的值。外部资源的rel
values to external resources are icon
, pingback
, prefetch
, and stylesheet
. All other values are pure hypertext links.rel
值是icon
、pingback
、prefetch
和stylesheet
。所有其他值都是纯超文本链接。
Confusion with the "rel" Attribute Values与“rel”属性值混淆
Besides the 除了这里显示的rel
attribute values shown here, you’ll probably come across other values on the internet. rel
属性值外,您可能还会在互联网上遇到其他值。It’s quite hard to keep track of this as well as of what works and what doesn’t work on which web browsers. 很难跟踪这一点,以及在哪些网络浏览器上哪些有效,哪些无效。But I’m sure there’ll be some movement in this regard in the future. 但我相信未来在这方面会有一些进展。However, you should note that many of those values you find on the web are merely suggestions. 然而,你应该注意到,你在网上找到的许多价值观只是建议。Going into this topic in greater depth is beyond the scope of this chapter; however, you can find a good overview and recommendations at http://microformats.org/wiki/existing-rel-values. 更深入地探讨这个话题超出了本章的范围;但是,您可以在以下网址http://microformats.org/wiki/existing-rel-values找到一个很好的概述和建议。The W3C website also provides a useful overview at www.w3.org/TR/html5/links.html#sec-link-types.W3C网站还提供了有用的概述www.w3.org/TR/html5/links.html#sec-link-types。