– omg Sep 06 '09 at 13:13

  • It'll apply to the whole document in legacy browsers, because the "scoped" attribute will simply be ignored. – Alohci Sep 06 '09 at 16:42
  • 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 Aug 26 '10 at 16:42
  • 7

    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.

    Quentin
    • 914,110
    • 126
    • 1,211
    • 1,335
    • But both firefox and IE will work,can you list one example that won't work? – omg Sep 06 '09 at 10:52
    • And can you quote the specification that proves it's against the standard? – omg Sep 06 '09 at 10:56
    • 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. – Quentin Sep 06 '09 at 10:57
    • "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 Sep 06 '09 at 14:51
    4

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

    Community
    • 1
    • 1
    Gumbo
    • 643,351
    • 109
    • 780
    • 844
    4

    In HTML5, a <style> tag is allowed to appear under anything in <body> or <head>.

    Mostly you are not allowed to put blocking elements into inline elements but meta elements like style, script, meta may appear wherever you want.

    Daniel W.
    • 31,164
    • 13
    • 93
    • 151
    • Can you provide a source? It's not letting me use ` – chovy Feb 16 '23 at 11:47
    • @chovy [w3.org - The style element](https://www.w3.org/TR/2011/WD-html5-author-20110809/the-style-element.html) - you might need the [`scoped` attribute](https://www.w3.org/TR/2011/WD-html5-author-20110809/the-style-element.html#attr-style-scoped). – Daniel W. Feb 16 '23 at 19:28
    • my bad, i forgot to close the `` tag – chovy Feb 16 '23 at 20:18
    0

    I found an example where the tag inside a div is not read correctly by IE8 (it works fine in Firefox and Chrome) I can't reproduce it with a simple example because I don't know where the problem is. But if I move the outside the div everything works again. Why I'm using a tag inside a div? because I'm loading external data with AJAX inside that div, and I don't know other way to add the styles in that situation.

    Enrique
    • 4,693
    • 5
    • 51
    • 71
    • Define the styles you intend to apply as a series of classes in your main CSS file. Upon completion of the AJAX request when you're generating the DOM elements, use the `Element.addClass()` method. – Micah Henning Dec 06 '12 at 15:17