Course contentsShow
← Web Development
Lesson 1448 of 2,98226. Code Quality ToolsFree lesson

Common HTML Validation Rules

Understand essential rules: unique IDs, proper tag closing, attribute formatting, and doctype requirements for valid HTML.

Common HTML Validation Rules

You'll learn the essential validation rules that keep your HTML clean, accessible, and error-free.

When you validate HTML, you're checking that your markup follows the official standards. Think of it like proofreading an essay—you're catching typos, grammar mistakes, and structural issues before they cause problems. HTMLHint and other validators enforce a set of common rules that catch the most frequent HTML mistakes.

Essential Validation Rules

Unique IDs

Every id attribute in your document must be unique. IDs are like social security numbers—no two elements can share the same one. If you have duplicate IDs, JavaScript selectors and CSS styles may target the wrong element unpredictably.

Invalid:

<div id="header">Main Header</div>
<div id="header">Sidebar Header</div>  <!-- Error: duplicate ID -->

Proper Tag Closing

All tags must be properly closed. Self-closing tags (like <img> or <br>) follow specific syntax, while container tags need matching closing tags. Unclosed tags break document structure and cause rendering issues.

Invalid:

<div>
  <p>Paragraph content
  <span>Forgot to close paragraph!</span>
</div>

Attribute Formatting

Attributes must be quoted and properly formatted. Use double or single quotes consistently, and avoid spaces around the equals sign.

Invalid:

<img src = "photo.jpg" alt='Missing quote>  <!-- Inconsistent quotes, spacing -->

Doctype Declaration

Every HTML document should start with a DOCTYPE declaration. It tells browsers which version of HTML you're using and ensures standards mode rendering.

<!DOCTYPE html>  <!-- Always at the very top -->

Key Takeaway: Valid HTML requires unique IDs, properly closed tags, correctly formatted attributes, and a DOCTYPE declaration—these rules prevent bugs and ensure consistent browser behavior.