tags:

views:

598

answers:

7

Strictly speaking, do style tags need to be inside the head of an HTML document? The 4.01 standard implies that, but it's not explicitly stated:

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.

I say "strictly speaking" because I have an app that puts style elements inside the body, and all the browsers I've tested with seem to use the style elements. I'm just wondering if that's actually legal.

+3  A: 

They aren't supposed to go outside the head, but they work anyway; though you might notice a quick flicker. The site shouldn't validate with the style tag outside of the head, but does that really matter? Also, link tags work outside the head as well, even though they aren't supposed to.

geowa4
Saying "they work" is a little bit tricky. At best you can say most current browsers will still render the styles, but there's nothing about this error that just inherently "works." It could not work in any future version of any browser and they wouldn't be doing anything wrong.
Chuck
imo, styles rendered = works; nothing tricky. that last sentence needs to be rewritten; it makes no sense. i mentioned how it wasn't "right" when i said it wouldn't validate, so i must not understand what you meant by that sentence.
geowa4
The problem is that even if they *are* styled, you will have some flicker on the content when those `style`s kick in.
voyager
unless the style tag is first in the body
geowa4
+1  A: 

A style tag anywhere but inside the <head> will not validate with W3C rules.

womp
+1  A: 

If you're in doubt, the W3C markup validator always helps :)

http://validator.w3.org/

Lazlo
+1  A: 

Like the other replies have stated it doesn't actually need to be there. However, it will not validate. This may or may not matter in this instance, but please keep in mind that rendering of html is entirely up to the browsers. From what I know all used browsers of today will support putting it outside the head, but you cannot guarantee that for the future browsers and future browser releases.

Stick with the standard and you are safer. How much safer is up for very much debate.

jim
+6  A: 

style is supposed to be included only on the head of the document.

Besides the validation point, one caveat that might interest you when using style on the body is the flash of unstyled content. The browser would get elements that would be styled after they are displayed, making them shift on size/shape/font and/or flicker. It is generally a sign of bad craftsmanship. Generally you can get away with putting style anywhere you want, but try to avoid it whenever it is possible.

voyager
A: 

Putting the style (link to stylesheet) should go in the Head, as it will be loaded first, and it:

  1. Speed up your website
  2. Make sure all the elements get styled appropriately

It's illegal in my book.

Martin
A: 

Are we talking about an in-line style like <p style='color:red'>
rather than a <style> tag outside the <head> ?

This is deprecated, which I consider a pity, but not actually illegal.

pavium
The question title is specifically asking about STYLE elements and not style attributes. Besides, what would you do with inline styles within the HEAD? Also, the style attribute is not deprecated in HTML 4.01 which the OP is asking about
Andy Ford
But since the question used the bare word 'style' (and called it an element) instead of the tag, I thought there was a possibility that an inline style could have been intended. That's why I asked 'Are we talking about an in-line style?' A simple 'no' would have sufficed.
pavium
no
voyager
:)
voyager
Thanks, voyager, you've lifted a great burden from my mind. The sun is shining once more.
pavium