Introduction
HTML5 establishes specific rules about how tags can be nested to ensure semantic correctness and consistent rendering across all browsers. Understanding these rules is fundamental to writing valid, maintainable HTML code.When unsure about nesting rules, validate your HTML using the W3C HTML Validator and check the HTML specification.
General Nesting Principles
Block Elements
Block elements can generally contain both block and inline elements.Inline Elements
Inline elements can only contain other inline elements and text content.Void Elements
Void elements cannot contain any child elements or text content.Specific Element Rules
Content Grouping Elements
Content Grouping Elements
Interactive Elements
Interactive Elements
<a> (Anchor)
Can contain: Inline elements and text (phrasing content)Cannot contain: Other <a> elements, interactive elements, or block elements<button> (Button)
Can contain: Inline elements and text onlyCannot contain: Block elements or interactive elements<label> (Form Label)
Can contain: Inline elements and textCannot contain: Block elementsList Elements
List Elements
<ul> / <ol> (Lists)
Can contain: Only <li> elements as direct childrenCannot contain: Text nodes directly or other elements as direct children<li> (List Item)
Can contain: Block and inline elements<dl>, <dt>, <dd> (Description List)
<dl> contains: Only <dt>/<dd> (or <div> wrapping pairs in HTML5)<dt> contains: Inline elements and text<dd> contains: Block and inline elementsTable Elements
Table Elements
Form Elements
Form Elements
<form> / <fieldset> (Forms)
Can contain: Block, inline, and interactive elementsBest practice: Use <legend> as the first child of <fieldset> for accessibility<select> / <datalist> (Selection Controls)
<select> contains: Only <option>/<optgroup> elements<datalist> contains: Only <option> elements for suggestionsCannot contain: Text nodes or other elements as direct children<option> (Select Option)
Can contain: Text only<textarea> (Text Area)
Can contain: Text onlyHeading Elements
Heading Elements
Semantic Structural Elements
Semantic Structural Elements
Semantic elements like
<header>, <nav>, <main>, <article>, <section>, <aside>, and <footer> are used to convey meaning and structure.Can contain: Block and inline elementsMedia & Interactive Widgets
Media & Interactive Widgets
<video> / <audio> (Media Elements)
Can contain: <source>, <track>, and fallback content (text)<picture> (Responsive Images)
Can contain: Only <source> elements and a single <img> (last child as fallback)Cannot contain: Other kinds of children<details> / <summary> (Disclosure Widget)
<details> contains: <summary> (recommended as first child) and other content<summary> contains: Inline elements and text<figure> / <figcaption> (Figure)
<figure> contains: Self-contained media with optional <figcaption><dialog> (Dialog Box)
Can contain: Block and inline elementsComplete Specific Rules Reference
The following table summarizes all specific nesting rules implemented in the HTML Tags Checker:| Parent Element | Allowed Children | Notes |
|---|---|---|
<p> | Inline elements only | Cannot contain block elements |
<li> | Block and inline elements | |
<dt> | Inline elements only | |
<dd> | Block and inline elements | |
<thead> | <tr> only | |
<tbody> | <tr> only | |
<tfoot> | <tr> only | |
<tr> | <td>, <th> only | |
<table> | <caption>, <colgroup>, <thead>, <tbody>, <tfoot>, <tr> | |
<ul> | <li> only | |
<ol> | <li> only | |
<dl> | <dt>, <dd> only | Can also contain <div> wrappers in HTML5 |
<select> | <option>, <optgroup> only | |
<optgroup> | <option> only | |
<option> | Text only | |
<textarea> | Text only | |
<a> | Inline elements and text | Cannot contain block elements or other <a> |
<button> | Inline elements and text | Cannot contain block elements or interactive elements |
<label> | Inline elements and text | Cannot contain block elements |
<h1> - <h6> | Inline elements only | |
<video> | <source>, <track>, text | |
<audio> | <source>, <track>, text | |
<picture> | <source> elements | Must have <img> as last child |
<details> | <summary>, block, inline | <summary> should be first child |
<dialog> | Block and inline elements | |
<summary> | Inline elements and text |
Common Invalid Patterns
-
Block elements inside
<p> -
Nested interactive elements
-
Block elements inside inline wrappers
-
Text nodes as direct children of lists
-
Form controls outside
<form>without theformattribute
Validation Tools
- W3C HTML Validator — Official HTML validation
- Browser DevTools — Inspect HTML structure in the DOM