The Doctype Declarations or Document type declaration is an HTML declaration that must be written at the very top of any HTML document. This should not be confused with HTML tags, as Doctype declaration, which is the short form for document type declaration, is meant to tell the browser or the validator, what is the version of the markup language used on a given web page. Whereas Tags are there for the search engines and helps them understand information about the content, rather than markup.
While this is not the case anymore, a Doctype declaration that included a full web address also instructed the browsers to fetch and display the pages in the approved standards-compliant mode – the XHTML, CSS, HTML, or DOM – as they should. However, when using HTML5, the Doctype is there only to enable us to adhere to the standard mode of document writing.
In modern browsers that use this standards-compliant mode, the lack of Doctype or a faulty Doctype declaration means that the markup will not be validated as it should, resulting in an error.
What happens with errors or no Doctype in the document?
What happens if we do not use the Doctype declaration in an HTML document and try to render it on a browser? Well, the browser will read it as invalid since this goes against standards-compliant mode. However, the browser would fall back to its former version. This means that the browser figures out that there could be compatibility, and parses the HTML or other markup language document in question
For instance, Microsoft Edge would probably revert to a previous version similar to the old Internet Explorer and try to parse and display the CSS files that way. The same thing would happen if it were on Firefox or any other modern browser. But then the displayed page may not look up to date, and one would be able to tell there is something wrong. It can also result in different user experiences since each visitor uses a different operating system and a different browser.
Doctype in HTML 4 version
Before HTML 5, we had the HTML 4 version which used the Doctype declaration in a pretty different way. Since HTML 4.01 is associated with SGML or the Standard General Markup Language, the Doctype declaration enables the connection but references to DTD or Document Type Definition, instead. For the browsers to comply and render information as it should, the document type definition must be given so that it stipulates the rules for SGML.
Moreover, unlike in HTML5 where Doctype must appear at the top of the web pages, in HTML 4 the Doctype appears after we have declared the XML version. The HTML 4 Doctype declaration one also encloses the URL or full web address which is not the case with Doctype in HTML5.
This is an example of an HTML 4 Doctype syntax, and it is quite clear to see the differences.
<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
While today, we start our HTML document using the declaration <!Doctype>, in the above example this is not the case. Also, all HTML 4.01 documents conform to one of three Standard General Markup Language Document Type Definition in the doctype declaration. In turn, the public identifiers of these DTDs are constant and are as follows:
In the HTML 5 version, the Doctype as seen earlier is much shorter, since there is no use of document type definition that includes referencing a complete URL. Also, HTML 5 is quite different from its predecessor, HTML 4, and it is not based on the same Standard General Markup Language.