HTML Validation
In this page:
Why HTML Validation Matters
Because browsers try to recover gracefully from broken markup, invalid HTML can still render an apparently normal page, hiding structural problems that may only surface in a different browser, a screen reader, or a future update.
Note: Run new pages through a validator before publishing, the same way you would run tests before shipping code.
Warning: A page that "looks fine" is not proof that its HTML is valid; visual correctness and markup correctness are two separate things.
Example: Why HTML Validation Matters
<p>Unclosed paragraph
<div>Nested incorrectly</div>
Using the W3C Markup Validator
The W3C Markup Validator, available at validator.w3.org, can check a page three ways: by live URL, by uploading an HTML file, or by pasting raw code directly into a text box.
Note: Use the "direct input" option while still drafting a page locally, before it has a public URL to check.
Warning: Validating a URL only checks the HTML actually delivered to the browser, not any markup generated afterward by JavaScript.
Example: Using the W3C Markup Validator
<!-- Paste this code into validator.w3.org to check it -->
<!DOCTYPE html>
<html>
</html>
Common Validation Errors
Most validation errors fall into a few repeating categories: unclosed tags, elements nested in the wrong order, duplicate IDs, and attributes that do not belong on a particular element.
Note: Fix errors from the top of the validator's report down, since one early unclosed tag can trigger a cascade of unrelated-looking errors below it.
Warning: Duplicate id values are a common but easy-to-miss error, since browsers do not warn about them even though they break id-based CSS and JavaScript targeting.
Example: Common Validation Errors
<div id="box">First</div>
<div id="box">Duplicate id causes a validation error</div>
Doctype and Document Structure Errors
A missing or malformed doctype puts browsers into "quirks mode", an old compatibility mode that changes how CSS and layout are calculated, and it is one of the very first things a validator checks.
Note: Always start every HTML document with the modern, simple doctype: <!DOCTYPE html>.
Warning: An old or misspelled doctype can silently trigger quirks mode, causing layout differences that have nothing to do with your CSS itself.
Example: Doctype and Document Structure Errors
<!DOCTYPE html>
<html lang="en">
</html>
Validating for Accessibility
Beyond pure syntax, validators and dedicated accessibility checkers flag issues like missing alt text, unlabeled form fields, and poor heading order, all of which affect how well a page works with screen readers.
Note: Pair the W3C Markup Validator with a dedicated accessibility checker for a fuller picture, since not every accessibility issue is a strict HTML syntax error.
Warning: A page can be perfectly valid HTML and still be inaccessible, since accessibility depends on more than syntax alone, like meaningful alt text and logical heading order.
Example: Validating for Accessibility
<img src="photo.jpg">
<!-- Missing alt attribute flagged by accessibility checkers -->
- Assuming a page is correct simply because it looks right in the browser, without ever running it through a validator.
- Leaving tags unclosed or improperly nested, which validators flag as structural errors.
- Ignoring validator warnings about missing alt text or form labels, treating them as optional rather than accessibility requirements.
- HTML validation checks markup against the official language rules, independent of how forgiving browsers are.
- The W3C Markup Validator can check a live URL, an uploaded file, or pasted code directly.
- Fixing validation errors early prevents inconsistent rendering and accessibility problems across browsers and assistive technology.
Validation is a build-time and authoring practice rather than a browser feature, so it applies the same regardless of which browser eventually renders the page.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep