← Back to Chapters

HTML Validation

? HTML Validation

⚡ Quick Overview

HTML validation checks your markup against web standards. Valid HTML is more portable across browsers, improves accessibility, and reduces layout quirks. Validators and linters help you catch structural and semantic issues early so you can ship stable, predictable pages.

? Key Concepts

✅ What to validate

  • Syntax & structure: required elements (like <title>), proper nesting, and closed tags.
  • Attributes: allowed attributes, required ones (e.g., alt on <img>), and unique id values.
  • Accessibility hints: ARIA usage, landmark roles, and correct label associations.
  • Links & media: broken links (via link checker) and media with missing descriptions.

? Tools

? Minimal Valid Template

A minimal, valid HTML5 document includes a doctype, root <html>, a <head> section with metadata, and a <body> for content:

? View Code Example
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Valid HTML Page</title>
</head>
<body>
  <h1>Welcome</h1>
  <p>This is a valid HTML document.</p>
</body>
</html>

? Using Real Validators

  1. Open the W3C Validator.
  2. Choose “Validate by URL” (live site) or “Validate by Direct Input/Upload”.
  3. Fix errors from top to bottom. Many later errors are side effects and will disappear once the first structural problems are fixed.

? Tips & Best Practices

  • Add lang on <html> and a unique, descriptive <title> on every page.
  • Provide alt text for images; use empty alt="" only for purely decorative images.
  • Keep all id values unique. Duplicate IDs confuse CSS selectors, anchors, and label/for associations.
  • Ensure valid nesting (e.g., <li> inside <ul>/<ol>, not directly in <div>).
  • Close all tags properly and avoid overlapping tags that break the DOM structure.
  • Don’t nest interactive elements (e.g., no <button> inside <a>).
  • Prefer modern, non-obsolete elements/attributes and replace deprecated markup with semantic HTML + CSS.
  • Integrate validators into your editor/CI so issues are caught continuously instead of only before release.

? Try It Yourself

  • Run one of your pages through the W3C or Nu validator and fix at least one structural error.
  • Search your code for id="... and ensure each value is unique across the whole document.
  • Add missing alt text to images; mark decorative images with empty alt="".
  • Remove any deprecated or presentational attributes (like bgcolor) and move styling to CSS.

? Summary

  • HTML validation keeps your markup consistent with web standards and improves cross-browser reliability.
  • Validators check structure, attributes, accessibility hints, and more—beyond simple syntax.
  • Use official tools like the W3C Validator and Nu HTML Checker, plus editor/CI linting for everyday work.
  • Fixing a few key issues (lang, title, alt, unique IDs, proper nesting) dramatically improves overall quality.