Proper way to make HTML nested list?

前端 未结 7 1252
执念已碎
执念已碎 2020-11-22 04:05

The W3 docs have a nested list example prefixed by DEPRECATED EXAMPLE:, but they never corrected it with a non-deprecated example, nor explained exactly what is

相关标签:
7条回答
  • 2020-11-22 04:19

    What's not mentioned here is that option 1 allows you arbitrarily deep nesting of lists.

    This shouldn't matter if you control the content/css, but if you're making a rich text editor it comes in handy.

    For example, gmail, inbox, and evernote all allow creating lists like this:

    With option 2 you cannot due that (you'll have an extra list item), with option 1, you can.

    0 讨论(0)
  • 2020-11-22 04:24

    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>
    

    W3C Standard for Nesting Lists

    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:

    1. Chapter One
      1. Section One
      2. Section Two
      3. Section Three
    2. Chapter Two
    3. 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.

    0 讨论(0)
  • 2020-11-22 04:26

    If you validate , option 1 comes up as an error in html 5, so option 2 is correct.

    0 讨论(0)
  • 2020-11-22 04:36

    Option 2

    <ul>
    <li>Choice A</li>
    <li>Choice B
      <ul>
        <li>Sub 1</li>
        <li>Sub 2</li>
      </ul>
    </li>
    </ul>
    

    Nesting Lists - UL

    0 讨论(0)
  • 2020-11-22 04:38

    Option 2 is correct.

    The nested list should be inside a <li> element of the list in which it is nested.

    Link to the W3C Wiki on Lists (taken from comment below): HTML Lists Wiki.

    Link to the HTML5 W3C ul spec: HTML5 ul. Note that a ul element may contain exactly zero or more li elements. The same applies to HTML5 ol. The description list (HTML5 dl) is similar, but allows both dt and dd elements.

    More Notes:

    • dl = definition list.
    • ol = ordered list (numbers).
    • ul = unordered list (bullets).
    0 讨论(0)
  • 2020-11-22 04:41

    I prefer option two because it clearly shows the list item as the possessor of that nested list. I would always lean towards semantically sound HTML.

    0 讨论(0)
提交回复
热议问题