Html <link> 标签中是否需要 type="text/css" ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5409114/
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
Is type="text/css" necessary in a <link> tag?
提问by pimvdb
I was wondering whether or not it is necessary to use <link rel="stylesheet" type="text/css" href=...>
over <link rel="stylesheet" href=...>
. The rel="stylesheet"
marks the information that it is a stylesheet - so text/css
doesn't actually add anything as far as I'm concerned.
我想知道是否有必要使用<link rel="stylesheet" type="text/css" href=...>
over <link rel="stylesheet" href=...>
。这rel="stylesheet"
标志着它是一个样式表的信息 - 因此text/css
就我而言实际上并没有添加任何内容。
The only stylesheet format used by HTML is CSS anyway, so what does text/css
'say' to the browser? Some websites seem to add the type="text/css"
attribute (http://www.jquery.com/), whilst other ones don't (http://www.youtube.com/).
无论如何,HTML 使用的唯一样式表格式是 CSS,那么text/css
对浏览器“说”什么呢?一些网站似乎添加了该type="text/css"
属性(http://www.jquery.com/),而其他网站则没有(http://www.youtube.com/)。
So, what is the use of type="text/css"
in a <link rel="stylesheet">
element, and is it necessary to include it?
那么,type="text/css"
在一个<link rel="stylesheet">
元素中有什么用,是否有必要包含它呢?
采纳答案by Tom Gullen
It's not required with the HTML5 spec, but for older versions of HTML is it required.
HTML5 规范不需要它,但对于旧版本的 HTML,它是必需的。
Html 4 W3.org spec
Html 4 W3.org 规范
http://www.w3.org/TR/html40/struct/links.html#edef-LINKhttp://www.w3.org/TR/html40/present/styles.html
http://www.w3.org/TR/html40/struct/links.html#edef-LINK http://www.w3.org/TR/html40/present/styles.html
Type stands for The MIME type of the style sheet. The only supported value I have ever seen is Text/CSS, which is probably why HTML5 has dropped it. I imagine they had it for earlier versions to allow future expansion possibilities which never happened.
类型代表样式表的 MIME 类型。我见过的唯一支持的值是 Text/CSS,这可能是 HTML5 放弃它的原因。我想他们为早期版本提供了它,以允许未来从未发生过的扩展可能性。
Using HTML5 and not specifying the type, I have run so far into no problems with compatibility even when testing older versions of IE.
使用 HTML5 并且不指定类型,到目前为止,即使在测试旧版本的 IE 时,我也没有遇到兼容性问题。
回答by alexmuller
It's not required, no.
这不是必需的,不。
The part of the HTML Living Standard you're interested in is The link
element, which states:
您感兴趣的 HTML Living Standard 部分是The link
element,它指出:
A
link
element must have either arel
attribute or anitemprop
attribute, but not both.The
type
attribute gives the MIME type of the linked resource. It is purely advisory. The value must be a valid MIME type string.For external resource links, the
type
attribute is used as a hint to user agents...
甲
link
元件必须具有一个rel
属性或itemprop
属性,但不能同时使用。该
type
属性给出了链接资源的 MIME 类型。这纯粹是建议性的。该值必须是有效的 MIME 类型字符串。对于外部资源链接,该
type
属性用作对用户代理的提示...
回答by All Workers Are Essential
HTML4.01 does not require the type
attribute on a <link>
tag. According to the specification, the type
attribute is a hint to the browser. If the browser does not support the hinted content type, then it can skip fetching it.
HTML4.01 不需要标签type
上的属性<link>
。根据规范,该type
属性是对浏览器的提示。如果浏览器不支持提示的内容类型,那么它可以跳过获取它。
type = content-type [CI]
This attribute gives an advisory hint as to the content type of the content available at the link target address. It allows user agents to opt to use a fallback mechanism rather than fetch the content if they are advised that they will get content in a content type they do not support.
Authors who use this attribute take responsibility to manage the risk that it may become inconsistent with the content available at the link target address.
类型 = 内容类型 [CI]
此属性提供有关链接目标地址处可用内容的内容类型的咨询提示。它允许用户代理选择使用回退机制而不是获取内容,如果他们被告知他们将获得他们不支持的内容类型的内容。
使用此属性的作者负责管理它可能与链接目标地址处的可用内容不一致的风险。
- NOTE:
<link>
defers to<a>
's definition for thetype
attribute.
- 注意:
<link>
按照属性<a>
的定义type
。
回答by GShenanigan
My understanding is that it's to allow the specification of stylesheets in formats other than text/css.
我的理解是它允许以 text/css 以外的格式指定样式表。
While that has become the dominant (and standard) delivery format for stylesheets for (X)HTML documents, the specification is actually wide enough to allow a variety of different MIME types to be passed, it's just that standard browsers don't implement them.
虽然这已成为 (X)HTML 文档样式表的主要(和标准)交付格式,但该规范实际上足够广泛以允许传递各种不同的 MIME 类型,只是标准浏览器没有实现它们。
回答by David Gilbertson
Not relevant to most people, but some systems (at least Microsoft SharePoint 2013) will use this type attribute when working out how to process HTML (e.g. when creating a .master page file).
与大多数人无关,但某些系统(至少是 Microsoft SharePoint 2013)在研究如何处理 HTML 时(例如,在创建 .master 页面文件时)将使用此类型属性。
回答by prasen sinha
No it just stands for MIME type, it has been deprecated in HTML, We can drop using it without any browser compatibility.
不,它仅代表 MIME 类型,它在 HTML 中已被弃用,我们可以在没有任何浏览器兼容性的情况下放弃使用它。