How does the browser know what language other documents are written in? For example, why doesn’t css need a doctype?


#1

Question

How does the browser know what language other documents are written in? For example, why doesn’t css need a doctype?

Answer

DOCTYPE declarations are only required for html based documents. Other JavaScript or CSS files are actually loaded into the page with <link> or tags. These tags signify the file type to the browser thereby allowing the browser to properly parse the file.


FAQ: Learn HTML Elements - Common HTML Elements - Preparing for HTML
#2

Why is DOCTYPEtag needed to identify the file is HTML when the file already starts with
and ends
</html>

Isn’t this requirement tautological?


#3

There are different doctypes for different versions of HTML—all of which still use the .html file extension and the <html></html> tag pair. For the current version, the doctype declaration has been simplified to:

<!DOCTYPE html>

but this is still telling the browser what version is being used (HTML5). If you were to look at a doctype on an older HTML document, then you might see a longer doctype like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Even though the HTML5 doctype declaration is dramatically simplified, it is still required so that the browser knows which version of HTML is being used. Once it knows, then it is able to render the document in “strict mode.” If no doctype is declared, then it can still tell that the document is HTML based on the file extension and <html></html> tag pair, but since it does not know which version of HTML is being used, it renders the document in “quirks mode.”


#4

Very useful & nice instrections