简体   繁体   中英

Why is DOCTYPE declaration not a HTML element or HTML tag?

Why and what is the reason behind for the DOCTYPE declaration to not be a valid HTML element or tag? Would it be considered as valid HTML code though?

I do know that HTML5 standard defines tags as below:

Tags are used to delimit the start and end of elements in the markup.

However, there are self closing tags such that do not delimit any text content as an element either, which led me to be a bit confused and ask here.

Technically <!DOCTYPE > is not a tag/element, it just an instruction to the browser about the document type or to provide links to a set of rules that the HTML page had to follow, which could mean automatic error checking etc.

Because it doesn't need to be a tag. It's almost a redundancy for the .html file extension. It just tells the browser the type of document that it is receiving.

Would it be considered as valid HTML code though?

To make your code valid, it is required (not all of the time in preset environments), if that is your question.

However, there are self-closing tags that do not delimit any text content as an element either, which led me to be a bit confused and ask here.

These are elements that carry information in their attributes (such as input, img, etc). The DOCTYPE declaration has no attributes (aside from the type of HTML) and thus, does not need to be an HTML tag or element.

There are some other reasons that go into DOM parsing which you can read about in this question if you're interested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM