为什么 HTML <body> 标签中的 <style> 标签不能在 W3C 上验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4803518/
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
Why can’t <style> tags within the HTML <body> tag validate on W3C?
提问by chris
Possible Duplicate:
Does <STYLE> have to be in the <HEAD> of an HTML document?
I am part of a team that has developed a CMS system.
我是开发 CMS 系统的团队的一员。
We would like to be able to insert <style>
tags within the <body>
of an HTML page in a way that validates to W3C standards, as validation is a requirement of our SEO consultant.
我们希望能够以符合 W3C 标准的方式在 HTML 页面中插入<style>
标签<body>
,因为验证是我们 SEO 顾问的要求。
The style tag functions perfectly well within the body tag across all browsers, yet it refuses to validate.
style 标签在所有浏览器的 body 标签中运行良好,但它拒绝验证。
回答by Spliffster
The HMTL DTD does not allow it, therefore it doesn't validate. Doesn't matter, all browsers support it anyway.
HMTL DTD 不允许它,因此它不会验证。没关系,反正所有浏览器都支持它。
回答by RichardTheKiwi
The HTML 4 spec states that is supposed to be within the HEAD element, not the BODY element. See also this related (duplicate?) question
HTML 4 规范规定它应该在 HEAD 元素中,而不是在 BODY 元素中。另请参阅此相关(重复?)问题
does-style-have-to-be-in-the-head-of-an-html-document
do-style-have-to-be-in-the-head-of-an-html-document
HTML permits any number of STYLE elements in the HEADsection of a document.
HTML 允许在文档的HEAD部分中包含任意数量的 STYLE 元素。
回答by Quentin
<style>
elements contain meta data that applies to the whole document. This is what the <head>
element was designed to contain. It just makes sense for them to live in the head, so that is where the specification requires that they be.
<style>
元素包含适用于整个文档的元数据。这就是该<head>
元素旨在包含的内容。让它们活在头脑中是有意义的,所以这就是规范要求它们存在的地方。
回答by Paul D. Waite
Literally, a <style>
tag within the <body>
tag won't validate because all the HTML specs say it can't be there.
从字面上看,<style>
标签中的<body>
标签不会被验证,因为所有的 HTML 规范都说它不能存在。
As to whythe specs say that, I don't know. The requirement is still in the latest HTML spec, and recently things that work in the real world that were forbidden by previous specs (e.g. omitting the type
attribute on the <style>
tag) have been allowed, so I'm assuming there must be a reason why the requirement is still there.
至于为什么规格这么说,我不知道。该要求仍然在最新的 HTML 规范中,并且最近允许在现实世界中工作的先前规范禁止的事情(例如省略标签type
上的属性<style>
),所以我假设一定有一个原因需求还是有的。
I imagine there are some cases where including a <style>
tag in the body makes page rendering harder/slower/less reliable.
我想在某些情况下,<style>
在正文中包含标签会使页面呈现更难/更慢/更不可靠。
回答by JK.
What are you validating against? HTML, XHTML Transitional, XHTML strict?
你在验证什么?HTML、XHTML 过渡、XHTML 严格?
Probably because inline styles are not valid in XHTML Strict. You need to use class="foo"
instead, and define foo inside a CSS file.
可能是因为内联样式在 XHTML Strict 中无效。您需要class="foo"
改用并在 CSS 文件中定义 foo 。