So, for example, if my HTML document does not declare the HTML5 doctype, but I use something in the HTML5 spec, will that still work? Does that cause problems for some browsers? Will the document still validate?
3 Answers
It depends on the target browsers. Webkit-browsers, Firefox and Opera will handle HTML5 elements quite normally even if your doctype is not HTML5.
IE9 on the other hand (I bet you saw this coming), may behave entirely differently on another type of doctype. If IE9 is not in IE9 Standards mode (it could be in quirks, IE8 compat, whatever), it will not support the HTML5 features it does in IE9 standards.
So essentially your main concern is that you need to make sure all browsers go into strict standards mode. The easiest way to achieve this is to use the HTML5 doctype, since it will trigger standards mode in all browsers - including older browsers that don't actually support HTML5.
-
If you are targeting IE9+, you can use mime
application/xhtml+xml
and force standards mode without a Doctype at all.– HawkenNov 2, 2013 at 5:50 -
application/xhtml+xml
forces XML parsing in most browsers which is not HTML5. Nov 2, 2013 at 17:16 -
XML parsing is the same for XHTML 1.x or XHTML5. There aren't seperate 'modes' since XML parsing ignores the Doctype.– HawkenNov 2, 2013 at 19:01
-
The question was specifically for HTML5, not XHTML5. These are two very different things. Nov 4, 2013 at 11:41
-
An XML syntax error will in some browsers prevent displaying the page at all. The way you suggest would cause this if the user had invalid XML on the page. Valid HTML5 (such as no closing tag) = invalid XML. I would say there's a very big and very important difference there... Nov 5, 2013 at 13:42
Yes, you can, however, it will not validate and possibly look weird in some or all browsers. You shouldn't do it. If you are using HTML5, declare a proper doctype: <!doctype html>
.
In the HTML5 specs, it says that the doctype is not mandatory, however, no browser wide browser implementation of that rule yet exists. In theory, you should be able to do it without any problems.
-
2The HTML5 specs do require a doctype for text/html documents. They do not require a doctype for XML resources, and the browsers that support XHTML served as an XML resource, do always use standards mode for such resources with or without a doctype.– AlohciAug 30, 2011 at 19:58
You can, but the behavior will be undefined. This means that your results may be [even more] inconsistent across browsers, and that it may fail in some circumstances for unknown reasons.
It definitely won't validate, and it is certainly not something you should do.