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

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.

56 Likes

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

Isn’t this requirement tautological?

31 Likes

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.”

196 Likes

Very useful & nice instrections

4 Likes

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.

16 Likes

why does’nt the doctype show anything on the screen

1 Like

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.

48 Likes

I just want to second mtf’s explanation as that is how I interpreted it as well.

4 Likes

This really explains it, nice!

3 Likes

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.
5 Likes

This might sound like a silly question and off topic but would the DOCTYPE HTML declaration be the parent of the the head element?

No, it is not even HTML. The document root is the <html/> element, which can have <head/> and <body/> elements as its direct children.

8 Likes

Can you show an example? Not understanding what you mean when you say CSS files are loaded into the page with or tags…

I think an example would be:

<link href="/media/examples/link-element-example.css" rel="stylesheet">

You can read more about it here.

2 Likes

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.

vvvvvvv

Sorry if i’m butchering this. Still very new

1 Like

What are all the other tags that we can use to start our html docs and what do they signify?

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.

That is a good explanation, this really helps. just want to know if it must be in the Capital letter?

The DTD may be written in upper or lower case. Keep in mind that it is not HTML, but XML.

Thank you for this information.