HTML Validation

Difficulty: Intermediate

HTML validation is the process of checking whether your HTML markup conforms to the standards defined by the W3C (World Wide Web Consortium). Valid HTML ensures consistent rendering across browsers, improves accessibility, helps search engines parse your content correctly, and reduces unexpected behavior caused by browser error-correction algorithms. The W3C Markup Validation Service (validator.w3.org) is the official tool for checking HTML validity, and understanding common validation errors is essential for writing professional-quality markup.

The DOCTYPE declaration is the very first line of an HTML document and tells the browser which version of HTML the page uses. In HTML5, the declaration is simply <!DOCTYPE html> -- this triggers standards mode in all browsers. Without a DOCTYPE or with an incorrect one, browsers fall into quirks mode, where they emulate legacy rendering behavior from the late 1990s. In quirks mode, the box model calculates differently (width includes padding and border), table cells inherit font sizes differently, inline elements behave differently, and many CSS features do not work as expected. Always start every HTML document with <!DOCTYPE html>.

Common HTML validation errors fall into several categories. Structural errors include unclosed tags, improperly nested elements (like <p><div></div></p> where a block element is inside an inline element), duplicate IDs on a page, and missing required attributes (like alt on img elements). Content model violations occur when elements are placed where the HTML spec does not allow them, such as block-level elements inside <p> tags or interactive elements nested inside other interactive elements (a link inside a button). Attribute errors include using deprecated attributes, misspelled attribute names, and invalid attribute values.

Beyond the W3C validator, several other validation tools help ensure markup quality. Browser DevTools display parsing errors in the console. The axe accessibility checker and WAVE tool validate accessibility-related markup issues. HTML linters like htmlhint and html-validate can be integrated into your build process for automated checking. ESLint with the jsx-a11y plugin validates JSX accessibility in React projects. Running validation as part of your CI/CD pipeline catches errors before they reach production.

Valid HTML is not just about passing a validator -- it has real impact on user experience and business metrics. Screen readers rely on valid, semantic HTML to convey content structure to visually impaired users. Search engine crawlers may misinterpret or penalize pages with significant markup errors. Invalid HTML can cause layout differences across browsers because each browser has its own error-recovery algorithm. Even seemingly minor issues like unclosed tags can cause cascading layout problems where styles intended for one section leak into others.

Code examples

DOCTYPE and its effect on rendering mode

<!-- HTML5 DOCTYPE (triggers standards mode) -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Standards Mode</title>
</head>
<body>
  <p>This page renders in standards mode.</p>
</body>
</html>

<!-- Missing DOCTYPE (triggers quirks mode!) -->
<html>
<head>
  <title>Quirks Mode</title>
</head>
<body>
  <p>This page renders in quirks mode!</p>
</body>
</html>

<!-- Detect current rendering mode with JavaScript -->
<script>
  console.log('Rendering mode:', document.compatMode);
  // "CSS1Compat" = standards mode
  // "BackCompat" = quirks mode
</script>

The <!DOCTYPE html> declaration must be the very first line of the document (no whitespace or comments before it). It triggers standards mode rendering. document.compatMode returns 'CSS1Compat' for standards mode and 'BackCompat' for quirks mode.

Common validation errors and fixes

<!-- ERROR: Duplicate IDs -->
<div id="header">First header</div>
<div id="header">Second header</div>
<!-- FIX: Use unique IDs -->
<div id="main-header">First header</div>
<div id="sub-header">Second header</div>

<!-- ERROR: Block element inside inline element -->
<span>
  <div>Block inside inline</div>
</span>
<!-- FIX: Use block container or change to inline -->
<div>
  <div>Block inside block</div>
</div>

<!-- ERROR: Missing alt attribute -->
<img src="photo.jpg" />
<!-- FIX: Add descriptive alt text -->
<img src="photo.jpg" alt="Mountain landscape at sunset" />
<!-- FIX: Empty alt for decorative images -->
<img src="decoration.svg" alt="" />

<!-- ERROR: p cannot contain block elements -->
<p>
  Some text
  <div>This breaks the paragraph</div>
</p>
<!-- FIX: Use div as container instead -->
<div>
  <p>Some text</p>
  <div>This is separate</div>
</div>

<!-- ERROR: Obsolete elements -->
<center>Centered text</center>
<font color="red">Red text</font>
<!-- FIX: Use CSS instead -->
<div style="text-align: center;">Centered text</div>
<span style="color: red;">Red text</span>

IDs must be unique per page. Block elements cannot be nested inside inline elements. The alt attribute is required on all img elements (use empty alt='' for decorative images). Elements like <center> and <font> are obsolete and should be replaced with CSS.

Proper document structure for validation

<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Required: character encoding -->
  <meta charset="UTF-8" />

  <!-- Required: viewport for responsive design -->
  <meta name="viewport" content="width=device-width, initial-scale=1" />

  <!-- Required: page title -->
  <title>Properly Structured HTML Page</title>

  <!-- Optional but recommended: description -->
  <meta name="description" content="A valid, well-structured HTML page" />

  <link rel="stylesheet" href="styles.css" />
</head>
<body>
  <header>
    <nav aria-label="Main navigation">
      <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/about">About</a></li>
      </ul>
    </nav>
  </header>

  <main>
    <h1>Page Title</h1>
    <article>
      <h2>Article Title</h2>
      <p>Article content with <strong>bold</strong> text.</p>
      <figure>
        <img src="chart.png" alt="Sales chart showing Q4 growth" width="600" height="400" />
        <figcaption>Figure 1: Q4 sales performance</figcaption>
      </figure>
    </article>
  </main>

  <footer>
    <p>&copy; 2026 Company Name</p>
  </footer>
</body>
</html>

A valid HTML5 document requires: DOCTYPE declaration, html element with lang attribute, head with charset meta and title, and a body. Using semantic elements (header, nav, main, article, footer, figure) improves both validation scores and accessibility. Only one h1 per page is recommended for SEO.

Key points

Concepts covered

W3C Validator, DOCTYPE, HTML Errors, Accessibility Validation, Markup Quality