Hacker News new | ask | show | jobs
by debugnik 1396 days ago
> That is a _VERY_ minor nit

Well, the title is precisely the only mandatory element in a valid HTML5 document, even if forgetting it seems harmless.

1 comments

Long time ago I read/skimmed the specification, but I think the DOCTYPE preamble is the only _required_ element in a HTML5 document. The specification allows you to omit <head/> if it's empty, and if that's allowed, then it should be allowed to not having any <title/> elements as well.

Edit with details from https://www.w3.org/TR/2014/REC-html5-20141028/document-metad...

> Note: The title element is a required child in most situations, but when a higher-level protocol provides title information, e.g. in the Subject line of an e-mail when HTML is used as an e-mail authoring format, the title element can be omitted.

From https://www.w3.org/TR/2014/REC-html5-20141028/document-metad...

> If it's reasonable for the Document to have no title, then the title element is probably not required. See the head element's content model for a description of when the element is required.

So strictly speaking, if it's meant to be used as a traditional web page, you should really have it (obviously), but it's not strictly required.

It’s not that you’re allowed to omit the head element (you’re not, and you can’t), but that its start and end tags are optional. Same with the html and body elements. (These remarks apply to HTML syntax only; in XML syntax, which is certainly still a thing, you can (if you care not for validity) omit whatever elements you choose to, only needing some root element.)

As far as sources are concerned, the HTML spec is maintained by WHATWG, not W3C. The relevant citations start at https://html.spec.whatwg.org/multipage/semantics.html#docume....

The normative reference on the necessity of <title> is in the content model for the head element:

> If the document is an `iframe srcdoc` document or if title information is available from a higher-level protocol: Zero or more elements of metadata content, of which no more than one is a `title` element and no more than one is a `base` element.

> Otherwise: One or more elements of metadata content, of which exactly one is a `title` element and no more than one is a `base` element.

For the rest, you’re correct: the DOCTYPE is the only always-mandatory thing in a valid HTML document.

> It’s not that you’re allowed to omit the head element (you’re not, and you can’t), but that its start and end tags are optional.

That’s like calling a cheese sandwich without any cheese a cheese sandwich.

It's not a sandwich recipe. The bread doesn't matter. The cheese does.
If you omit writing <head> and </head> in HTML syntax, there will still be a head tag in the parsed result.

(This is different from XML syntax, where omitting the start and end tags means omitting the element as a whole so that there will be no head tag in the parsed result.)

I think they are saying HTML implies the <head> for you - the <title> is still required.
convo was about HTML 5 specifically (the W3C version). I've never heard HTML WHATWG called HTML 5.
> I've never heard HTML WHATWG called HTML 5.

Directly from the specification:

> 1.2 Is this HTML5?

> In short: Yes.

https://html.spec.whatwg.org/multipage/introduction.html#is-...?

From the head element section mentioned:

> If the document is an iframe srcdoc document or if title information is available from a higher-level protocol: Zero or more elements of metadata content, of which no more than one is a title element […].

> Otherwise: One or more elements of metadata content, of which exactly one is a title element […].

So it is required, not just suggested, for a web page, but not for all kinds of html documents; TIL. The parser still tries to parse head contents before body contents even if you omit the head tags, so a doctype followed by title is the shortest valid full page.

I didn't mention the doctype because I believe it isn't strictly speaking an element, just a preamble, but you're right, it's required as well.

> I didn't mention the doctype because I believe it isn't strictly speaking an element, just a preamble, but you're right, it's required as well.

Funny thing, when I read “the only mandatory element in a valid HTML5 document”, I interpreted “element” in its generic English sense (piece, thing) rather than its HTML sense (node of type element, as distinct from text/comment/doctype/other-types-only-found-in-XML-syntax nodes).

True, I guess I would too if I were a native English speaker. Even when the words are almost identical in my language, I read them as programming jargon before plain English.