What's a valid HTML5 document?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 16:27:15

问题


I've just been reading the HTML5 author spec. It states that the <html>, <head> and <body> tags are optional. Does that mean that you can leave them out completely and still have a valid HTML5 document?

If I'm interpreting this correctly, it means this should be completely valid:

<!DOCTYPE html>
<p>Hello!</p>

Is this correct?

You can check out the spec here:

http://dev.w3.org/html5/spec-author-view/syntax.html#syntax

"8.1.2.4 Optional tags" is the bit out about it being OK to omit <html>, <head> and <body>


回答1:


This is the minimal HTML5-valid document:

<!doctype html><title> </title>



回答2:


The title element is indeed required, but as Jukka Korpela notes, it also must be non-empty. Furthermore, the content model of the title element is:

Text that is not inter-element white space.

Therefore, having just a space character in the title element is not considered valid HTML. You can check this in W3C validator.

So, an example of a minimal and valid HTML5 document is the following:

<!doctype html><title>a</title>



回答3:


W3C HTML validator maintainer here. FYI with regard to the validator behavior, as of today, the validator now enforces the requirement in the HTML spec that the title element must contain at least one non-whitespace character -

http://validator.w3.org/nu/?doc=data%3Atext%2Fhtml%3Bcharset%3Dutf-8%2C%3C%2521doctype%2520html%3E%3Ctitle%3E%2520%2520%2520%3C%252Ftitle%3E




回答4:


While the <html>, <head> and <body> start and end tags are optional, the <title> tags are required, except in special circumstances, so no, your sample is not (ordinarily) valid.




回答5:


I think you are reading it correctly. Although browsers will even render incorrect HTML (try breaking the rules and FF will render the same).



来源:https://stackoverflow.com/questions/9797046/whats-a-valid-html5-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!