A list is quite simply a series of things. In (X)HTML, there are two types of lists:
In this chapter, I will explain:
You may observe I used every type of list in this introduction. :-)
Item lists are simply a series of items. There are two types of item lists: ordered lists and unordered lists. Both these have the same child element: the list item element.
Element Name: li
The li
element must be the child element of either an ordered or unordered list element. It is a block element, and can contain any other body element, including paragraphs, divisions—even other lists. It can also contain text directly.
Element Name: ul
The unordered list gives no hint (aside from order of appearance) of any sequence in its contents. In essense, it's used when the order of its items isn't all that important. The ul
element can have only li
elements as child elements, but it can have as many of these child elements as you please.
Element Name: ol
What is the difference between the following lists?
If you guessed that List #2 implies that there is a specific order, you're absolutely right. The ordered list (which has the element name ol
) is a sequence of items that have a specific order.
A definition list is essentially a list of definitions, like a dictionary or glossary. It works a bit differently from item lists: a definition list's items come in two parts: the definition term and the definition description.
Element Name: dl
The dl
element can only contain definition term and definition description elements. Its job is simply to contain a definition list.
Element Name: dt
The dt
element holds the term to be defined. Only inline elements may be nested within it.
Element Name: dd
The dd
element contains the description of the term. Any body element can be nested within, and it can contain text directly.
The example below has dl
tags in bold, dt
elements underlined, and dd
elements in italics
Hyperlinks are often arranged in lists, especially when creating a links page or website navigation—after all, what are website navigation and links pages but lists of hyperlinks?
Question: Which of the above lists is suitable for hyperlinks?
Answer: All of them, depending on what you want to do.
Of course, by no means must you place links in lists alone—if a phrase within a paragraph serves well for the text of a hyperlink, by all means use it so—but if you have do a series of links that is not embedded within other text, it is probably best to use one of the above lists.
In the following examples, the a
elements (in other words, the hyperlinks) are underlined. The tags of other inline elements have been removed for clarity.
Most of the time, the order in which pages of a website is visited has little importance, so the ul
element is used.
Ordered lists are used when a series of pages are to be viewed in sequence. For example, an ordered list would be used for the chapters of an online book, or perhaps a multi-page glossary. The main page of this book contains several ordered lists for navigation, since it is a listing of the chapters.
Definition lists are not commonly used for lists of links, but they would be completely appropriate when you want to give information about each link.
Below is an example of lists nested within lists. The li
and dd
elements can both contain ul
, ol
, and/or dl
elements.
ul
Tags Underlined, ol
Tags In Italics, dl
Tags In BoldThe above was a basic example of different kinds of lists nested within each other. Below is an excellent example of nested lists of the same kind (in this case, ordered lists):
If the above list looks familiar, it should: it's the outline of this very chapter.