views:

646

answers:

4

I would like to place a comment (<!-- this --> style) at the very top of my HTML, preceding the DOCTYPE declaration. Does the standard allow this? Is it supported by the major browsers? Are there any pitfalls in doing this?

+7  A: 

Writing the DOCTYPE first is certainly best practice.

I remember strange problems a long, long time ago where some browser (probably IE6) ignored a DOCTYPE because there was something seemingly innocent before it - I think just whitespace, but maybe it was a comment. In any case, it was a horrible, horrible bug to have to track down, and there's certainly never any good reason to have comments or whitespace before the DOCTYPE.

Writing the DOCTYPE first is, I'd say, just something experienced web developers do to avoid horrible, elusive bugs.

Triptych
Thanks, I'm going to place my comment after the html tag instead.
Travis Beale
It needs to come before everything including <html> if you want it to work correctly.
nertzy
+7  A: 

While it's acceptable per the standard I believe, you definately want to avoid it, as it'll throw IE into quirks mode.

(See Triggering different rendering modes)

jvenema
Anything other than whitespace preceding the DOCTYPE throws IE (6 and 7, at least — not sure about 8) into quirks mode.
Ben Blank
+3  A: 

That may cause IE7 to render in quirks mode as if a doctype was not there at all, according to this page.

Bing
A: 

Comments before the doctype are allowed, but cause (older) IE versions to revert to quirks mode. They are, actually, used for that purpose sometimes. Note that an XML PI (<?xml version ...>) can have the same effect.

Ms2ger