javascript <link href=""> 标签是否只出现在 <head> 标签中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5828365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:29:59  来源:igfitidea点击:

Do <link href=""> tags only go in the <head> tag?

javascripthtmlcss

提问by Click Upvote

Is it possible/good practice to use <link href"">to include stylesheets outside of the <head>tag, or do they only work / should they be only used in <head>?

是否有可能/良好的做法<link href"">是在<head>标签之外包含样式表,或者它们是否只能工作/是否应该只用于<head>

采纳答案by Boris Zbarsky

It's possible to do it.

有可能做到。

It's not recommended to do it, because whatever content comes before the <link>will start rendering and then when the stylesheet is loaded will be rerendered with the new styles. That means that the pageload will be slower (because the browser has to redo all that work) and uglier (because there will be this flash of content with one style that's then restyled to look different).

不建议这样做,因为在<link>开始渲染之前出现的任何内容都会在加载样式表时使用新样式重新渲染。这意味着页面加载会更慢(因为浏览器必须重做所有工作)和更丑(因为会有这种具有一种样式的内容,然后重新设计以使其看起来不同)。

回答by alex

They are invalid outside of head, though no browser I know of won't apply the styles if not a child of head.

它们在 . 之外无效head,尽管我所知道的浏览器如果不是head.

回答by Upvote

The tag defines the relationship between a document and an external resource.

The tag is most used to link to style sheets.

Note: The link element must be embedded in the head section, and it can appear any number of times.

标签定义了文档和外部资源之间的关系。

该标签最常用于链接到样式表。

注意:链接元素必须嵌入到头部部分中,并且可以出现任意次数。

From: http://www.w3schools.com/tags/tag_link.asp

来自:http: //www.w3schools.com/tags/tag_link.asp

The LINK element (<link>)is used to add external information related to the HTML document in the header of your document in the HEAD element.

LINK 元素(<link>)用于在 HEAD 元素的文档标题中添加与 HTML 文档相关的外部信息。

From: http://www.w3.org/QA/Tips/use-links

来自:http: //www.w3.org/QA/Tips/use-links

回答by Alex Waters

The <link>tag does not define structure of a webpage, so it should stay out of the <body>and <footer>.

<link>标签没有定义网页的结构,所以它应该呆的出来<body><footer>

I can't think of any reason why you would ever want/need to put it anywhere other than the <head>

我想不出任何理由为什么你会想要/需要把它放在除了 <head>

回答by Brian Fisher

According to the HTML 4.01 spec, the <link>tag must appear in the head section:

根据 HTML 4.01 规范,<link>标签必须出现在 head 部分:

This element defines a link. Unlike A, it may only appear in the HEAD section of a document, although it may appear any number of times. Although LINK has no content, it conveys relationship information that may be rendered by user agents in a variety of ways (e.g., a tool-bar with a drop-down menu of links).

该元素定义了一个链接。与 A 不同,它可能只出现在文档的 HEAD 部分,尽管它可能出现多次。尽管 LINK 没有内容,但它传达了可由用户代理以多种方式呈现的关系信息(例如,带有链接下拉菜单的工具栏)。

Source W3C: http://www.w3.org/TR/html401/struct/links.html#edef-LINK

来源 W3C:http: //www.w3.org/TR/html401/struct/links.html#edef-LINK

回答by otakustay

Yes, element is metadata element, whose permitted parent is head, or a element in head.

是的,元素是元数据元素,其允许的父元素是头,或者是头中的元素。