On 08/14/2011 10:38 PM, Geoff Lane wrote:
On Sunday, August 14, 2011, 8:53:58 PM, Jukka K. Korpela wrote:

For some time I've been under the impression that you could
only use<style>...</style>   in the head.

The impression is correct as far as HTML specifications are considered.
However, browsers generally implement style elements in body, too.
---
Thanks for your input.

I wonder whether that's true of HTML5, as I used<!doctype html>  when
I passed the markup to the W3C validator and it didn't complain that
the<style>  block was illegal, which my experience from other
misplaced elements suggests it would.

Checking http://www.w3.org/TR/html5/semantics.html#the-style-element,
which gives the spec for the style element for HTML5, it states:

4.2.6 The style element
Categories
     Metadata content.
     If the scoped attribute is present: flow content.
Contexts in which this element can 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 other style
     elements and inter-element whitespace.

So, in HTML5, style blocks are legal in the body, but only if they're
the first things other than whitespace to come after the opening body
tag. Since I'm inserting stuff into someone else's page, there will be
a lot of flow content before my style block. Hence, strictly, what I'm
doing is illegal. ... but if browsers support it for now then I'm not
going to complain as this content has a fairly short life (a few
months at most)!

You seem to have missed one important point in the quoted text: "If the |scoped| attribute is present:". The style element is only allowed in the head, *unless* it has the |scoped| attribute, which is described in the specification [1] as:

The scoped attribute is a boolean attribute. If set, it indicates
that the styles are intended just for the subtree rooted at the style
element's parent element, as opposed to the whole Document.

As you noticed, while putting style elements in the body isn't allowed, browsers will still support that. However, they might start rendering the part of the document *before* your style element without its styles, and may then need to re-render that part after parsing the style.

HTH
Ms2ger

[1] http://www.whatwg.org/html/#attr-style-scoped
______________________________________________________________________
css-discuss [css-d@lists.css-discuss.org]
http://www.css-discuss.org/mailman/listinfo/css-d
List wiki/FAQ -- http://css-discuss.incutio.com/
List policies -- http://css-discuss.org/policies.html
Supported by evolt.org -- http://www.evolt.org/help_support_evolt/

Reply via email to