Option 2 is correct: The nested <ul>
is a child of the <li>
it belongs in.
If you validate, option 1 comes up as an error in html 5 -- credit: user3272456
Correct: <ul>
as child of <li>
The proper way to make HTML nested list is with the nested <ul>
as a child of the <li>
to which it belongs. The nested list should be inside of the <li>
element of the list in which it is nested.
<ul>
<li>Parent/Item
<ul>
<li>Child/Subitem
</li>
</ul>
</li>
</ul>
A list item can contain another entire list — this is known as "nesting" a list. It is useful for things like tables of contents, such as the one at the start of this article:
- Chapter One
- Section One
- Section Two
- Section Three
- Chapter Two
- Chapter Three
The key to nesting lists is to remember that the nested list should relate to one specific list item. To reflect that in the code, the nested list is contained inside that list item. The code for the list above looks something like this:
<ol>
<li>Chapter One
<ol>
<li>Section One</li>
<li>Section Two </li>
<li>Section Three </li>
</ol>
</li>
<li>Chapter Two</li>
<li>Chapter Three </li>
</ol>
Note how the nested list starts after the <li>
and the text of the containing list item (“Chapter One”); then ends before the </li>
of the containing list item. Nested lists often form the basis for website navigation menus, as they are a good way to define the hierarchical structure of the website.
Theoretically you can nest as many lists as you like, although in practice it can become confusing to nest lists too deeply. For very large lists, you may be better off splitting the content up into several lists with headings instead, or even splitting it up into separate pages.