Why can’t <style> tags within the HTML <body> tag validate on W3C? [duplicate]

佐手、 提交于 2019-12-23 06:48:37

问题


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.

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.

The style tag functions perfectly well within the body tag across all browsers, yet it refuses to validate.


回答1:


The HMTL DTD does not allow it, therefore it doesn't validate. Doesn't matter, all browsers support it anyway.




回答2:


The HTML 4 spec states that is supposed to be within the HEAD element, not the BODY element. See also this related (duplicate?) question

does-style-have-to-be-in-the-head-of-an-html-document

W3C HTML 4.01 ref on <style>

HTML permits any number of STYLE elements in the HEAD section of a document.




回答3:


<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.




回答4:


Literally, a <style> tag within the <body> tag won’t validate because all the HTML specs say it can’t be there.

As to why the 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.

I imagine there are some cases where including a <style> tag in the body makes page rendering harder/slower/less reliable.




回答5:


What are you validating against? HTML, XHTML Transitional, XHTML strict?

Probably because inline styles are not valid in XHTML Strict. You need to use class="foo" instead, and define foo inside a CSS file.



来源:https://stackoverflow.com/questions/4803518/why-can-t-style-tags-within-the-html-body-tag-validate-on-w3c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!