McTweak.ai

Learn to code with the McTweak crew!

Chapter 2, Episode 4

Nested Lists

Lists inside Lists: Creating Hierarchies

AllyMcTweak

AllyMcTweak

So our client wants us to organize their "500 Greatest Foods of All Time" list... but right now it's just one gigantic bullet-point disaster.

TrashyMcTweak

TrashyMcTweak

It's like a FOOD AVALANCHE! A CULINARY CATASTROPHE! A GASTRONOMICAL GARBAGE FIRE! (gestures wildly at the screen) Why is "habanero ice cream" right next to "grandma's pot roast"? WHY IS "DEEP FRIED BUTTER" LISTED SEVENTEEN TIMES?!

GrumpyMcTweak

GrumpyMcTweak

This list is an ORGANIZATIONAL NIGHTMARE! No categories! No hierarchy! Just a random JUMBLE of food items stretching into the DIGITAL ABYSS! How is ANYONE supposed to navigate this LABYRINTH OF CULINARY CHAOS?!

CodyMcTweak

CodyMcTweak

Um, I could try putting them in alphabetical order? That's... free to implement...

FattyMcTweak

FattyMcTweak

(munching loudly) Mmff—you could try—*gulp*—grouping them by price point! Premium foods at the top, budget foods at the bottom. (licks fingers) Users always want to see the expensive options first.

AshleyMcTweak

AshleyMcTweak

The client specifically requested "a well-organized hierarchy that makes logical sense." They did NOT specify alphabetical OR price-based ordering. They want categories and subcategories.

AllyMcTweak

AllyMcTweak

So we need nested lists—lists within lists. It's the perfect way to organize hierarchical information like this.

TrashyMcTweak

TrashyMcTweak

NESTED LISTS? Like INCEPTION but with BULLET POINTS? (makes explosion sound) MIND. BLOWN.

CodyMcTweak

CodyMcTweak

How do you put a list... inside another list? Wouldn't it just make one super long list?

GarbageMcTweak

GarbageMcTweak

(looking at the screen) Classic nested list problem. You need structure, hierarchy. Like a family tree, but for data.

CodyMcTweak

CodyMcTweak

Oh! So we could have main categories like "Breakfast Foods" and then inside that list, we could have "Pancakes," "Waffles," and stuff?

SnowzieMcTweak

SnowzieMcTweak

(drops toy at keyboard, barks proudly)

GarbageMcTweak

GarbageMcTweak

See that burger toy? It has layers—bun, patty, cheese, tomato, lettuce, bun. That's like our food list hierarchy. Main categories are the buns holding everything together. Subcategories are the fillings, each in their proper place.

Today's Mission

Create a hierarchical "My Favorite Foods" list using nested HTML lists to organize categories and subcategories.

Understanding Nested Lists

Nested lists are HTML lists that contain other lists within their list items. They help you create hierarchical structures to organize information logically - perfect for categories and subcategories!

What Are Nested Lists?

Regular List:

  • Item 1
  • Item 2
  • Item 3
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

Nested List:

  • Category A
    • Sub-item 1
    • Sub-item 2
  • Category B
    • Sub-item 3
    • Sub-item 4
<ul>
  <li>Category A
    <ul>
      <li>Sub-item 1</li>
      <li>Sub-item 2</li>
    </ul>
  </li>
  <li>Category B
    <ul>
      <li>Sub-item 3</li>
      <li>Sub-item 4</li>
    </ul>
  </li>
</ul>

Key Point

The nested list is placed inside the <li> element, not between list items. This is the most common mistake when creating nested lists!

Types of Nested Lists

You can nest any type of list inside any other list - unordered inside ordered, ordered inside unordered, and even multiple levels deep!

Unordered in Unordered

  • Fruits
    • Apples
    • Bananas
    • Oranges
  • Vegetables
    • Carrots
    • Broccoli
<ul>
  <li>Fruits
    <ul>
      <li>Apples</li>
      <li>Bananas</li>
      <li>Oranges</li>
    </ul>
  </li>
  <li>Vegetables
    <ul>
      <li>Carrots</li>
      <li>Broccoli</li>
    </ul>
  </li>
</ul>

Ordered in Unordered

  • Morning Routine
    1. Wake up
    2. Brush teeth
    3. Eat breakfast
  • Evening Routine
    1. Dinner
    2. Homework
    3. Sleep
<ul>
  <li>Morning Routine
    <ol>
      <li>Wake up</li>
      <li>Brush teeth</li>
      <li>Eat breakfast</li>
    </ol>
  </li>
  <li>Evening Routine
    <ol>
      <li>Dinner</li>
      <li>Homework</li>
      <li>Sleep</li>
    </ol>
  </li>
</ul>

Multiple Levels Deep

  • Continents
    • North America
      • United States
        • New York
        • California
      • Canada
    • Europe
      • France
      • Italy

Warning from GrumpyMcTweak

"DON'T GO TOO DEEP! Three to four levels is usually the practical limit before users get LOST IN THE NESTED ABYSS!"

Creating Nested Lists: Step by Step

1

Start with your main list

Begin with a regular unordered <ul> or ordered <ol> list.

<ul>
  <li>Food Category 1</li>
  <li>Food Category 2</li>
  <li>Food Category 3</li>
</ul>
2

Add a nested list inside a list item

