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.
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.”
There are not only different doctypes, for example if an HTML document happens to be written in HTML 4.01 or in XHTML, but the doctype is also a standard that lets the browser know that the following is an HTML 5 document. It just gives it a little help.
In fact, you will find that the browser will usually display the page just fine, even without the doctype in it. But just because it works, doesn’t mean it is right. Besides, that it can have some unforseen issues, as each browser goes different when it comes to how to interpret elements, tags, style, etc. when it is not properly declared.
Because it is not meant for the screen, but for the user agent. It’s not even in the HTML. It tells the browser the ROOT ELEMENT, <html></html> which is where the HTML is. The part we see on the screen is all in the BODY element. Everything in the HEAD is metadata to help with parsing and indexing.
CSS and JavaScript are used within a HTML document for styling and calculations, however the document remains an HTML document. The doctype simply describes what HTML you are using in the document.
indicates the current standard HTML (HTML5). CSS and JavaScript are then used within the HTML document without changing the sort of doctype it is.
Ok cool, thanks. I think I somewhat understand. You use the
then go to CSS and do whatever else you would like to do with the content.
Ive seen something similar to what you can do with a span tag within CSS but strictly on HTML didnt even have to go to the style sheet to be styled. Just added to the style attribute and was able to manipulate the content.
since there has been different versions of HTML precursor to the current HTML version which is HTML5, the declaration just tells the browser that, the document it is about to parse is in HTML5.