Notes 3
— print (last updated: Jun 28, 2009) print

Select font size:

Ordered and Unordered Lists

Lists (see HTML4.01 specs) are ways of organizing information in a hierarchical way according to a sequence of elements (numbered or not) along with their subsequences. HTML lists are usually thought of as combinations of the elements: ul (unordered list), ol (ordered list), and li (list item) (see the HTML4.01 ol & ul specs).

Unordered lists

These are the simplest to work with because they have the fewest controls.
An unordered list:
<ul>
<li>Cars</li>
<li>Trucks</li>
</ul>

looks like this:
An unordered list:
A sublist is a list contained entirely within a list item. If we want to list types of cars, we think of placing the entire sublist within the first li element like this:
A 2-deep unordered list:
<ul>
<li>Cars
   <ul>
   <li>Honda</li>
   <li>Subaru</li>
   </ul>
</li>
<li>Trucks</li>
</ul>

which looks like this:
A 2-deep unordered list:
When a sublist is introduced, it is worthwhile to maintain the entire list indented at the appropriate level. Going one step further, we can specify the type of Honda getting:
A 3-deep unordered list:
<ul>
<li>Cars
   <ul>
   <li>Honda
     <ul>
     <li>Civic</li>
     <li>Accord</li>
     </ul>
   </li>
   <li>Subaru</li>
   </ul>
</li>
<li>Trucks</li>
</ul>

which looks like this:
A 3-deep unordered list:
Observe how the levels are distinguished by list-item characters:
  • (disc),  
  • (circle),  
  • (square)
  • Any sublists at greater depths all use the square for itemization.

    List construction

    With complicated lists like the last one above, it may be best to construct this top-down as we constructed it from the examples 1, 2, and 3. Starting from list 1, separate Cars from the closing </li> and insert the indented list of Honda, Subaru. Then, from 2, separate Honda from the closing </li> and insert the indented list of Civic, Accord.

    Ordered lists

    Ordered lists use the ol element instead of ul. The difference is that the successive li elements are enumerated. Here is a simple example:
    An ordered list:
    <ol>
    <li>Cars
       <ol>
       <li>Honda</li>
       <li>Subaru</li>
       </ol>
    </li>
    <li>Trucks</li>
    </ol>
    
    
    which looks like this:
    An ordered list:
    1. Cars
      1. Honda
      2. Subaru
    2. Trucks
    An ordered list will use the decimal enumeration 1, 2, ... unless modified with the attributes: Here is an example:
    Starting here:
    <ol type="A">
    <li>Cars
       <ol type="i">
       <li>Honda</li>
       <li>Subaru</li>
       </ol>
    </li>
    <li>Trucks</li>
    </ol>
    And taking up from where we left off.
    <ol start="3" type="A">
    <li>Motorcycles</li>
    <li>Bicycles
        <ol type="i">
        <li value="3">racing</li>
        <li>touring</li>
        </ol>
    </li>
    </ol>
    
    
    which looks like this:
    Starting here:
    1. Cars
      1. Honda
      2. Subaru
    2. Trucks
    And taking up from where we left off.
    1. Motorcycles
    2. Bicycles
      1. racing
      2. touring
    These attributes are, unfortunately, deprecated, and technically should be replaced by style settings; nevertheless, they are useful in practice.

    List item elements used outside of lists

    The list item element (li) can be used outside lists, but its behavior is possibly not what you want if the enclosed text spans more than one line. For example if we have this:
    <li>
    The list item element (<tt>li</tt>) can be used outside lists, 
    but its behavior is possibly not what you want if the
    enclosed text spans more than one line.
    </li>
    
    
    which looks like this:
  • The list item element (li) can be used outside lists, but its behavior is possibly not what you want if the enclosed text spans more than one line.
  • For single line items, this is often OK, because it appears to be nearly impossible to make a list not indent.

    List syntax issues

    The official HTML specification requires closing tags for ol, ul but it permits the omission of these closing tag for li. Nevertheless, you should strive to use these closing tags (correctly) to be in conformance with the XHTML standards which do require them. One of the by-products of allowing the omission of closing tags is the fact that this list with incorrect XHTML syntax still produces the desired results:
    <ul>
    <li>Cars</li>
      <ul>
      <li>Honda</li>
      <li>Subaru</li>
      </ul>
    <li>Trucks</li>
    </ul>
    
    
    In effect, HTML is disregarding the closing li tags.

    Definition lists

    Definition lists use the elements: dl (definition list), dt (definition term) and dd (definition description) (see the HTML4.01 dl, dt & dd specs).

    The overall structure is contained by the dl element tags, and within this are dt elements (terms); within dt elements are dd elements (term descriptions). Here is an example:
    Here are my terms
    <dl>
      <dt>HTML
      <dd>Hypertext Markup Language</dd>
      </dt>
      <dt>XML
      <dd>Extensible Markup Language</dd>
      </dt>
    </dl>
    
    which looks like this:
    Here are my terms
    HTML
    Hypertext Markup Language
    XML
    Extensible Markup Language


    © Robert M. Kline