Place a new list inside a list item, right after the text but before the closing </li> tag.

<ul>
  <li>Food Category 1
    <ul>
      <li>Food Item A</li>
      <li>Food Item B</li>
    </ul>
  </li>
  <li>Food Category 2</li>
  <li>Food Category 3</li>
</ul>

AllyMcTweak says: "Notice the indentation! Proper indentation makes nested lists much easier to read and debug."

3

Add more nested lists as needed

Continue adding nested lists to other list items or even create a third level by nesting lists within nested lists.

<ul>
  <li>Food Category 1
    <ul>
      <li>Food Item A</li>
      <li>Food Item B</li>
    </ul>
  </li>
  <li>Food Category 2
    <ul>
      <li>Food Item C</li>
      <li>Food Item D
        <ul>
          <li>Variant 1</li>
          <li>Variant 2</li>
        </ul>
      </li>
    </ul>
  </li>
  <li>Food Category 3</li>
</ul>

Creating a Food Hierarchy

Let's build a "My Favorite Foods" hierarchy using nested lists - perfect for organizing a food blog or restaurant menu!

My Favorite Foods Example

<ul>
  <li>Breakfast Foods
    <ul>
      <li>Pancakes
        <ul>
          <li>Blueberry</li>
          <li>Chocolate Chip</li>
        </ul>
      </li>
      <li>Waffles</li>
      <li>French Toast</li>
    </ul>
  </li>
  <li>Lunch Options
    <ul>
      <li>Sandwiches
        <ul>
          <li>BLT</li>
          <li>Grilled Cheese</li>
        </ul>
      </li>
      <li>Soups
        <ul>
          <li>Tomato</li>
          <li>Chicken Noodle</li>
        </ul>
      </li>
    </ul>
  </li>
  <li>Dinner Favorites
    <ul>
      <li>Pizza</li>
      <li>Pasta</li>
      <li>Steak</li>
    </ul>
  </li>
</ul>

Result:

  • Breakfast Foods
    • Pancakes
      • Blueberry
      • Chocolate Chip
    • Waffles
    • French Toast
  • Lunch Options
    • Sandwiches
      • BLT
      • Grilled Cheese
    • Soups
      • Tomato
      • Chicken Noodle
  • Dinner Favorites
    • Pizza
    • Pasta
    • Steak

List Structure:

  • Level 1 Main categories (meal types)
  • Level 2 Food groups or dishes
  • Level 3 Specific varieties

TrashyMcTweak's Tip

"Want to make your lists look EXTRA FANCY? Use CSS to style different list levels with different bullet styles! You can use list-style-type to change from discs to circles to squares as you go deeper!"

/* CSS for styling different list levels */
ul { list-style-type: disc; }
ul ul { list-style-type: circle; }
ul ul ul { list-style-type: square; }

Your Turn: Build a Food Hierarchy

Create Your "My Favorite Foods" List

Now it's your turn to create a nested list of your favorite foods! Edit the HTML below to create your own food hierarchy with at least 3 main categories and 2 levels of nesting.

Code:

Result:

Your list will appear here when you click "Run Code".

Common Nested List Mistakes

❌ Incorrect ✅ Correct
Putting a list between list items:
<ul>
  <li>Item 1</li>
  <ul>  <-- Error! List not inside an <li>
    <li>Sub-item</li>
  </ul>
  <li>Item 2</li>
</ul>
Putting a list inside a list item:
<ul>
  <li>Item 1
    <ul>  <-- Correct! List inside <li>
      <li>Sub-item</li>
    </ul>
  </li>
  <li>Item 2</li>
</ul>
Forgetting to close list items properly:
<ul>
  <li>Item 1
    <ul>
      <li>Sub-item</li>
    </ul>
  <-- Missing </li> tag!
  <li>Item 2</li>
</ul>
Properly closing all tags:
<ul>
  <li>Item 1
    <ul>
      <li>Sub-item</li>
    </ul>
  </li>  <-- Properly closed </li> tag
  <li>Item 2</li>
</ul>

GrumpyMcTweak's Warning

"INDENTATION ISN'T JUST FOR SHOW! Proper indentation helps you spot MISSING TAGS and STRUCTURAL ERRORS that will BREAK YOUR PAGE HIERARCHY!"

Final Challenge: Advanced Food Hierarchy

Create a Multi-Level Restaurant Menu

For extra practice, try creating a restaurant menu with at least three levels of nesting:

  1. Meal categories (Appetizers, Main Courses, Desserts)
  2. Food types within each category
  3. Specific dishes with details

Example Structure:

  • Appetizers
    • Salads
      • Caesar Salad (with croutons and parmesan)
      • Greek Salad (with feta and olives)
    • Soups
      • French Onion (topped with gruyere)
      • Tomato Bisque (with basil garnish)
  • Main Courses
    • Pasta Dishes...
    • Grilled Items...

Wrap-Up

What We Learned:

  • Nested lists create hierarchical structures in HTML
  • A nested list goes inside a list item (<li>), not between items
  • You can nest unordered lists (<ul>) or ordered lists (<ol>) in any combination
  • Proper indentation is crucial for maintaining readable code
  • Lists can be nested multiple levels deep (but should usually be limited to 3-4 levels)
SnowzieMcTweak

Code approved by SnowzieMcTweak!

Hierarchy established. Lists nested. Mission accomplished.