HTML Document Syntax, Parsing Rules, and Conformance
HTML versus XML syntax, required document structure, parser rules, content models, wire formats, and how to check conformance with validators.
This page summarizes the WHATWG HTML Living Standard for authors and conformance testers: how HTML relates to “HTML5,” when to use HTML syntax versus XML, what a conforming document contains, how parsing differs from SGML and XML, where elements may appear, and how to validate markup without relying on browser error tolerance alone.
HTML Living Standard and scope: is HTML5 still a thing?
Section titled “HTML Living Standard and scope: is HTML5 still a thing?”The specification defines a large part of the web platform—markup semantics, scripting APIs, and related rules—alongside CSS, JavaScript, HTTP, and other standards. People still ask whether “HTML5” applies to this document.
In short: Yes.
In more length: the term “HTML5” is widely used as a buzzword to refer to modern web technologies, many of which (though by no means all) are developed at the WHATWG.
— WHATWG, 1 Introduction
Its stated scope is semantic markup and scripting APIs for accessible pages from static documents to dynamic applications—not a full operating-system or presentation specification (CSS handles most presentation).
This specification is limited to providing a semantic-level markup language and associated semantic-level scripting APIs for authoring accessible pages on the web ranging from static documents to dynamic applications.
— WHATWG, 1 Introduction
The Edition for Web Developers covers the same topics with less implementer detail; normative edge cases below refer to the multipage edition unless noted.
HTML syntax vs XML syntax: MIME type and error handling
Section titled “HTML syntax vs XML syntax: MIME type and error handling”Authors choose a syntax (HTML or XML) and serve it with a matching MIME type. HTML syntax is recommended for most authors; bytes labeled text/html are processed with HTML rules. Bytes served as XML (for example application/xhtml+xml, registered in IANA considerations) use an XML processor. This spec does not use the term “XHTML,” though XML syntax with the HTML namespace is common in the wild.
Authors are reminded that the processing for XML and HTML differs; in particular, even minor syntax errors will prevent a document labeled as XML from being rendered fully, whereas they would be ignored in the HTML syntax.
— WHATWG, 1 Introduction
The in-memory result is a DOM, but HTML syntax, XML syntax, and the DOM cannot always represent the same content (namespaces, noscript, and some comment forms differ). Treat “it looks fine in Chrome” as weak evidence for XML-serialized documents.
| Authoring choice | Typical MIME type | Syntax errors |
|---|---|---|
| HTML syntax | text/html |
Often still parsed; conformance still matters |
| XML syntax | application/xhtml+xml (or other XML MIME) |
Minor errors can block full render |
Required document structure: DOCTYPE, html, and head contents
Section titled “Required document structure: DOCTYPE, html, and head contents”Writing HTML documents applies to documents and to tools that generate them. A document must contain parts in order: optional byte-order mark, comments and whitespace, a DOCTYPE, then eventually the document element—an html element. Element and attribute names are ASCII case-insensitive in HTML syntax. Extra author-facing rules include implied table structure (for example a table must not directly contain tr rows) and optional newlines after pre or textarea start tags.
Documents must consist of the following parts, in the given order:
Optionally, a single U+FEFF BYTE ORDER MARK (BOM) character.
Any number of comments and ASCII whitespace.
A DOCTYPE.
…
The document element, in the form of an html element.
— WHATWG, 13 The HTML syntax
For metadata in head, Google’s valid page metadata guidance aligns with the standard’s element set:
The
<head>element must only contain the following valid elements (and no other invalid elements), as per the HTML standard
Valid children include title, meta, link, script, style, base, noscript, and template—not arbitrary div or other flow content in head.
HTML parsing rules: why validators and browsers diverged historically
Section titled “HTML parsing rules: why validators and browsers diverged historically”For resources labeled text/html, user agents must use the HTML parsing algorithm in Parsing HTML documents—not SGML rules from HTML 4.
While the HTML syntax described in this specification bears a close resemblance to SGML and XML, it is a separate language with its own parsing rules.
… This version of HTML thus returns to a non-SGML basis.
— WHATWG, 13.2 Parsing HTML documents
Parsing is incremental: when the parser hits a script, it may pause while the script runs. That interaction explains many “works in the browser but fails validation” cases: invalid input may still get a defined tree, while conformance describes what producers should emit for predictable interoperability.
Non-normative examples in the parsing section show active formatting elements (nested b tags) being reconstructed so later paragraphs inherit formatting without explicit tags in the source—another reason not to depend on serialized HTML matching your mental model of the DOM.
Content models and transparent elements: where can div, a, and script go?
Section titled “Content models and transparent elements: where can div, a, and script go?”Each element in Semantics, structure, and APIs of HTML documents has categories (metadata, flow, phrasing, embedded, and others), contexts where it may appear, and a content model for its children. Whitespace between elements is ignored for content-model checking; scripts may create orphan nodes that are not yet inserted where the model would allow.
Metadata content in head includes base, link, meta, noscript, script, style, template, and title. Flow and phrasing govern most body markup; interactive and embedded categories constrain controls and replaced content.
Some elements use a transparent content model: allowed children depend on the parent. With no parent context, transparent elements effectively allow flow content—so phrasing such as text remains valid inside them when the chain of parents permits it.
Thus, “Apples” is allowed, as text is phrasing content.
— WHATWG, 3 Semantics, structure, and APIs of HTML documents
Full per-element rules live in the element index; validators enforce content models even when browsers build a tree anyway.
Script timing: why an img load handler never ran
Section titled “Script timing: why an img load handler never ran”Scripts run to completion while parsing can pause for them. If you declare an img in markup and attach onload in a later script, the load event may fire during an earlier parser pause and be missed.
However, if the author first created the img element and then in a separate script added the event listeners, there’s a chance that the load event would be fired in between, leading it to be missed.
— WHATWG, 1 Introduction
Reliable patterns use event handler content attributes on the element, or create the element and attach listeners in the same script execution. Scripting also notes servers should serve JavaScript as text/javascript; a type value whose MIME essence does not match JavaScript will not run. Prefer omitting type for classic scripts rather than obsolete values like language or parameterized text/javascript; charset=utf-8 strings.
Conformance, validators, and obsolete presentational markup
Section titled “Conformance, validators, and obsolete presentational markup”Authors should use conformance checkers (validators); the WHATWG maintains a tool list at https://whatwg.org/validator/.
Authors are encouraged to make use of conformance checkers (also known as validators) to catch common mistakes. The WHATWG maintains a list of such tools at: https://whatwg.org/validator/
— WHATWG, 1 Introduction
Invalid documents may still be parsed, but conformance reduces surprises across browsers, archives, and non-browser consumers.
Presentational markup was largely removed from HTML; use CSS instead of legacy alignment and font attributes.
For those reasons, presentational markup has been removed from HTML in this version.
— WHATWG, 1 Introduction
Obsolete features still constrain some legacy attributes if present—for example img border must be "0" if set, and style type should be text/css. Obsolete features (dev) lists features that trigger warnings in checkers (“valid HTML with warnings”) versus hard errors, including many presentational attributes checkers discourage in production.
Form and datetime wire formats: locale vs submission
Section titled “Form and datetime wire formats: locale vs submission”In markup and submission, date and time strings use fixed, locale-independent formats so servers can parse them reliably.
The format used “on the wire”, i.e., in HTML markup and in form submissions, is intended to be computer-readable and consistent irrespective of the user’s locale. Dates, for instance, are always written in the format “YYYY-MM-DD”, as in “2003-02-01”.
— WHATWG, 4.10 Forms
The control UI may show locale-specific labels, but authors should not assume third-party date libraries match Common microsyntaxes exactly—global datetimes need time zones, years before 1 CE are not representable, and duration syntax omits months. See The input element for localization examples (ambiguous dd/mm display vs stable wire values).
Referrer policy: noreferrer, attribute, meta, and header order
Section titled “Referrer policy: noreferrer, attribute, meta, and header order”When multiple referrer signals exist, URLs defines processing order:
In general, the order in which these signals are processed are:
First, the presence of a noreferrer link type;
Then, the value of a referrer policy attribute;
Then, the presence of any meta element with name attribute set to referrer.
Finally, theReferrer-PolicyHTTP header.
— WHATWG, 2.4 URLs
Link type noreferrer wins over a referrerpolicy attribute on the same link; page-wide meta and HTTP headers apply later in the cascade.
Content attributes, IDL reflection, and document.all
Section titled “Content attributes, IDL reflection, and document.all”Content attributes appear in markup; IDL attributes on DOM interfaces reflect them via algorithms described in Common DOM interfaces. Script reads and writes mostly go through IDL properties; validators and serializers use content attributes as the authoring source of truth unless the spec defines associated elements.
Legacy document.all implements HTMLAllCollection with special JavaScript behavior for compatibility:
The typeof operator in JavaScript returns the string “undefined” when applied to objects implementing the HTMLAllCollection interface.
— WHATWG, 2.6 Common DOM interfaces
Do not use typeof document.all for feature detection; use explicit feature tests instead.
Default rendering and the hidden attribute
Section titled “Default rendering and the hidden attribute”Rendering gives suggestions, not mandatory layout for every user agent:
User agents are not required to present HTML documents in any particular way. However, this section provides a set of suggestions for rendering HTML documents…
— WHATWG, 15 Rendering
UAs are encouraged to map HTML presentational hints to CSS and to hide head, meta, script, and similar metadata from visual layout. The hidden attribute normally suppresses rendering (with exceptions noted in the rendering section); noscript is hidden when scripting is enabled. Inspecting the live tree in Chrome DevTools DOM tools shows the DOM after parsing and scripts, which may differ from View Source—useful when debugging reflection and parser fixes.
CSS selectors and HTML case rules
Section titled “CSS selectors and HTML case rules”In HTML documents, CSS compares HTML element type selectors and attribute names in ASCII lowercase; some attribute values (such as type and rel) are compared case-insensitively. Details are in Other embedded content semantics. Quirks mode further changes class and ID selector matching—another reason to use standards mode and valid DOCTYPE for predictable styling.
How to check this on your site
Section titled “How to check this on your site”-
Run your page through a conformance checker listed at the WHATWG validator page and fix errors; review warnings for obsolete presentational attributes per Obsolete features.
-
Confirm the
Content-Typematches your syntax:text/htmlfor HTML syntax, or an XML MIME type such asapplication/xhtml+xmlonly for XML serialization, as described in 1 Introduction and IANA considerations. -
In Chrome DevTools, compare the Elements panel DOM to your source after scripts run; verify
headcontains only allowed metadata elements per Google valid metadata. -
For forms, submit test data and confirm dates and times match wire formats in 4.10 Forms, not only the localized control display.
-
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you are asking about.