Learn HTML and CSS: Nesting
Nesting and the tree
"My nephew says HTML is a tree," says Maggie. "All I see is text." Time to show Maggie that tree.
Elements inside elements
An element can contain other elements. That's called nesting. The element around them is the parent, the elements inside are its children.
That's how a tree grows: <main> contains a heading and a list, and the list contains the flavours. A bulleted list is <ul>, each item in it an <li>. An <li> always belongs in a list: it's a child of <ul>.
An element runs until its closing tag. So what opens last closes first: first </li>, then </ul>, then </main>.
Indenting your code makes the tree visible. The browser doesn't care, but you will.
Under the page is the tree as the browser sees it: every element indented under its parent.
<main> <h2>Our flavours</h2> <ul> <li>Vanilla</li> <li>Pistachio</li> <li>Strawberry</li> </ul></main>Try it yourself
Under the title, add a chapter Cups with a list of the sizes Small, Medium and Large below it.
<main> <h1>Cold Comfort</h1></main>body { margin: 0; font-family: system-ui, sans-serif; background: #fff8f0; color: #3b2a1a;}header { padding: 24px 16px; background: #ffd6e0;}.logo { display: inline-block; margin: 8px 0 0; padding: 6px 18px; border-radius: 999px; background: #ffffff; font-size: 28px;}.slogan { margin: 8px 0 0;}main { padding: 8px 16px 24px;}Ready for more?
The full lesson has 6 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson