Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML HTML Basics Getting Started with HTML Lists and Links Challenge

Ryan Canty
PLUS
Ryan Canty
Courses Plus Student 5,665 Points

Turn each list item into a link. The first item should link to cakes.html, the second to pies.html and the third to cand

What is wrong with this code?! I can't for the life of me figure it out....

index.html
<!DOCTYPE html>
<html>
  <head>
    <title>Lists and Links</title>
  </head>
  <body>
  <ul>
    <a href="cakes.html"><li>Cakes</li></a>
    <a href="pies.html"><li>Pies</li></a>
    <a href="candy.html"><li>Candy</li></a>
    </ul>
  </body>
</html>

4 Answers

You just need to switch your tags around, with the <li> on the outside and <a> on the inside.

<li><a href="cakes.html">Cakes</a></li>
<li><a href="pies.html">Pies</a></li>
<li> <a href="candy.html">Candy</a></li>
Ryan Canty
Ryan Canty
Courses Plus Student 5,665 Points

I appreciate the response, thanks! But is there any practical reason for that apart from just passing the lesson? they both do the same thing

You are actually hyper linking (<a> tag) on the text of each list item. The list item tag (<li>) is a formatting tag that tells the browser that what is to follow is just a member of the unordered list (<ul>). The anchor tag is actually linking the text which is why it should surround only the text.

I hope that explanation helps.

From what I understand, it's because <li> is the child element of <ul>, so it has to come before <a>. I would also like someone else to elaborate on this...

This is correct. It would not be valid html if the <li> tags were inside the <a> tags.

a tags can not be direct children of ul tags

Here's a link to the ul element section of the html5 specification: https://www.w3.org/TR/html5/grouping-content.html#the-ul-element

The specification is not an easy thing to read but the important thing here is what is listed for "Content Model:" This is the part that tells you what is allowed to be directly inside of the element in question.

Here it states:

Zero or more li and script-supporting elements.

So nothing else besides that can be directly inside a ul.

<ul> <li><a href="cakes.html">Cakes</a></li> <li><a href="pies.html">Pies</a></li> <li><a href="candy.html">Candy</a></li> </ul>