What's a minimally valid HTML5 document?
Asked Answered
C

4

35

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>

Congregational answered 21/3, 2012 at 0:34 Comment(9)
Just tried it in validator.w3.org/#validate_by_input It doesn't like it, reading spec now....Adenocarcinoma
@Adam, if you add an empty set of <title></title> tags on the second line, it passes the validation.Cyrilcyrill
You MUST have the <html> tag, as written in your link : "Documents must consist of [...] The root element, in the form of an html element." For <head> and <body>, I'll have a look.Eugenieeugenio
@samsamX, no, that's inaccurate. If you read the whole doc you see later: "an HTML document always has a root html element, even if the string <html> doesn't appear anywhere in the markup.". If you omit the <html> tag in a valid way, there is an implicit root html element anyway.Cyrilcyrill
Yep, I was wrong. I'm reading the whole page and it's (effectively) pretty disturbingEugenieeugenio
@samsamX, they trick you into believing it was meant to be read by humans. Even a laywer would have a hard time with the HTML5 spec.Cyrilcyrill
Note that this is not an HTML5 thing. Every version of HTML (except XHTML) has specified this.Registered
@BenLee - I disagree. While there are a few tricky bits of the HTML5 spec where you need to concentrate quite hard (the handling of namespaces springs to mind), the vast majority of it is very readable.Registered
@Alohci, okay, I was using hyperbole. But still you have to admit it's a bit dense, even if it's understandable with careful reading. Someone even put together this: developers.whatwg.org -- an explanation of the spec designed with readability in mind, and also designed with developers in mind (anything important only to browser vendors is removed)Cyrilcyrill
D
40

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

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>
Duffy answered 10/10, 2014 at 17:20 Comment(1)
It might be worth mentioning <meta charset="UTF-8"> here since characters may not render correctly without it. (even if the validator doesn't complain). Also !DOCTYPE may sometimes need to be uppercase see: #7021461Unlikely
W
21

This is the minimal HTML5-valid document:

<!doctype html><title> </title>
Windproof answered 21/3, 2012 at 0:39 Comment(3)
According to MDN even the title tag may be omitted "if the element is an <iframe> srcdoc, or if title information is available from a higher level protocol". So the minimal HTML5-valid document is context-dependent. (I have heard it alleged that 'title information is available from a higher level protocol' covers email.)Birck
It is no longer valid, since now HTML5 drafts require that the title element have nonempty content. Any character (even a space) there will do. (Until, perhaps, they change the rules again.)Gamine
According to Validator.nu and the W3C validator the <title> tag with a whitespace is regarded as empty. There has to be some non-space character inside.Naughton
G
13

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

Gabrila answered 24/2, 2015 at 5:40 Comment(1)
Is it true that the spec requires that? I see "Content model: Text that is not inter-element whitespace." But inter-element whitespace is defined as being certain forms of Text nodes. <title></title> has no text node children so it does not have inter-element whitespace. Text on the other hand is defined as either nothing, or text nodes. "Nothing" matches so a strict reading says that the content model is satisfied. No doubt that's not the intent of the definitions, but maybe there's a spec flaw there.Registered
R
8

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.

Registered answered 21/3, 2012 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.