<!DOCTYPE html>
...
<style scoped>
/* css */
</style>
w3.org验证程序给我这个错误:
Line 883, Column 17: Element style not allowed as child of element body in this context.
(Suppressing further errors from this subtree.)
<style scoped>...
Contexts in which element style may be used:
If the scoped attribute is absent: where metadata content is expected.
If the scoped attribute is absent: in a noscript element that is a child of a head element.
If the scoped attribute is present: where flow content is expected, but before any other flow content other than inter-element whitespace and style elements, and not as the child of an element whose content model is transparent.
Content model for element body:
Flow content.
据我了解,'scoped'属性可以将样式标签置于文档的头部之外。那么,为什么验证者对此不满意?
(我使用的是Wordpress,此代码是由插件生成的,这就是为什么我不能只将其放在首位。)
编辑:这不验证-
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<script type="text/javascript"></script>
<style scoped></style>
</body>
</html>
但是,如果script标签位于style标签之后,则可以。是什么原因呢?
W3C标记验证器充当HTML5检查器时,会根据各种草案(例如HTML 5.1 Nightly)处理此问题,该草案现在表示该style
元素只能出现在该head
元素内部,除非scoped
存在该属性,在这种情况下,可能会出现“在需要流内容的地方,但是在元素间空白和样式元素之外的任何其他流内容之前,而不是其内容模型透明的元素的子元素中”。在您的实际示例中,该元素出现在一个script
元素之后(被视为流内容)。因此,更改元素的顺序会在给定定义下将语法更改为有效。
或者,您可以将style
元素包装在一个div
元素中:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<script type="text/javascript"></script>
<div>
<style scoped></style>
</div>
</body>
</html>
scoped
根据W3C建议HTML5,该属性完全无效。它存在于HTML5草案中,但由于缺乏实现而被从建议书中删除,但仍处于“标准化轨道”上,并可能进入HTML 5.1。
请注意,现有的浏览器通常会忽略该scoped
属性,并在style
几乎任何位置允许元素并将其内容应用于整个HTML文档(甚至是style
元素之前的部分)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句