HTML validation

Also known as: markup validation

On this page 7

HTML validation is the automated process of checking a document’s markup against the conformance rules of the HTML standard and reporting every place where the code breaks those rules. In HTML work on the Web, a validator, sometimes called a markup validation service, parses the page the same way a conforming parser would and then tests the resulting structure for invalid nesting, unknown elements, missing required attributes and disallowed values.

Validation does not judge design, performance or accessibility directly. It answers a narrower question: does this markup conform to the specification? That question matters because browsers silently repair broken code, and the repair can produce a document tree that differs from what the author intended.

How an HTML validator analyzes markup

A modern validator works in two stages: parsing and conformance checking. First it tokenizes the source with the parsing algorithm defined in the WHATWG HTML Living Standard, the same algorithm browsers use, and records every parse error it encounters, such as a stray end tag or an unquoted attribute value containing illegal characters.

Second, it walks the resulting tree and applies the content model rules of each element. The specification states which children an element may contain, which attributes it accepts and which values those attributes allow. A ul element may contain only li and script-supporting elements, for example, so a div placed directly inside it is a content model violation even though every browser will display it.

The table below summarizes the main categories of problems that a conformance checker reports.

CategoryTypical exampleWhy it matters
Parse errorUnclosed p before a tableBrowser repair alters the tree
Content model errordiv as a direct child of ulBreaks expected structure
Attribute errorUnknown attribute or invalid valueFeature may silently fail
Duplicate IDTwo elements with the same idBreaks labels, anchors and scripts
Obsolete featurecenter or font elementPresentation belongs in CSS
WarningMissing lang on htmlNot an error, but a quality risk
Categories of findings in HTML validation

Errors, warnings and the role of the doctype

Validators distinguish errors, which are definite conformance failures, from warnings and informational messages, which flag likely mistakes or outdated practice. A page with zero errors is conforming, while warnings can be reviewed case by case and sometimes accepted deliberately.

The doctype declaration controls how the document is interpreted. With <!DOCTYPE html> at the top, browsers use standards mode, and the validator applies the current HTML rules. A missing or legacy doctype triggers quirks mode in browsers, which changes layout behavior, and the validator reports the missing doctype as an error. Older validators selected a document type definition (DTD) from the doctype; current checkers ignore DTDs because HTML5 is no longer defined as an SGML application.

Several validation approaches coexist, and each one examines a different aspect of the same document.

ApproachBasisMain use today
Living Standard conformanceWHATWG parsing and content modelsCurrent HTML pages
DTD validationSGML or XML document type definitionsLegacy HTML 4 and XHTML 1.0
XML well-formednessXML syntax rulesXHTML served as XML
Accessibility lintingWCAG-related heuristicsComplements, does not replace, validation
Framework lintingTemplate and component rulesSource code before rendering
Validation approaches and what each one checks

Why browsers tolerate invalid HTML

HTML parsing is deliberately error-tolerant. The standard defines exactly how a browser must recover from every malformed input, so all conforming browsers build the same DOM from the same broken markup. This is why invalid pages often look correct and why validation errors can seem harmless.

The danger lies in the gap between source and result. When a p element is left open before a table, the parser closes it implicitly, so CSS selectors and scripts that assume the table sits inside the paragraph target nothing. Misnested formatting elements trigger the adoption agency algorithm, which clones elements and can duplicate styling unexpectedly. Assistive technologies read the repaired DOM, not the source, so structural errors can produce confusing output for screen reader users.

Validation therefore acts as a specification-level test: it confirms that the tree a browser builds matches the structure the author wrote, which makes behavior predictable across engines and future browser versions.

Using HTML validation in a development workflow

Validation is most effective when it runs on the rendered output, not only on templates. Server-side frameworks, content management systems and client-side components assemble the final markup, and errors often appear only after this assembly, for example duplicate IDs from repeated components or empty attributes produced by missing data.

Teams usually combine a manual check during development with automated validation in continuous integration. The open-source Nu Html Checker, which powers the W3C Markup Validation Service, can run as a local Java service or command-line tool, so build pipelines can validate generated pages without sending content to an external server.

The decision table below matches common situations with an appropriate validation strategy.

SituationRecommended approachReason
Static site buildValidate every generated file in CIErrors are caught before deployment
CMS with editor contentValidate representative templates and sample postsEditors introduce unpredictable markup
Single-page applicationValidate the rendered DOM snapshotSource files do not show final markup
Legacy site migrationFix parse errors first, then content model errorsParse errors have the widest effects
Third-party widgetsIsolate and document unavoidable errorsExternal code is outside team control
Choosing a validation strategy

Limitations and common misconceptions

Validation is a necessary but not sufficient quality check. A fully conforming page can still be inaccessible, slow or semantically poor, because the validator cannot judge whether a heading hierarchy makes sense or whether alternative text describes an image accurately.

  • Valid does not mean accessible. Validation catches some accessibility defects, such as duplicate IDs, but full WCAG conformance requires separate testing.
  • Valid does not mean semantic. A page built entirely from div elements can be valid while ignoring semantic HTML.
  • Search engines do not rank by validity. Severe parse errors can still hide content or metadata from crawlers.
  • Not every error has equal weight. An obsolete attribute is cosmetic, while an unclosed element inside a form can change which fields are submitted.
  • Custom elements are allowed. Names containing a hyphen are valid for web components, but their internal behavior is not checked.

In plain words

HTML validation is like a spelling and grammar check for web page code. The code that builds a page has strict rules about which pieces go where. A validator reads the code and lists every place where those rules are broken.

Browsers are forgiving and will usually show a page even when its code is messy. However, they quietly guess what the author meant, and the guess is sometimes wrong. Validation removes the guesswork, so the page behaves the same way everywhere and is easier to maintain.

A real-world example

A small agency maintains a university department website with about 400 pages generated by a CMS. Users report that a contact form occasionally submits without the email field. Running the rendered pages through a local validator reveals 37 errors per page on average, including a form element that a template closed too early inside a table cell.

Because the parser repaired the broken nesting, the email input ended up outside the form element on some layouts and was never submitted. The team fixes the template, removes duplicate IDs created by a repeated sidebar component and adds a validation step to the deployment pipeline. After the fix, the error count drops to zero, the form works on every page and new template changes are checked automatically before release.