Bird
Raised Fist0
HTMLmarkup~5 mins

Nested elements in HTML

Choose your learning style10 modes available
Introduction

Nested elements help organize content inside other content, like putting boxes inside bigger boxes. This makes your webpage structured and easy to understand.

When you want to group related content together, like a paragraph inside a section.
When you need to create lists inside other lists, such as a list of steps with sub-steps.
When building menus with submenus that appear inside main menus.
When adding images or links inside text blocks.
When structuring forms with labels and inputs inside form tags.
Syntax
HTML
<parent-element>
  <child-element>
    <!-- content or more nested elements -->
  </child-element>
</parent-element>
Every opening tag must have a matching closing tag to keep the structure clear.
Indent child elements inside their parent to make the code easier to read.
Examples
A section contains a heading and a paragraph nested inside it.
HTML
<section>
  <h1>Title</h1>
  <p>This is a paragraph inside a section.</p>
</section>
A list with nested sublist inside the first item.
HTML
<ul>
  <li>Item 1
    <ul>
      <li>Subitem 1a</li>
      <li>Subitem 1b</li>
    </ul>
  </li>
  <li>Item 2</li>
</ul>
Navigation menu with links nested inside list items.
HTML
<nav>
  <ul>
    <li><a href="#home">Home</a></li>
    <li><a href="#about">About</a></li>
  </ul>
</nav>
Sample Program

This webpage shows a section with a heading and a list of fruits. The list has a nested list inside the Banana item to show types of bananas.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Nested Elements Example</title>
</head>
<body>
  <main>
    <section>
      <h2>My Favorite Fruits</h2>
      <ul>
        <li>Apple</li>
        <li>Banana
          <ul>
            <li>Yellow Banana</li>
            <li>Green Banana</li>
          </ul>
        </li>
        <li>Cherry</li>
      </ul>
    </section>
  </main>
</body>
</html>
OutputSuccess
Important Notes
Tip: Use indentation to see which elements are inside others easily.
Remember: Browsers read nested elements from outside in, so the order matters.
Nested elements help with styling and accessibility by grouping related content.
Summary
Nested elements mean putting HTML tags inside other tags.
They help organize content clearly and logically on a webpage.
Always close your tags and indent nested elements for readability.

Practice

(1/5)
1. What does it mean to have nested elements in HTML?
easy
A. Putting one HTML tag inside another tag
B. Writing HTML tags without closing them
C. Using only one HTML tag per page
D. Adding CSS styles inside HTML tags

Solution

  1. Step 1: Understand the meaning of nested elements

    Nested elements mean placing one HTML tag inside another to organize content.
  2. Step 2: Compare options with this meaning

    Only Putting one HTML tag inside another tag describes putting one tag inside another, which matches nested elements.
  3. Final Answer:

    Putting one HTML tag inside another tag -> Option A
  4. Quick Check:

    Nested elements = tags inside tags [OK]
Hint: Nested means inside another tag, like boxes inside boxes [OK]
Common Mistakes:
  • Confusing nesting with styling
  • Thinking nesting means missing closing tags
  • Assuming nesting means one tag per page
2. Which of the following is the correct way to nest elements in HTML?
easy
A.

Text

B.

Text

C.

Text

D. <div>

Text

Solution

  1. Step 1: Check tag order and closing

    Correct nesting means tags open and close in order: parent opens, child opens, child closes, parent closes.
  2. Step 2: Validate each option

    Text

    follows correct order: <div> <p> Text </p> </div>. Others have mismatched or missing closing tags.
  3. Final Answer:

    <div><p>Text</p></div> -> Option B
  4. Quick Check:

    Tags must close in reverse order of opening [OK]
Hint: Tags must close in reverse order they open [OK]
Common Mistakes:
  • Closing parent before child
  • Swapping tag order
  • Forgetting closing tags
3. What will the browser display for this HTML code?
<div>Hello <strong>World</strong>!</div>
medium
A. Hello World! (with 'World' bolded)
B. Hello World!
C. Hello World! (all normal text)
D. Error: tags not closed

Solution

  1. Step 1: Identify nested tags and their effect

    The <strong> tag inside <div> makes the word 'World' bold.
  2. Step 2: Understand browser rendering

    Browser shows 'Hello ' normal, 'World' bold, and '!' normal, all inside a block.
  3. Final Answer:

    Hello World! (with 'World' bolded) -> Option A
  4. Quick Check:

    <strong> makes text bold inside parent [OK]
Hint: Tags inside text change style of that part only [OK]
Common Mistakes:
  • Thinking tags show as text
  • Ignoring effect of <strong>
  • Assuming error due to nesting
4. Find the error in this nested HTML snippet:
<ul>
  <li>Item 1
  <li>Item 2</li>
</ul>
medium
A. Extra closing tag for <ul>
B. Using <li> outside <ul>
C. Incorrect nesting of <ul> inside <li>
D. Missing closing tag for first <li>

Solution

  1. Step 1: Check each list item tag

    The first <li> tag has no closing </li> before the second <li> starts.
  2. Step 2: Confirm proper list structure

    Each <li> must be closed inside <ul>. Missing closing tag causes invalid HTML.
  3. Final Answer:

    Missing closing tag for first <li> -> Option D
  4. Quick Check:

    Every <li> needs a matching </li> [OK]
Hint: Close each list item before starting next [OK]
Common Mistakes:
  • Assuming <li> auto-closes
  • Confusing <ul> and <li> roles
  • Ignoring missing closing tags
5. You want to create a navigation menu with nested submenus using HTML. Which structure correctly nests a submenu inside a main menu item?
hard
A.
B.
C.
D.

Solution

  1. Step 1: Understand submenu nesting

    A submenu is a <ul> inside a <li> of the main menu to group related items.
  2. Step 2: Check each option's structure

    nests <ul> with 'Team' and 'History' inside the 'About' <li>, which is correct. Others break nesting rules or place <ul> outside <li>.
  3. Final Answer:

    <nav><ul><li>Home</li><li>About<ul><li>Team</li><li>History</li></ul></li><li>Contact</li></ul></nav> -> Option C
  4. Quick Check:

    Submenu <ul> inside <li> = correct nesting [OK]
Hint: Put submenu <ul> inside main menu <li> [OK]
Common Mistakes:
  • Placing submenu <ul> outside <li>
  • Not closing <li> before submenu
  • Mixing order of <ul> and <li>
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard