Skip to main content

Void & Nested Elements ๐ŸŒฟ

Mentor's Note: Think of nesting elements like Russian Nesting Dolls โ€” one doll fits perfectly inside another. In HTML, tags must open and close in a strict order: the last tag opened must be the first tag closed! Let's learn how to nest correctly. ๐Ÿช†

๐Ÿ“š Educational Content: This guide covers the parent-child relationship in HTML and elements that do not contain content.

What You'll Learn

By the end of this tutorial, you'll know:

  • What Void (Empty) elements are and how to write them
  • How to nest HTML tags inside other tags correctly
  • HTML's hierarchical tree structure (Parent, Child, Sibling)
  • Common nesting mistakes that break layouts

๐ŸŒŸ The Scenario: The Gift Boxes ๐ŸŽโ€‹

Mental Model: Packing gifts in boxes. Imagine you are packing gifts for a friend:

  • The Big Box (<html>): The outermost container.
  • The Medium Box (<body>): A box placed inside the big box.
  • The Present (<strong>): The actual gift placed inside the medium box.
  • The Rule: You cannot close the big box until the medium box is closed!
  • Void Elements (<hr />): Single items that don't need a box (like a ribbon wrapped directly around a box). โœ…

๐Ÿ“– Void Elements (Empty Tags)โ€‹

Most HTML elements have an opening tag, content, and a closing tag. However, some elements cannot contain content or other tags. These are called Void Elements or Empty Tags.

Key Features of Void Elements:โ€‹

  • They do not have a closing tag (e.g., no </br>).
  • They only have an opening tag, which can contain attributes.
  • In modern HTML5, you do not need a trailing slash (/), though in XHTML it was written as <br />. HTML5 accepts both, but <br /> is preferred.

Common Void Elements:โ€‹

TagPurposeExample
<br />Line Break (starts a new line)Line one<br />Line two
<hr />Horizontal Rule (draws a separator line)<hr />
<img />Embeds an image<img src="pic.jpg" alt="Photo" />
<input />Creates a form input field<input type="text" />
<meta />Specifies metadata in the head<meta charset="UTF-8" />

๐Ÿ—๏ธ Nested Elements (Hierarchy)โ€‹

Nesting means placing one HTML element inside another element. This creates a parent-child relationship.

Syntax Rules:โ€‹

Tags must be closed in the reverse order they were opened.

  • Correct (Last In, First Out):
    <p>This is <strong><em>important</em></strong> text.</p>
  • Incorrect (Overlapping):
    <p>This is <strong><em>important</strong></em> text.</p>
    (Here, <strong> is opened first but closed before <em> is closed, causing overlapping layout issues in browsers).

Tree Structure Terminology:โ€‹

<div>
<h1>My Title</h1>
<p>A paragraph with a <a href="#">link</a>.</p>
</div>
  • Parent: <div> is the parent of <h1> and <p>.
  • Child: <h1> and <p> are children of <div>.
  • Sibling: <h1> and <p> are siblings because they share the same parent.
  • Descendant: The <a> tag is a child of <p> and a descendant of <div>.

๐ŸŽจ Visual Logic: Nesting Treeโ€‹

Here is how the browser sees nested elements as a tree structure:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Div (Parent) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”
โ”‚ H1 โ”‚ โ”‚ P โ”‚ (Siblings)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”
โ”‚ A โ”‚ (Descendant of Div)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿงช Interactive Elementsโ€‹

Quick Quizโ€‹

Quick Quiz

Identify the correctly nested HTML block:

  • <h1><em>Heading</h1></em>
  • <h1><em>Heading</em></h1>
  • <h1><em>Heading</em>
  • <em><h1>Heading</h1></em> (Note: em is inline and should not contain block h1)

Explanation: In <h1><em>Heading</em></h1>, the <em> tag is opened last, so it must be closed first. <h1> is closed last.


๐Ÿ“š Best Practices & Common Mistakesโ€‹

โœ… Best Practicesโ€‹

  • Indent nested tags: Always use 2 or 4 spaces of indentation for child elements. This makes the parent-child hierarchy visible:
    <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    </ul>
  • Avoid block elements inside inline elements: Never put block-level elements (like <div> or <h1>) inside inline elements (like <a> or <span>).

โŒ Common Mistakes โš ๏ธโ€‹

  • Overlapping tags:
    • Incorrect: <strong><em>Text</strong></em>
    • Correct: <strong><em>Text</em></strong>
  • Writing closing tags for void elements:
    • Incorrect: <br />content</br> or <img /></img>
    • Correct: <br /> and <img src="image.jpg" alt="text" />

๐Ÿ’ก Board Focus & Exam Tips ๐Ÿ‘”โ€‹

Common Board Questions (GSEB/CBSE/ICSE)โ€‹

  • Q: What are Empty Tags (Void elements) in HTML? Give two examples.
  • Answer: Empty tags are elements that do not contain any content or require a closing tag. Examples include <br /> (line break) and <hr /> (horizontal rule).
  • Q: Is HTML a hierarchical structure? Explain.
  • Answer: Yes, HTML is structured as a tree where elements nested inside other elements become child nodes of their containing parent element.


๐Ÿ“ Visit Us

๐Ÿซ VD Computer Tuition Surat

VD Computer Tuition
๐Ÿ“ Address
2/66 Faram Street, Rustompura
Surat โ€“ 395002, Gujarat, India
๐Ÿ“ž Phone / WhatsApp
+91 84604 41384
๐ŸŒ Website

Computer Classes & Tuition โ€” Areas We Serve in Surat

Adajanโ€ขAlthanโ€ขAmroliโ€ขAthwaโ€ขAthwalinesโ€ขBhagalโ€ขBhatarโ€ขBhestanโ€ขCanal Roadโ€ขChowkโ€ขCitylightโ€ขDumasโ€ขGaurav Pathโ€ขGhod Dod Roadโ€ขHaziraโ€ขJahangirpuraโ€ขKamrejโ€ขKapodraโ€ขKatargamโ€ขLimbayatโ€ขMagdallaโ€ขMajura Gateโ€ขMota Varachhaโ€ขNanpuraโ€ขNew Citylightโ€ขOlpadโ€ขPalโ€ขPandesaraโ€ขParle Pointโ€ขPiplodโ€ขPunaโ€ขRanderโ€ขRing Roadโ€ขRustampuraโ€ขSachinโ€ขSalabatpuraโ€ขSarthanaโ€ขSosyo Circleโ€ขUdhnaโ€ขVarachhaโ€ขVed Roadโ€ขVesuโ€ขVIP Road
๐Ÿ“ž Call Sir๐Ÿ’ฌ WhatsApp Sir