tags:

views:

364

answers:

5

[This is related to this question, but not since it's not about email.]

In many cases -- especially when working with a CMS or someone else's framework, it's much easier to embed <style> tags and <script> tags in the <body> than in the <head>. This seems to work in IE6, IE7 (Windows), Firefox 3.x and Safari (OS X).

Strictly speaking, is this wrong? And if it is, what negative consequences might it cause... aside from being completely ignored in some clients?

Note: Glad everybody wants to talk about DRY and centralizing styles. Imagine for a second that I want to use style tags within a document because they ARE NOT GLOBAL and that I DO NOT HAVE ACCESS TO THE HEAD ON A PER-PAGE BASIS. For whatever reason, be it that the site differs on a page-by-page basis, or a per-paragraph basis or whatever. I'm not interested in it being hard to track down and change. I'm worried about possible consequences of using style in the body.

You centralize stuff that's central. Everything else is bloat in the central stylesheets.

+6  A: 

XHTML 1.0 Strict:

<script> is permitted in the body, and <style> is permitted only in the head.

HTML 4.01 Transitional:

<script> is permitted in the body, and <style> is permitted in the head and title.

Jonathan Sampson
In the `title`?
Gumbo
The Transitional DTD explicitly excludes style elements from the title.
David Dorward
"Referenced in: HEAD, TITLE"
Jonathan Sampson
And title shows as its contents "Content (#PCDATA)-(SCRIPT|STYLE|META|LINK|OBJECT)"
Jonathan Sampson
@Johnathan Sampson: The minus is to be read as exclusion.
Gumbo
@Gumbo `title` is referenced inclusively on the spec for `style` as a parent tag. If the minus were to be exclusive, that list following it would contain many more tags, no?
Jonathan Sampson
@Jonathan Sampson: I don’t know why they explicitly excluded those elements. But the definition of `TITLE` (`<!ELEMENT TITLE - - (#PCDATA) -(%head.misc;) -- document title -->`, see http://www.w3.org/TR/html4/struct/global.html#edef-TITLE) is to be read as: `TITLE` is the element name; `-` means the opening tag must not be ommited (may be ommited if `o`); `-` means the closing tags must not be ommited; `(#PCDATA)` defines the allowed content data type; `-(%head.misc;)` means elements of *head.misc* are not allowed; `-- document title --` is just a comment.
Gumbo
@Jonathan Sampson: But you could also just write an example document with a `STYLE` element as child of the `TITLE` element and let it be validated by an HTML validator.
Gumbo
@Gumbo, I wasn't out to scrutinize what I read in the spec. I naively trusted it :) Perhaps the style tag is not permitted within title (the mention that it is sounds crazy, IMHO).
Jonathan Sampson
You read a summary of the spec which says that the title element *references* the style element. The reference is to say "You can't have this here" (rather than the more common "You can").
David Dorward
+2  A: 

Well, you have the problem of directly embedding styles and scripts into your content. The primary mantra here is the DRY (Don't Repeat Yourself) Principle. You may use a script or particular style in multiple places. When that style or script requires modification, you now get to go on a scavenger hunt for all the places where that code exists. Keeping your styles and scripts in a common place is ideal.

On the other hand, if it is a minor style issue (pixel pushing or something related to just that one view), it's probably okay.

Secret Agent Man
true, obviously we're talking about one-offs. Things that are used in more than one place are centralized.
Yar
A: 

The biggest problem, in my opinion, is convenience. If you want to change the style of a page, it's much easier to do so if all the style and script information is in one area. It's possible for style/script information to be in a <style> node, in the style attribute of a node (i.e. <body style='...'>) or in an external file (i.e. <link rel='stylesheet' type='text/css' href='style.css' />). It's much easier to use a consistent location than to try to hunt down all the places that a style could occur.

It's also worth noting that saying, "aside from being completely ignored in some clients" is akin to saying "aside from exploding when you hit it from behind" or "aside from flying off-course and landing in a civilian neighborhood". That's severe enough a problem in itself to warrant using the standard practice.

Imagist
I disagree, re: last point. HTML and CSS is so unpredictable, IMO, that if I don't know about a certain client, ignoring my styling might be a lot safer than respecting it in an unknown way.
Yar
A: 

But why would you have style-tags in the body? The styles are global anyways, so i can't find any logical reason to do so.

To simplify and separate things even more you should use external stylesheets too.

Scripts are allowed, and is there for a reason: They might give output, they should be run at specific times and other reasons.

Arve Systad
Some MVC developers load specific stylesheets into the content of their views, which may plant them somewhere in the middle of a template, and thus in the body.
Jonathan Sampson
Then you should either put everything into one stylesheet (or one set of stylesheets) that is always included, or generate one style sheet depending on the view you're at. If you need separate stylesheets for every view, you've just misunderstood one of the basic principles of CSS. It's always been done with a fixed (set of) stylesheet(s) - why can't it be done so now?
Arve Systad
We should limit this conversation to whether you can do it or not, I think. I might, on a certain sight, have particular styles that are NOT global but rather on a per paragraph basis, or a per page basis. Using style is somewhat more readable than using style="" attribute.
Yar
That is why have class-selectors :-) You might also add an ID-attribute to the body-element, and change things accordingly. Silly example: `body#about p { font-weight: bold; }`
Arve Systad
Totally, but if it's only in one place in your system ever, then it shouldn't be in the central stylesheet: then it's just bloat. Example: 1px movements to get stuff to look right in IE. Then you have #thinger .ie and #thinger.
Yar
you don't know if you need a style until you are deep into formatting a document. example: some pages include code samples deep inside nested tables and some don't -- should they all have the styles for code samples and every other possible style for the entire web site?
Aaron Watters
+1  A: 

The short answer:


The detailed answer:

STYLE is defined to be in head.misc:

<!ENTITY % head.misc "SCRIPT|STYLE|META|LINK|OBJECT" -- repeatable head elements -->

And elements of head.misc are only allowed to be children of the HEAD element. So STYLE is only allowed to be child of the HEAD element.

SCRIPT is defined to be in head.misc and in special:

<!ENTITY % special
   "A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">

And special is defined to be in inline:

<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">

Additionally SCRIPT is also allowed to be child of the BODY element. So SCRIPT is allowed in the HEAD element nad wherever inline is allowed.

Gumbo
so what does it mean that most browsers parse in the body? That it should not be used anyway?
Yar
@yar: Yes, it’s not part of the standard and thus should not be used although it may work in most browsers.
Gumbo