Ad – 728Γ—90
🌐 Beginner

HTML Syntax & Document Structure

HTML syntax is the set of rules that defines how to write valid HTML. Understanding the anatomy of a tag, nesting rules, how attributes work, and what the browser does with whitespace will help you write clean, predictable HTML and debug problems quickly.

⏱️ 14 min read🎯 BeginnerπŸ“… Updated 2026

Anatomy of an HTML Tag

HTML
<!--   OPENING TAG   CONTENT   CLOSING TAG  -->
       <p>          Hello!    </p>

<!-- Tag with an ATTRIBUTE -->
<a href="https://ylearner.org" target="_blank">Learn HTML</a>
<!--  ↑ tag name   ↑ attr name  ↑ attr value    ↑ content  -->

<!-- VOID ELEMENT β€” no closing tag, no content -->
<img src="photo.jpg" alt="A photo">
<br>
<hr>
<input type="text">
<meta charset="UTF-8">
PartExampleRule
Tag namep, h1, divLowercase by convention
Opening tag<p>Required for all elements
Closing tag</p>Required for all non-void elements
Attributehref="url"name="value" inside opening tag only
ContentText or child elementsBetween opening and closing tag
Void element<img>, <br>No content, no closing tag

Nesting Rules

Elements must close in reverse order of opening. Think of it as a stack β€” last opened, first closed.

HTML
<!-- βœ… CORRECT β€” closed in reverse order -->
<p>This is <strong>bold <em>and italic</em></strong> text.</p>

<!-- ❌ WRONG β€” tags overlap (browser will try to fix this
     but results are unpredictable) -->
<p>This is <strong>wrong <em>overlap</strong></em> text.</p>

<!-- ❌ WRONG β€” block element inside inline element -->
<a href="#"><p>Don't put paragraphs inside anchor tags</p></a>

<!-- βœ… CORRECT β€” block inside block, inline inside inline -->
<div>
  <p>A paragraph <strong>with bold</strong> text.</p>
  <p>Another paragraph with a <a href="#">link</a>.</p>
</div>
Ad – 336Γ—280

Whitespace in HTML

HTML collapses multiple spaces and newlines into a single space when rendering. This means you can indent your code freely for readability β€” it won't affect the output.

HTML
<!-- These two produce identical output -->
<p>Hello     World</p>

<p>
  Hello
  World
</p>

<!-- Output in both cases: "Hello World" -->

<!-- To force a non-breaking space: -->
<p>10&nbsp;km</p>

<!-- To preserve whitespace exactly (code blocks): -->
<pre>
  function hello() {
    console.log("hi");
  }
</pre>

Case Sensitivity

HTML tag names and attribute names are case-insensitive β€” <P>, <p>, and <P> all work. However, always use lowercase β€” it is the universal convention, required by XHTML, and what every linter, validator, and team expects.

The DOM Tree

When the browser parses your HTML it builds a Document Object Model (DOM) β€” a tree of nodes representing every element and text in the document. Understanding the DOM helps you understand how CSS targets elements and how JavaScript manipulates the page.

HTML β†’ DOM Tree
<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <h1>Hello</h1>
    <p>World <a href="#">link</a></p>
  </body>
</html>

<!-- DOM tree:
document
└── html
    β”œβ”€β”€ head
    β”‚   └── title ("My Page")
    └── body
        β”œβ”€β”€ h1 ("Hello")
        └── p
            β”œβ”€β”€ "World "
            └── a ("link")
-->

Validate Your HTML

The W3C HTML Validator (validator.w3.org) checks your HTML for errors β€” unclosed tags, invalid nesting, missing required attributes. Running your pages through it is a good habit, especially when something displays oddly and you can't find why.

πŸ“‹ Summary

  • Tags have: opening tag, content, closing tag. Void elements have no content or closing tag.
  • Attributes go in the opening tag: name="value". Always use double quotes.
  • Elements must be closed in reverse order (last opened = first closed).
  • Tag names are case-insensitive but always write lowercase.
  • HTML collapses whitespace β€” indent freely for readability.
  • Use W3C Validator to catch syntax errors.

FAQ

What happens if I forget a closing tag? +

Modern browsers have error-correction built in β€” they try to fix common mistakes like unclosed tags. However, the correction is not always what you intended and results vary between browsers. Always close your tags explicitly to get predictable output.

Are self-closing slashes needed in HTML5? +

No. In HTML5 you can write <br> or <br/> β€” both are valid. The self-closing slash (/>) was required in XHTML but is optional in HTML5. Most developers omit it for brevity.