33

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>

TylerH
  • 20,799
  • 66
  • 75
  • 101
d13
  • 9,817
  • 12
  • 36
  • 44
  • Just tried it in http://validator.w3.org/#validate_by_input It doesn't like it, reading spec now.... – Adam Mar 21 '12 at 00:40
  • @Adam, if you add an empty set of `` tags on the second line, it passes the validation. – Ben Lee Mar 21 '12 at 00:41
  • You MUST have the `` tag, as written in your link : "Documents must consist of [...] The root element, in the form of an html element." For `` and ``, I'll have a look. – zessx Mar 21 '12 at 00:41
  • 1
    @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 doesn't appear anywhere in the markup.". If you omit the tag in a valid way, there is an implicit root html element anyway. – Ben Lee Mar 21 '12 at 00:44
  • Yep, I was wrong. I'm reading the whole page and it's (effectively) pretty disturbing – zessx Mar 21 '12 at 00:45
  • 2
    @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. – Ben Lee Mar 21 '12 at 00:46
  • Note that this is not an HTML5 thing. Every version of HTML (except XHTML) has specified this. – Alohci Mar 21 '12 at 00:48
  • @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. – Alohci Mar 21 '12 at 00:55
  • @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: http://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) – Ben Lee Mar 21 '12 at 01:08

4 Answers4

39

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>
Smi
  • 13,850
  • 9
  • 56
  • 64
22

This is the minimal HTML5-valid document:

<!doctype html><title> </title>
Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
  • 2
    According to [MDN](https://developer.mozilla.org/en/docs/Web/HTML/Element/head) even the title tag may be omitted "if the element is an ` – Mark Amery Jan 14 '14 at 11:16
  • 8
    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.) – Jukka K. Korpela Jul 11 '14 at 05:17
  • 7
    According to [Validator.nu](https://html5.validator.nu/) and the [W3C validator](https://validator.w3.org/#validate_by_input) the `` tag with a whitespace is regarded as empty. There has to be some non-space character inside. – Sebastian Simon Nov 29 '15 at 13:24
12

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

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • 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. `` 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. – Alohci Sep 02 '16 at 20:05
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.

Alohci
  • 78,296
  • 16
  • 112
  • 156