Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does my HTML5 doctype have to be on the VERY first line?

Tags:

I've always wondered whether whitespace before a DOCTYPE matters, but I can't seem to find any definitive information on the web. People say not to do it, but I can't see what effect it's likely to have, or even if the spec says it must be like that. I can't figure out if it triggers compatibility mode in IE or anything annoying like that.

The reason is, in my (crappy asp.net) code, I would much prefer to write:

<%@ Page language="c#" Codepage="65001" AutoEventWireup="true" %> <%@ OutputCache Location="None" VaryByParam="none" %> <!doctype html> 

than:

<%@ Page language="c#" Codepage="65001" AutoEventWireup="true" %><%@ OutputCache Location="None" VaryByParam="none" %><!doctype html> 

Because the first is neater. But the result of the top one is a few blank lines in your rendered html.

Not a huge issue. I've just always wondered if it matters :)

like image 443
captainclam Avatar asked Jun 07 '12 02:06

captainclam


People also ask

Does DOCTYPE HTML have to be the first line?

The HTML document type declaration, also known as DOCTYPE , is the first line of code required in every HTML or XHTML document. The DOCTYPE declaration is an instruction to the web browser about what version of HTML the page is written in.

What happens when <! DOCTYPE HTML is not written as first line of HTML5 document?

The absence of the DOCTYPE or its incorrect usage will force the browser to switch to quirks mode. It means that the browser will do its best to layout the page that is considered to be old or created against web standards.

What is the proper HTML5 DOCTYPE?

Definition and Usage All HTML documents must start with a <! DOCTYPE> declaration. The declaration is not an HTML tag. It is an "information" to the browser about what document type to expect.

Why is the first line in an HTML5 document <! DOCTYPE HTML?

DOCTYPE> tag is the very first line in the HTML document. It tells the browser what version of HTML the document is written so that the browser knows what to expect. This tag is also commonly referred to as the <! DOCTYPE> element.


1 Answers

Yes! comments and spaces may come first.

Here's the relevant section from the official HTML Specification

like image 142
Ray Toal Avatar answered Sep 30 '22 06:09

Ray Toal