How to Validate HTML and Fix Errors That Actually Affect a Page
Separate parser recovery from conformance errors and prioritize broken nesting, duplicate IDs, forms and accessibility.
How to Distinguish Parser Recovery From Conformance Errors
The WHATWG HTML specification defines two distinct categories of problems that validators report. Understanding the difference determines which errors you fix and which you ignore.
Parser recovery errors are mistakes the browser's tokenizer and tree-construction algorithm absorb automatically. Examples include unclosed <p> tags, omitted <tbody> elements, or stray </div> tags. The browser builds the correct DOM despite these issues. The validator flags them, but the rendered page works as intended.
Conformance errors are violations the parser cannot fix, or that produce a DOM different from what you intended. These cause broken layout, non-functional forms, or incorrect assistive technology output. The WHATWG parser specification §12.2.6.4 describes how the tree-construction algorithm handles errors, but explicitly notes that certain violations produce implementation-dependent results.
When reviewing validator output, ignore errors about optional end tags, implied elements, or unknown attributes (unless misspelled or proprietary). Investigate errors about missing required attributes, duplicate IDs, misnested elements that change scope, and structural violations inside <form>, <table>, or interactive elements.
Fix Crossing Tag Boundaries That Collapse the DOM
Crossing tags occur when closing tags appear in the wrong order relative to their opening tags:
<b>Bold <i>and italic</b> oh no</i>
The parser must close both open elements at the first closing tag it encounters. The resulting DOM:
<b>
"Bold "
<i>"and italic"</i>
</b>
"oh no"
The </i> becomes orphaned and has no effect. Content after the crossing point loses intended formatting. Validators report "Stray end tag" or "End tag for b seen, but there were open elements."
Observable check: Open browser developer tools, inspect the element, and compare the DOM tree to your intended structure. If child elements appear at unexpected nesting levels, crossing tags are the likely cause.
Fix: Reorder tags so they nest properly:
<b>Bold <i>and italic</i> oh no</b>
Hard limit: Crossing tags inside interactive elements produce undefined behavior. A <button> containing a crossing <a> tag may become unclickable in some user agents. If a button or link does not respond to clicks or keyboard activation, inspect the DOM nesting before investigating JavaScript event handlers.
Remove Duplicate IDs That Break Labels, Links, and Scripts
Duplicate id values create measurable failures across multiple browser subsystems:
<label for="email">associates with only the first element bearingid="email". Subsequent fields with the same ID remain unlabeled.- Fragment navigation (
#section2) scrolls to the first matching element, ignoring later ones. document.getElementById()returns only the first match by specification §3.2.6.
Validators report "Duplicate ID x." The fix is mechanical: rename each duplicate so every id is unique within the document. For machine-generated HTML, a script that appends a counter to repeated IDs works reliably.
Scope exception: Duplicate IDs inside different shadow DOM trees do not conflict. Each shadow root maintains its own identifier scope. Outside shadow roots, duplicates are always harmful.
Observable check: Click a <label> associated with a duplicated ID. If focus moves to the wrong field, or no field receives focus, duplicates are present.
Validate Form Controls by Checking Required Attributes
Form validation errors that affect data submission include:
- Missing
nameon<input>,<select>, or<textarea>: Controls without anameattribute are not included in the form data set during submission. The value is lost silently. - Missing
typeon<button>: The default type issubmitper §4.10.7. A button intended to trigger JavaScript will instead submit the enclosing form. - Missing
foron<label>: The label has no programmatic association with any control. Clicking the label does not focus the intended input.
Observable check for missing name: Submit the form and examine the network request payload or FormData object. Fields without name attributes will be absent.
Observable check for button type: Click a <button> without an explicit type inside a <form>. If the page navigates or reloads, the button is acting as type="submit". Add type="button" to prevent form submission.
Validator limitation: A form with action="#" passes validation but fails in some email clients and offline HTML renderers. Use action="" or omit the attribute entirely to submit to the current document URL.
Repair Accessibility Errors With Observable Impact
Validators flag several accessibility-related conformance errors that affect assistive technology users:
- Missing
alton<img>: Screen readers fall back to announcing the filename or the word "image." Browsers display nothing for the broken image context. Addalt=""for decorative images or a descriptive text alternative for meaningful ones. - Missing
langon<html>: Voice synthesis engines use language metadata for pronunciation rules. Without it, screen readers may apply incorrect phonetics. Addlang="en"or the appropriate language code. - Empty headings: An
##element passes validation but creates a heading entry in the accessibility tree with no accessible name. Screen reader users navigating by heading hear nothing. Either add content or remove the heading element.
Observable check for empty headings: Open the browser's accessibility inspector (Chrome DevTools > Elements > Accessibility pane). Empty headings show no computed accessible name.
Hard limit on validator scope: Validators check syntax, not semantic appropriateness. A page can pass validation while containing nonsensical ARIA roles, illogical heading hierarchies, or insufficient color contrast. Use a dedicated accessibility testing tool alongside validation.
Verify File Structure and Encoding When Validation Passes but Rendering Fails
Two conformance errors cause failures that validators may not fully diagnose:
Missing or incorrect <!DOCTYPE html>: Without a doctype, browsers enter quirks mode, which changes the CSS box model. Width and height calculations differ from standards mode. Validators report "No DOCTYPE found" or "DOCTYPE is not a valid HTML5 doctype." Always include <!DOCTYPE html> as the first line of the document.
Mismatched character encoding: When the declared encoding (<meta charset="utf-8">) does not match the actual byte encoding of the file, non-ASCII characters become garbled. The validator parses the bytes it receives and may not detect the mismatch if the declaration is syntactically correct.
Observable check: Open the page in a browser and visually inspect characters like emoji, accented letters, or currency symbols. If they display as `or garbled sequences, the encoding is mismatched. Verify the actual encoding withfile -i` (Unix) or by reopening the file with explicit encoding in a text editor.
Diagnostic Sequence for Prioritizing Fixes
When a validator reports errors on a page that appears to render correctly, follow this order:
- Fix duplicate IDs. Search for
id="([^"]+)"and check for repeats. Each duplicate can break label associations, fragment navigation, and script selectors. - Add missing
nameattributes to form controls. Every<input>,<select>, and<textarea>intended to submit data requires aname. - Add explicit
typeto<button>elements. Buttons that should not submit forms needtype="button". - Correct crossing tags. If closing tags appear in the wrong order, restructure the markup so elements nest properly.
- Add
altattributes to images. Usealt=""for decorative images; provide descriptions for content images. - Verify doctype and charset. Confirm
<!DOCTYPE html>is present and the file is saved as UTF-8. - Re-validate. After these fixes, remaining errors are typically parser-recovery noise. Address them only if they correlate with a specific observable problem.
Converter Failures Caused by Validation Errors
HTML-to-PDF or HTML-to-DOCX converters often use strict parsers that cannot recover from errors browsers tolerate:
- Crossing tags cause strict XML-based parsers (used by WeasyPrint and similar tools) to halt with a fatal error.
- Duplicate IDs become duplicate document bookmarks in DOCX output, producing corrupted navigation structures.
- Missing
altattributes may cause PDF accessibility checkers to flag the output as non-compliant with PDF/UA standards.
If a conversion tool fails or produces corrupted output, fix the error categories above before investigating converter-specific configuration. The converter cannot infer intended structure from broken markup.