HTML: The Definitive Guide

Previous Chapter 8
Formatted Lists
Next
 

8.3 The <li> Tag

It should be quite obvious to you by now that the <li> tag defines an item in a list. It's the universal tag for HTML list items in ordered (<ol>) and unordered (<ul>) lists, as we discuss above, and for directories (<dir>) and menus (<menu>), which we discuss in detail later in this chapter.

Because the end of a list element can always be inferred by the surrounding document structure, most authors omit the ending </li> tags for their list elements. That makes sense because it becomes easier to add, delete, and move elements around within a list. We recommend not using the </li> end tag.

Although universal in meaning, there are some differences and restrictions to the use of the <li> tag for each HTML list type. In unordered and ordered lists, what follows the <li> tag may be nearly anything, including other lists and multiple paragraphs. Typically, if it handles indentation at all, the browser successively indents nested list items, and the content in those items is justified to the innermost indented margin.

Directory and menu lists are another matter. They are lists of short items like a single word or simple text blurb and nothing else. Consequently, <li> items within <dir> and <menu> tags may not contain other lists or other block elements, including paragraphs, preformatted blocks, or forms.

Clean documents, fully compliant with the HTML standard, should not contain any text or other document item inside the unordered, ordered, directory, or menu lists that is not contained within an <li> tag. Most browsers are tolerant of violations to this rule, but then you can't hold the browser responsible for compliant rendering for exceptional cases, either.

Changing the Style and Sequence of Individual List Items

Just as you can change the bullet or numbering style for all of the items in an unordered or ordered list, you also can change the style for individual items within those lists. With ordered lists, you also can change the value of the item number. As you'll see, the combinations of changing style and numbering can lead to a variety of useful list structures, particularly when included with nested lists.

The type attribute

Acceptable values for the type attribute in the <li> tag are the same as the values for the appropriate list type: items within unordered lists may have their type set to circle, square, or disc, while items in an ordered list may have their type set to any of the values shown previously in Table 8.1. The change affects the current item and any subsequent items in the list.

There is no way to revert back to the list's default type once you have changed the type for a single item; you'll need to explicitly reset the type on the next item. Thus, to make a single item in a list different from the rest, you'll need to change two items: the actual item you want changed, and the next item, which must be changed back to the general list format.

Figure 8.4 shows the effect that changing the type for an individual item in an ordered list has on subsequent items, as rendered by Netscape from the following source:

<ol>
  <li type=A>Changing the numbering style
  <li type=a>Doesn't alter the order! 
  <li> &lt;-- See? It's a "c"!
  <li type=I>Uppercase Roman numerals!
  <li type=i>Lowercase Roman numerals!
  <li type=1>Plain ol' numbers!
</ol>

Notice how the type attribute changes the display style of the number, but not the value of the number itself.

The value attribute

The value attribute lets you change the number of a specific list item and the ones that follow it. Since the ordered list is the only HTML list with sequentially numbered items, the value attribute is only valid when used within an <li> tag inside an ordered list.

To change the current and subsequent numbers attached to each item in an ordered list, simply set the value attribute to any integer. For example, the following source uses the value attribute to jump the numbering on items in an ordered list:

<ol>
  <li>Item number 1
  <li>And the second
  <li value=9> Jump to number 9
  <li>And continue with 10...
</ol>

The results are shown as rendered by Netscape in Figure 8.5.

The style and class attributes

The style attribute for the <li> tag creates an inline style for the elements enclosed by the tag, overriding any other style rule in effect. The class attribute lets you format the content according to a predefined class of the <li> tag; its value is the name of that class. [the section called "Inline Styles: The style Attribute"] [the section called "Style Classes"]


Previous Home Next
Ordered Lists Book Index Nesting Lists

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell