Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

HTML Syntax, Parsing and Conformance

How WHATWG HTML markup becomes a DOM, what conforming documents require, and how browsers recover from syntax errors.

This page summarizes the WHATWG HTML Living Standard on syntax, parsing, and conformance: how source text becomes a DOM, what authors should write, and why invalid markup still yields a defined tree in browsers.

In 2019, the WHATWG and W3C agreed on a single path forward for HTML:

In 2019, the WHATWG and W3C signed an agreement to collaborate on a single version of HTML going forward: this document.

— WHATWG, 1 Introduction

The standard is mostly phrased in terms of DOM trees, not raw tags:

Since DOM trees are used as the way to represent HTML documents when they are processed and presented by implementations (especially interactive implementations like web browsers), this specification is mostly phrased in terms of DOM trees, instead of the markup described above.

— WHATWG, 1 Introduction

HTML document syntax: tags, attributes and the DOM

Section titled “HTML document syntax: tags, attributes and the DOM”

A minimal document uses <!DOCTYPE html>, then html, head, and body. Elements nest in a tree; tags must not overlap (for example, closing </em> before </strong> when strong was opened inside em).

Attributes use name=value. Unquoted values are allowed only when the value contains no ASCII whitespace or the characters " ' \ = < >. Boolean attributes are **present or absent**: an empty value or any value including “false”can still mean “on” where the spec treats presence as true (seedisabled` below).

User agents parse markup into a DOM with node kinds such as Element, Text, and Comment. Extra text nodes from spaces and newlines are normal; whitespace rules (for example before head) can surprise authors comparing source to the DOM in devtools.

Scripting and the Link API (document.links, setAttribute) mutate or expose the same tree the parser built—behavior is defined against the DOM, not the original bytes.

Conformance: syntax errors, content models and author mistakes

Section titled “Conformance: syntax errors, content models and author mistakes”

Conformance splits documents (what authors should produce) from user agents (how browsers must process input). A conformance checker should report syntax and applicable non-syntax requirements; a DTD-only validator is not a full checker:

A simple DTD-based validator only checks for the first class of errors and is therefore not a conforming conformance checker according to this specification.

— WHATWG, 2 Common infrastructure

The spec lists syntax errors with non-normative rationales—patterns that are invalid for authors even when browsers define recovery:

Situation What goes wrong
Misplaced flow content <table><hr> can place hr as a sibling of the table, not inside it
Unclosed formatting An unclosed <i> across paragraphs can grow nested i elements
Character references in URLs ?art&copy may parse as ©; use &amp;copy when you mean a literal &
Tag name typos Unknown tags still tokenize but are conformance errors
Mixed heading levels <h1>…</h2> signals unclear structure

Content model and attribute restrictions catch semantic and parser quirks: disabled="false" is disallowed because presence disables the control, not the string value:

setting the disabled attribute to the value “false” is disallowed, because despite the appearance of meaning that the element is enabled, it in fact means that the element is disabled (what matters for implementations is the presence of the attribute, not its value).

— WHATWG, 1 Introduction

Other common cases: a <form> inside <p> implicitly closes the paragraph; ul children must be li elements; duplicate id values break assumptions for scripts and labels.

Registered as text/html (optional charset=utf-8), HTML is a scripted format with XSS and same-origin risks. Untrusted HTML should not share an origin with sensitive content:

It is critical, therefore, to ensure that any untrusted content that forms part of a site be hosted on a different origin than any sensitive content on that site.

— WHATWG, 17 IANA considerations

Non-normative guidance in the introduction recommends safelist input filters, not blocklists:

When writing filters to validate user input, it is imperative that filters always be safelist-based, allowing known-safe constructs and disallowing all other input. Blocklist-based filters that disallow known-bad inputs and allow everything else are not secure, as not everything that is bad is yet known (for example, because it might be invented in the future).

— WHATWG, 1 Introduction

HTML syntax: element kinds, attributes and optional tags

Section titled “HTML syntax: element kinds, attributes and optional tags”

Authors write HTML syntax with six element kinds: void (e.g. meta, img), template, raw text (script, style), escapable raw text (textarea, title), foreign (MathML/SVG integration), and normal elements.

Void elements use a start tag only. In HTML syntax, self-closing start tags on non-void HTML elements are not real self-closing tags—the trailing / is ignored for HTML elements:

The trailing U+002F (/) in a start tag name can be used only in foreign content to specify self-closing tags. (Self-closing tags don’t exist in HTML.)

