Skip to content
Tutorials Center
My learning

Topics

Lesson 109 of 122 ยท Accessibility and Best Practices

Validating Your HTML

Check your markup with the W3C Nu HTML Checker and editor linters, fix the most common validation errors, and see how browsers quietly repair broken HTML.

Browsers almost never refuse to show a page. Forget a closing tag, nest elements the wrong way or repeat an id, and the page still appears, more or less as you hoped. That forgiveness is great for visitors, but it hides your mistakes from you. Some of those mistakes break styling, scripts or accessibility in ways that only show up later.

Validation means checking your markup against the rules of the HTML standard. A validator reads your page like a strict proofreader and lists every place where it breaks those rules. This lesson shows you how to run one, how to read its messages and which errors you will meet most often.

Why validate at all?

  • Catch real bugs early. A duplicate id breaks a <label>, a fragment link and getElementById(). An unclosed <div> swallows the rest of your layout.
  • Get predictable results. Valid markup produces the same DOM in every browser, so CSS and JavaScript behave the same way.
  • Help assistive technology. Screen readers depend on correct structure: list items inside lists, labels pointing at real fields, headings that are really headings.
  • Learn the language. Validator messages are a fast way to learn the rules you did not know.

Valid is not the same as good. A page can pass validation and still have meaningless alt text, poor contrast or confusing headings. Validation is one check among several, alongside the accessibility checks you learned earlier.

The W3C Nu HTML Checker

The standard tool is the Nu HTML Checker, run by the W3C at validator.w3.org/nu. It checks against the current HTML Living Standard and offers three ways in:

  1. Address: paste the URL of a page that is online.
  2. File upload: choose an .html file from your computer.
  3. Text input: paste markup straight into a box. Ideal for checking a snippet or a page that only exists on your machine.

Each message includes a type, a location (line and column) and an explanation, plus an excerpt of the source with the problem highlighted.

  • Error: the markup breaks a rule of the standard. Fix these.
  • Warning: the markup is allowed but probably not what you meant, or is unnecessary (for example, role="navigation" on a <nav>).
  • Info: general notes about how the document was checked.

Fix from the top. One early mistake, like a missing closing tag, can cause a chain of later errors. Fix the first few messages, validate again and watch the list shrink.

The same checker is available offline as a Java program and as the vnu-jar package on npm, which is handy for checking every page of a site automatically in a build pipeline.

The most common errors

These are the problems you are most likely to see, and how to fix each one.

Problem Example Fix
Missing doctype file starts with <html> Add <!doctype html> as the very first line.
Missing or empty title no <title> in the head Every document needs a non-empty <title>.
Duplicate id two elements with id="email" Make every id unique on the page.
Image without alt <img src="logo.svg"> Add meaningful alt, or alt="" if decorative.
Stray end tag a </div> with no matching opening tag Remove it, or add the missing opening tag.
Unclosed element <section> never closed Close it where the section ends.
Block inside <p> <p><div>โ€ฆ</div></p> Close the paragraph first, or use a <div> as the wrapper.
Wrong parent <li> directly in a <div> List items belong in <ul>, <ol> or <menu>.
Interactive inside interactive <a> inside a <button>, or a link inside a link Use one interactive element, not nested ones.
Unknown or misplaced attribute <div href="...">, <p src="..."> Use the attribute only where the standard allows it.
Obsolete markup <center>, <font>, align="center" Replace with CSS (see Deprecated and Obsolete Features).

A few more rules trip people up:

  • Custom attributes need the data- prefix. <div tooltip="..."> is an error; <div data-tooltip="..."> is valid. See Custom Data Attributes.
  • Some elements have required attributes, such as src on <img>.
  • Element content models are strict: <table> children must be table parts, <select> contains options, a <dl> holds <dt>/<dd> pairs.

How browsers recover from broken HTML

If browsers are so forgiving, what do they actually do with broken markup? Before HTML5, every browser guessed in its own way, and the same mistake could produce different pages. Today the HTML standard defines a precise parsing algorithm, including exactly how to handle errors. Every modern browser follows it, so broken HTML is repaired in the same way everywhere.

The repairs can still surprise you. Run the example below and compare the source with the repaired DOM that the script prints.

Watch the parser repair markup

Look closely at the output:

  • The <div> cannot live inside a <p>, so the paragraph is closed early. The leftover </p> then creates a new, empty paragraph.
  • The badly nested <b> and <i> are split and re-opened so that every element is properly nested.
  • The text inside the table is moved out in front of the table ("foster parenting"), and a <tbody> is inserted automatically.

None of these repairs are what the author intended. Your CSS and scripts work on the repaired DOM, not on your source, which is why validation errors often explain "impossible" bugs. The DevTools lesson shows how to inspect this live DOM directly.

A note on WCAG. Older versions of WCAG had a success criterion called Parsing (4.1.1) that required valid markup. WCAG 2.2 marks it as obsolete, because browsers and assistive technology now handle parsing consistently. Problems that still matter, such as duplicate ids used in labels or missing names, are covered by other criteria.

Checking in your editor

You do not have to wait for an online check. Most editors can flag problems as you type:

  • VS Code ships with HTML language support: tag completion, matching tags and basic hints. Extensions add full linting.
  • HTMLHint and html-validate are linters you can run in the editor, on the command line or in continuous integration. Both can be configured with rules for your project, such as requiring alt on images or forbidding inline styles.
  • Browser DevTools report some problems in the Console or the Issues panel, such as form fields without labels or duplicate ids in forms.
  • Formatters like Prettier make unclosed or misnested tags obvious, because the indentation suddenly looks wrong.

Here is a tiny "validator" of your own that checks a page for three common issues. Try adding more problems to the markup and running it again.

A tiny markup checker

A real validator checks hundreds of rules like these, but the idea is the same.

A validation routine

Make validation a habit, not an afterthought:

  1. While writing: let your editor's linter highlight problems as you type.
  2. Before publishing a page: paste it into the Nu HTML Checker and fix every error.
  3. For a whole site: run vnu-jar or a linter automatically on every change, so errors never reach production.
  4. Look at warnings too. They often point to redundant or confusing markup.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a page with invalid HTML usually still display?

    The parsing algorithm includes error handling. Every modern browser repairs broken markup in the same, specified way.
  2. 2. You write <p>Intro <div>Box</div></p>. What does the browser build?

    A div cannot appear inside a paragraph, so the paragraph is closed automatically. The leftover end tag produces an empty paragraph.
  3. 3. Which attribute is valid on a <div>?

    Custom attributes must start with data-. The href attribute belongs on links and a few other elements, not on a div.
  4. 4. The validator reports 40 errors on a page you just wrote. What is the most efficient approach?

    One early mistake can trigger many follow-on errors. Fixing from the top often makes dozens of messages disappear at once.

Summary

  • Validation checks your markup against the HTML standard and catches mistakes browsers hide.
  • The W3C Nu HTML Checker accepts a URL, a file or pasted text; vnu-jar runs the same checks locally.
  • Common errors include duplicate ids, missing alt, stray or missing tags, blocks inside paragraphs and invented attributes.
  • Browsers repair broken HTML using a standard algorithm, but the result is often not what you meant.
  • Editor linters such as HTMLHint and html-validate catch problems while you type.
  • Valid HTML is a foundation, not a guarantee of accessibility or quality.

Next up: HTML vs XHTML.