Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMario Zampollo
11,292 Pointswhy <li><a>... and not <a><li>?
Why should I code e.g.:
<li><a href="portfolio.html">Portfolio</a></li>
instead of
<a href="portfolio.html"><li>Portfolio</li></a>?
Is there a specific reason to nest the anchor tag inside the list item tag rather than the other way around?
3 Answers
Jennifer Rai
Courses Plus Student 13,164 PointsHi Mario,
The reason that the anchor tag goes inside of the list-item is because the list item is a block level element and the anchor tag is an inline element.
Block elements (like li tags - also div, p, ul, ol, etc) generally start and end on a new line.
Where as inline elements (like anchor tags - as well as span, strong, em, etc) are normally displayed without starting a new line and can be nested inside of block elements.
Mario Zampollo
11,292 PointsReally helpful, thanks!
Jason Anello
Courses Plus Student 94,610 PointsThe html5 specification does allow the a
element to contain block level elements. Previously not allowed in older versions.
The use case for this is probably if you have an advertisement or pricing table that is made up of multiple elements and you want the whole thing to be one big clickable link. You could then wrap the whole thing in anchor tags, provided there's no interactive content within. Previous solutions required javascript.
The technical reason for why we can't do the code posted in the question is because the html specification doesn't allow it. Browsers might happily render it (Note: I didn't test), but I don't think it will pass the html validator. Traditionally, browsers have been forgiving of the html we write.
The specification states that a ul
or ol
has to contain zero or more li
or script supporting elements. No other element can be a direct child of a ul
or ol
.
Amin Rawah
3,803 PointsHi Mario..
because you first create an <ul>
,so you want tell the HTML this is my <li>
then you can specify if it is link or not..
i hope this helps :)
Mario Zampollo
11,292 PointsSure! a list tag is what it is expected to follow right after a unordered-list tag. It makes sense. Thanks!
Dave McFarland
Treehouse TeacherDave McFarland
Treehouse TeacherMario,
Your HTML code wasn't showing up correctly. I fixed it in your post.
For the future, to put HTML/CSS/JavaScript in a forum post:
hit return to create a new line and type three back tick characters ```
hit return to create another new line and paste your HTML
hit return and add three more back tick characters: ``` The back tick character isn't the same as the single quote -- ' -- mark; the back tick is located on the same key as the ~ on most keyboards.
Also to add correct color highlighting add the name of the language after the back ticks like this: ```CSS. Here's what CSS should look like in a forum post: