tags:

views:

290

answers:

3

I see something like this:

<div>
<style type="text/css">
...
</style>
</div>

It's very strange,but still work.

Is this against the standard?

+3  A: 

Yes, it violates the HTML specification.

<!ELEMENT DIV - - (%flow;)*            -- generic language/style container -->

(from the div section of the specification)

Follow the hyperlinks in the live version if you want to see exactly how %flow; expands (it doesn't include style).

Browsers just tend to do huge amounts of error recovery because so many authors do stupid things.

Don't depend on error recovery — there are lots of browsers out there, and they don't all behave the same way when the HTML doesn't conform to spec.

David Dorward
But both firefox and IE will work,can you list one example that won't work?
Shore
And can you quote the specification that proves it's against the standard?
Shore
I don't know any specific browser which can't recover from that error, but there are lots of browsers I haven't tested (even if they are fairly obscure) and lots of browsers that haven't been written yet.
David Dorward
"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." - from http://www.w3.org/TR/html4/present/styles.html#edef-STYLE . Would you do something that works, but is wrong, just because you can? And there is still no real point in having STYLE-elements scattered around your documents. Use external stylesheets and change what's needed in different sheets for different views if you need that.
Arve Systad
+2  A: 

The STYLE element is only allowed as child of the HEAD element. See this explanation for further details.

Gumbo
+1  A: 

It's worth pointing out that although it's invalid HTML, it's also extremely common, and any browser that didn't support it would fail to render properly a significant portion of the web.

Mash-ups in particular, need use of this feature, and HTML 5 defines <style scoped> to deal with this use case. <style scoped> can appear in the body, though styles so defined do not apply to the whole document, only to the section in which <style scoped> appears.

WARNING: HTML 5 is a draft, and there is no guarantee that <style scoped> or any other HTML 5 feature that is not already implemented will ever be implemented.

Alohci
BTW,does the <style> inside <body> has the feature you referred to as <style scoped> in current version of HTML?Will it only apply to the section or the whole document?
Shore
It'll apply to the whole document in legacy browsers, because the "scoped" attribute will simply be ignored.
Alohci
Not just mashups, also bad CMSs, like SharePoint (though I've only worked with vanilla WSS3) where you can't add another stylesheet to the page and you can't access the head of the document. style attributes can get worse than font tags very quickly
John Ferguson