50

Is it possible to write W3C compliant multi-level bullet points (unordered list) in HTML?

Nested ul can be used, but is not W3C compliant.

 <ul>
     <li>myItem 1</li>
     <li>myItem 2</li>
     <ul>
        <li>myItem 2a</li>
     </ul>
     <li>myItem 3</li>
     <li>myItem 4</li>
 </ul>
  • myItem 1
  • myItem 2
    • myItem 2a
  • myItem 3
  • myItem 4

In Visual Studio, the above code gives the warning: Validation (XHTML 1.0 Transitional): Element 'ul' cannot be nested within element 'ul'

Techboy
  • 4,286
  • 5
  • 36
  • 45
  • 2
    What does the validator complain about when you tried it? Please post sample HTML and exact validation output. – Anon Dec 16 '10 at 21:35
  • Have you tried putting an `
      ` inside a `
    • ` and seeing what the validator says?
    – Felix Dec 16 '10 at 21:37
  • You should be able to naturally. If not, you're doing it wrong. – Ben Dec 16 '10 at 21:38
  • As this thread bore out, that's not the case, Ben. Here is a similar thread: http://stackoverflow.com/questions/2235257/how-can-i-produce-an-nested-list-in-xhtml-strict-without-giving-the-nested-lists – hotshot309 May 22 '12 at 19:29

2 Answers2

93

The only valid child of either a ul or ol is an li element; an li can, however, contain a ul (or ol). To achieve your aim:

<ul>
  <li>myItem 1</li>
  <li>myItem 2</li>
  <li style="list-style-type:none">
    <ul>
      <li>myItem 2a</li>
    </ul>
  </li>
  <li>myItem 3</li>
  <li>myItem 4</li>
</ul>
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • ...if someone could edit my code block as code, it'd be appreciated (I don't know if it's the weirdness of iPhone editing, or my own misuse that's screwing it up, despite repeated attempts). – David Thomas Dec 16 '10 at 22:00
  • 1
    I haven't got enough points to edit your answer :-( Your code creates an unwanted bullet point. How can I get rid of that? Thanks. – Techboy Dec 16 '10 at 22:07
  • 1
    WTF? The last `
      ` broke the entire formatting until I removed all the spaces. Never seen that before. Must have been some evil Apple trickery :)
    – Pekka Dec 16 '10 at 23:41
  • Thanks @Pekka, I'm glad it wasn't just me that was confused, I had started to worry =) – David Thomas Dec 17 '10 at 08:41
  • This is not the answer because it creates an unwanted bullet (before the nested
      ).
    – Techboy Dec 17 '10 at 15:45
  • 2
    It's the only **valid (x)html** solution. The extra bullet should be dealt with via css. I'd suggest asking a separate question for help with that problem. – David Thomas Dec 17 '10 at 15:52
  • 15
    Actually all you have to do is put the nested UL in the 2nd li rather than creating a blank one. Then you won't get the unwanted bullet – Morgan T. Jun 05 '12 at 14:51
20

Complementing David Thomas answer, this would remove the unwanted bullet:

<ul>
    <li>myItem 1</li>
    <li>myItem 2        
        <ul>
            <li>myItem 2a</li>
        </ul>
    </li>
    <li>myItem 3</li>
    <li>myItem 4</li>
</ul>
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Marlon
  • 1,719
  • 3
  • 20
  • 42