Must doctype be one the First line?

Heys all, is it true that the doctype declaration must be on the very first line (there mustn’t even be a blank line above it) in order for it to be “working correctly” ?

The doctype declaration should be the very first thing in an HTML document, before the <html> tag.

The doctype declaration is not an HTML tag; it is an instruction to the web browser about what version of the markup language the page is written in.

The doctype declaration refers to a Document Type Definition (DTD). The DTD specifies the rules for the markup language, so that the browsers can render the content correctly.

does it mean that it can be at the second or third line and does not necessary have to be the very first line?

As far as I know you can put as many blank lines before it as you want.
The question is, why would you want to?

http://www.w3schools.com/tags/tag_DOCTYPE.asp

oh no, i just want to be sure that having blank spaces/lines before doctype would be acceptable.

I feel there should not be any blank spaces/lines before the doctype. I never tried with this.

Sorry but that W3schools page is full of it. As usual.

Browsers do NOT check to see what kind of markup you’re using. They use the MIME type (sent by your server) to determine if the page is plain text, html, or something else. IE including 9 do not understand the application xhtml+xml MIME type. If you are sending “XHTML” doctyped pages to a browser but are setting text/html as your MIME-type, the browser will consider your page as HTML with errors (closing slashes etc will be ignored as errors). IE will ask you if you want to open the document in something else, like another browser or whatever application you’ve got running.

Browsers do NOT read the DTD. This is why <!doctype html> works all the way back to IE5. However, sending only the first line of the doctype without the reference url WILL put IE into Quirks Mode and possibly the Almost-Standards modes other browsers have… only because they consider it an incomplete doctype. The DTD is for you and the validator.

If you do not want various versions of IE6 to go into Quirks Mode, set your doctype to char1 line1. IE7 I’m not so sure— IE7 has the <?xml?> prologue bug fixed (does not send IE7 into Quirks Mode) but ordinary comments before the doctype can still put IE7 into Quirks Mode.

As far as I’m concerned, if your back-end language is throwing whitespace and newlines before the doctype, consider it the first place to check if you are getting strange errors in IE.

ok so basically the problem is IE6 and possible IE7? the other browsers would not suffer any problems if there are white spaces or not before the doctype declaration right?

So far as I know, this is specifically an IE problem… because of Microsoft’s decision to use the absence/presence of a doctype for “doctype switching”… where older sites without doctypes are rendered the way they were meant back in the 90’s, while sites with a doctype are assumed to be created in this millenium and therefore get “Standards” rendering. (that’s “standards” with quotes around it of course… this is Microsoft we’re talking about…)

Most other browsers seem to have a not-quite-standards rendering mode for no doctype at all, but I haven’t seen anything caused by whitespace/newlines there… even the excessive whitespace and newlines caused by sites outputted by JSP.