开发者

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

开发者 https://www.devze.com 2023-02-07 00:14 出处:网络
This question already has answers here: 开发者_JAVA技巧 Closed 10 years ago. Possible Duplicate: Does <STYLE> have to be in the <HEAD> of an HTML document?
This question already has answers here: 开发者_JAVA技巧 Closed 10 years ago.

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.


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


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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号