— WHATWG, Parsing HTML documents

Foreign content in HTML syntax does not allow arbitrary namespace declarations:

The HTML syntax does not support namespace declarations, even in foreign elements.

— WHATWG, HTML syntax

Attributes may be empty, unquoted, or quoted. Duplicate attribute names on one tag are not allowed. Only a fixed set of foreign attributes (such as xlink:href, xml:lang, xmlns) can appear in HTML syntax.

Optional tags: implied structure without deleting elements

Section titled “Optional tags: implied structure without deleting elements”

Omitting certain start or end tags can still yield the same implied elements as explicit markup—for example an omitted html or body when rules allow. Omitting a start tag does not remove the element from the DOM:

Omitting an element’s start tag in the situations described below does not mean the element is not present; it is implied, but it is still there.

— WHATWG, HTML syntax

A start tag must not be omitted when that element has attributes (you need an explicit <html lang="en">, not a bare omitted root).

HTML parsing: parse errors, encoding and tree construction

Section titled “HTML parsing: parse errors, encoding and tree construction”

Parsing HTML documents defines a tokenizer and tree builder. Parse errors are syntax-level issues; processing rules still apply, and user agents may abort in controlled environments:

Parse errors are only errors with the syntax of HTML.

— WHATWG, Parsing HTML documents

Examples include duplicate-attribute, missing-semicolon-after-character-reference (&notin vs &notin; resolve differently), and DOCTYPE problems that trigger quirks mode.

Encoding uses a multi-step sniff: BOM, transport encoding, an optional prescan of the first bytes (declarations in meta are authoring-limited to the first 1024 bytes), parent-document inheritance for same-origin frames, then defaults often falling back to UTF-8. A conflicting declaration can force a reparse.

Misnested markup: foster parenting and adoption agency

Section titled “Misnested markup: foster parenting and adoption agency”

The parser maintains a stack of open elements and insertion modes (in head, in body, table modes, etc.). Table content placed in the wrong context can be foster parented (inserted before the table in an ancestor). Misnested formatting elements (classic example: links and tables interleaved) invoke the adoption agency algorithm, which can move nodes so nested markup becomes siblings after reparse—serialization and innerHTML round-trips are not guaranteed to be identical trees.

This algorithm’s name, the “adoption agency algorithm”, comes from the way it causes elements to change parents…

— WHATWG, Parsing HTML documents

Speculative parsing may run in parallel for performance around parser-blocking scripts; it must not change the main parser’s state, and fragment parsers do not speculate.

Serializing DOM children back to HTML escapes &, <, >, and quotes in attributes. Reparsing serialized HTML can enable XSS if dangerous combinations (for example closing style then injecting script) are not sanitized—the spec warns that careless innerHTML patterns can enable cross-site scripting attacks.

XML syntax (The XML syntax) is a separate conforming format: stricter rules, different template handling, and no document.write() in the XML parser path.

For migration, see Obsolete features. Elements such as frame, frameset, font, center, and marquee are entirely obsolete; presentational table attributes should give way to CSS. The manifest attribute on html is obsolete in favor of service workers.

Elements in the following list are entirely obsolete, and must not be used by authors

— WHATWG, 16 Obsolete features

Common mistakes when authoring or debugging markup

Section titled “Common mistakes when authoring or debugging markup”
  • Trusting visual nesting in source when the parser repairs tables, paragraphs, or formatting elements.
  • Using & in query strings without &amp; where a named character reference could start.
  • Assuming disabled="false" or checked="false" enables controls.
  • Putting xmlns on SVG in HTML syntax expecting XML-style namespace declarations.
  • Relying on optional tags in invalid structures, then wondering why querySelector does not match “obvious” source.
  • Round-tripping through innerHTML or copy-paste from devtools expecting byte-identical or isomorphic trees.
  • Serving user HTML on the same origin as admin or account pages without sanitization and origin separation.
  1. Run an HTML conformance checker (validator) on representative pages and fix reported syntax and content-model errors; see conformance classes.

  2. In browser devtools, compare Elements (DOM) to View Source on pages that use optional tags, tables, or rich text from a CMS.

  3. Confirm meta charset or http-equiv Content-Type appears within the first 1024 bytes of the document; see encoding sniffing.

  4. Test a misnested snippet (e.g. form inside paragraph) in a throwaway page and note implicit closes before shipping templates.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.