I would like to define a snippet of CSS in my page like this:
我想在我的页面中定义一个CSS片段,如下所示:
<style type="text/css">
hr {color:sienna;}
p {margin-left:20px;}
</style>
I keep reading that it should be defined within the <head>
element, but it seems to work fine when inline. Can someone confirm if this is okay?
我一直在读它应该在元素中定义,但它在内联时似乎工作正常。有人可以确认这是否可以?
4 个解决方案
#1
8
For HTML 4 the spec says:
对于HTML 4规范说:
The STYLE element allows authors to put style sheet rules in the head of the document. HTML permits any number of STYLE elements in the HEAD section of a document.
STYLE元素允许作者将样式表规则放在文档的头部。 HTML允许文档的HEAD部分中的任意数量的STYLE元素。
Reference: http://www.w3.org/TR/html4/present/styles.html#h-14.2.3.
参考:http://www.w3.org/TR/html4/present/styles.html#h-14.2.3。
Their specification of "head
of the document", rather than simply 'document' strongly suggests that elsewhere is inappropriate.
他们对“文件负责人”的说明,而不仅仅是“文件”强烈暗示其他地方是不合适的。
For HTML 5, however, this is relaxed and the style
element can be placed within the document itself:
但是,对于HTML 5,这是放松的,样式元素可以放在文档本身中:
The style element allows authors to embed style information in their documents. The style element is one of several inputs to the styling processing model. The element does not represent content for the user.
style元素允许作者在其文档中嵌入样式信息。样式元素是样式处理模型的几个输入之一。该元素不代表用户的内容。
Reference: http://www.w3.org/TR/html5/semantics.html#the-style-element.
参考:http://www.w3.org/TR/html5/semantics.html#the-style-element。
#2
1
Most browsers put it anywhere in the page, but just be aware that it only takes effect from that point onwards. Also, it's not valid HTML if you don't put it in the head element.
大多数浏览器将它放在页面的任何位置,但请注意,它只会从那时开始生效。此外,如果您不将其放在head元素中,则它不是有效的HTML。
Also, it's considered best practise to put it in the head element as it improves page render times.
此外,最好将它放在head元素中,因为它可以改善页面渲染时间。
#3
1
It is not OK.
不行。
While some browsers might (mistakenly) care about it when not in the HEAD element, this is not behavior you should rely on, as it is counter to the HTML standard and may or may not work in the future for any given browser.
虽然某些浏览器可能(错误地)在不在HEAD元素中时关心它,但这不是您应该依赖的行为,因为它与HTML标准相反,并且将来可能或可能不适用于任何给定的浏览器。
Edit: Update: In HTML 5, style elements can be scoped to only apply to a subtree, in which case they don't need to be in the head element.
编辑:更新:在HTML 5中,样式元素的范围可以仅适用于子树,在这种情况下,它们不需要位于head元素中。
They still, however, need to be in front of any other content they apply to, so the same principle applies.
但是,他们仍然需要在他们适用的任何其他内容面前,因此同样的原则适用。
#4
1
It is not strictly valid unless you are using HTML5 and the scoped attribute.
除非您使用HTML5和scoped属性,否则它不是严格有效的。
http://www.w3schools.com/html5/tag_style.asp
http://www.w3schools.com/html5/tag_style.asp
Although all browsers that I know of will accept the tag anywhere in the document.
虽然我所知道的所有浏览器都会在文档中的任何位置接受标记。
#1
8
For HTML 4 the spec says:
对于HTML 4规范说:
The STYLE element allows authors to put style sheet rules in the head of the document. HTML permits any number of STYLE elements in the HEAD section of a document.
STYLE元素允许作者将样式表规则放在文档的头部。 HTML允许文档的HEAD部分中的任意数量的STYLE元素。
Reference: http://www.w3.org/TR/html4/present/styles.html#h-14.2.3.
参考:http://www.w3.org/TR/html4/present/styles.html#h-14.2.3。
Their specification of "head
of the document", rather than simply 'document' strongly suggests that elsewhere is inappropriate.
他们对“文件负责人”的说明,而不仅仅是“文件”强烈暗示其他地方是不合适的。
For HTML 5, however, this is relaxed and the style
element can be placed within the document itself:
但是,对于HTML 5,这是放松的,样式元素可以放在文档本身中:
The style element allows authors to embed style information in their documents. The style element is one of several inputs to the styling processing model. The element does not represent content for the user.
style元素允许作者在其文档中嵌入样式信息。样式元素是样式处理模型的几个输入之一。该元素不代表用户的内容。
Reference: http://www.w3.org/TR/html5/semantics.html#the-style-element.
参考:http://www.w3.org/TR/html5/semantics.html#the-style-element。
#2
1
Most browsers put it anywhere in the page, but just be aware that it only takes effect from that point onwards. Also, it's not valid HTML if you don't put it in the head element.
大多数浏览器将它放在页面的任何位置,但请注意,它只会从那时开始生效。此外,如果您不将其放在head元素中,则它不是有效的HTML。
Also, it's considered best practise to put it in the head element as it improves page render times.
此外,最好将它放在head元素中,因为它可以改善页面渲染时间。
#3
1
It is not OK.
不行。
While some browsers might (mistakenly) care about it when not in the HEAD element, this is not behavior you should rely on, as it is counter to the HTML standard and may or may not work in the future for any given browser.
虽然某些浏览器可能(错误地)在不在HEAD元素中时关心它,但这不是您应该依赖的行为,因为它与HTML标准相反,并且将来可能或可能不适用于任何给定的浏览器。
Edit: Update: In HTML 5, style elements can be scoped to only apply to a subtree, in which case they don't need to be in the head element.
编辑:更新:在HTML 5中,样式元素的范围可以仅适用于子树,在这种情况下,它们不需要位于head元素中。
They still, however, need to be in front of any other content they apply to, so the same principle applies.
但是,他们仍然需要在他们适用的任何其他内容面前,因此同样的原则适用。
#4
1
It is not strictly valid unless you are using HTML5 and the scoped attribute.
除非您使用HTML5和scoped属性,否则它不是严格有效的。
http://www.w3schools.com/html5/tag_style.asp
http://www.w3schools.com/html5/tag_style.asp
Although all browsers that I know of will accept the tag anywhere in the document.
虽然我所知道的所有浏览器都会在文档中的任何位置接受标记。